-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_powi.py
More file actions
81 lines (62 loc) · 2.35 KB
/
check_powi.py
File metadata and controls
81 lines (62 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import subprocess
import sys
import os
import re
import time
from tqdm import tqdm
def pow_to_powi(text):
# Finds all possible std::pow(x, n), gcem::pow(x, n), or admath::pow(x, n)
# where n is a potential integer to amrex::Math::powi<n>(x)
# Match all positive and negative integers, whole float numbers
# with and without _rt
integer_pattern = r"-?(?:\d+\.0*(?:e0)?(?:_rt)?|\d)"
# Check for an integer in the second argument
match_pattern = rf"([^,]+),\s*({integer_pattern})"
# Match fails when there is a nested pow, so only inner most pow is matched
negate_pattern = rf"(?![\s\S]*(?:std|gcem|admath)::pow\((?:[^,]+),\s*(?:{integer_pattern})\))"
# Final pattern
pattern = rf"(?:std|gcem|admath)::pow\({negate_pattern}{match_pattern}\)"
def replacement(match):
x = match.group(1)
n = match.group(2)
# Only extracts out the integer part before decimal point
n = n.split('.')[0] if '.' in n else n
return f"amrex::Math::powi<{n}>({x})"
text = re.sub(pattern, replacement, text)
return text
def process_file(inpath, outpath):
# Process a single file
try:
with open(inpath, 'r') as file:
old_content = file.read()
# Iterate over content until content is the same
# This is used to get rid of potential nested pow
new_content = pow_to_powi(old_content)
while new_content != old_content:
old_content = new_content
new_content = pow_to_powi(old_content)
with open(outpath, 'w') as file:
file.write(new_content)
print(f"Successfully wrote to {outpath}")
except Exception as e:
print(f"Error processing file {inpath}: {e}")
sys.exit(1)
if __name__ == '__main__':
# Get file paths
inpath = sys.argv[1]
outpath = sys.argv[2]
# Check if input path is a valid file
if not os.path.isfile(inpath):
print(f"Error: {inpath} is not a valid file path.")
sys.exit(1)
# Start timer
start_time = time.time()
# Process the file
with tqdm(total=1, desc="Processing file") as pbar:
process_file(inpath, outpath)
pbar.update(1)
# End timer
end_time = time.time()
# Calculate elapsed time
elapsed_time = end_time - start_time
print(f"Total processing time: {elapsed_time:.2f} seconds")