ADflow  v1.0
ADflow is a finite volume RANS solver tailored for gradient-based aerodynamic design optimization.
autoEditReverse.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 """
3 autoEdit - A Python tool to automatically edit a set of files
4  according to the specified user rules:
5 G. Kenway
6 """
7 
8 # Import modules
9 import os
10 import sys
11 import re
12 
13 # Specify file extension
14 EXT = "_b.f90"
15 
16 DIR_ORI = sys.argv[1]
17 DIR_MOD = sys.argv[2]
18 
19 # Specifiy the list of LINE ID's to find, what to replace and with what
20 patt_modules = re.compile(r"(\s*use\s*\w*)(_b)\s*")
21 patt_module = re.compile(r"\s*module\s\w*")
22 patt_module_start = re.compile("(\s*module\s)(\w*)(_b)\s*")
23 patt_module_end = re.compile("(\s*end module\s)(\w*)(_b)\s*")
24 patt_subroutine = re.compile(r"\s*subroutine\s\w*")
25 patt_subend = re.compile(r"\s*end\s*subroutine")
26 patt_comment = re.compile(r"\s*!.*")
27 patt_inttype = re.compile(r"\s*integer\*4\s\w*")
28 
29 print("Directory of input source files :", DIR_ORI)
30 print("Directory of output source files :", DIR_MOD)
31 
32 useful_modules = [
33  "bcroutines_b",
34  "turbbcroutines_b",
35  "utils_b",
36  "flowutils_b",
37  "walldistance_b",
38  "bcpointers_b",
39  "initializeflow_b",
40  "turbutils_b",
41  "sa_b",
42  "fluxes_b",
43  "solverutils_b",
44  "residuals_b",
45  "surfaceintegrations_b",
46 ]
47 
48 for f in os.listdir(DIR_ORI):
49  if f.endswith(EXT):
50  # open original file in read mode
51  file_object_ori = open(os.path.join(DIR_ORI, f), "r")
52  print("\nParsing input file", file_object_ori.name)
53 
54  # read to whole file to string and reposition the pointer
55  # at the first byte for future reading
56  all_src = file_object_ori.read()
57  file_object_ori.seek(0)
58 
59  # First we want to dertmine if it is a 'useful' module or a
60  # 'useless' module. A useful module is one that has
61  # subroutines in it.
62  isModule = False
63  hasSubroutine = False
64  for line in file_object_ori:
65  line = line.lower()
66  if patt_module.match(line):
67  isModule = True
68  if patt_subroutine.match(line):
69  hasSubroutine = True
70 
71  # If we have a module, close the input and cycle to next file.
72  if isModule and not hasSubroutine:
73  file_object_ori.close()
74  continue
75  elif isModule and hasSubroutine:
76  f = f.replace("_b", "_b")
77 
78  # open modified file in write mode
79  file_object_mod = open(os.path.join(DIR_MOD, f), "w")
80 
81  # Go back to the beginning
82  file_object_ori.seek(0)
83  inSubroutine = False
84 
85  for line in file_object_ori:
86  # Just deal with lower case string
87  line = line.lower()
88 
89  # Replace _cb on calls
90  if "_cb" in line:
91  line = line.replace("_cb", "")
92 
93  # Replace _b modules with normal -- except for the useful
94  # ones.
95  m = patt_modules.match(line)
96  if m:
97  found = False
98  for m in useful_modules:
99  if m in line:
100  found = True
101  if found:
102  line = line.replace("_b", "_b", 1)
103  else:
104  line = line.replace("_b", "")
105 
106  # Tapenade is using nonstandard type declaration incompatible with f2008
107  # Remove for now and depend on compiler kind default, which should be in
108  # almost all cases 4-bytes
109  m = patt_inttype.match(line)
110  if m:
111  line = line.replace("integer*4", "integer")
112 
113  # # See if we need to modify the line with changing the
114  # # module names
115  # m = patt_module_start.match(line)
116  # if m:
117  # line = 'module %s_b2\n'%m.group(2)
118 
119  # m = patt_module_end.match(line)
120  # if m:
121  # line = 'end module %s_b2\n'%m.group(2)
122 
123  # Tapenade misses one function in inviscidupwindflux_b and we need to add it manually
124  # We once we know we are withing the subroutine we just search for a very specific string append
125  if patt_subroutine.match(line) and "inviscidupwindflux_b" in line:
126  inSubroutine = True
127 
128  if inSubroutine and "use flowutils_b, only : etot" in line:
129  line = line.strip("\n") + ", etot_b\n"
130 
131  if patt_subend.match(line):
132  inSubroutine = False
133 
134  # write the modified line to new file
135  file_object_mod.write(line)
136 
137  # close the files
138  file_object_ori.close()
139  file_object_mod.close()
140 
141  # success message
142  print(" Modified file saved", file_object_mod.name)