ADflow  v1.0
ADflow is a finite volume RANS solver tailored for gradient-based aerodynamic design optimization.
autoEditForward.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 = "_d.f90"
15 
16 DIR_ORI = sys.argv[1]
17 DIR_MOD = sys.argv[2]
18 
19 # Specify the list of LINE ID's to find, what to replace and with what
20 patt_modules = re.compile(r"(\s*use\s*\w*)(_d)\s*")
21 patt_module = re.compile(r"\s*module\s\w*")
22 patt_module_start = re.compile("(\s*module\s)(\w*)(_d)\s*")
23 patt_module_end = re.compile("(\s*end module\s)(\w*)(_d)\s*")
24 patt_subroutine = re.compile(r"\s*subroutine\s\w*")
25 patt_function = re.compile(r"\s*function\s\w*")
26 
27 patt_subend = re.compile(r"\s*end\s*subroutine")
28 patt_funcend = re.compile(r"\s*end\s*function\n")
29 
30 print("Directory of input source files :", DIR_ORI)
31 print("Directory of output source files :", DIR_MOD)
32 
33 useful_modules = [
34  "bcroutines_d",
35  "turbbcroutines_d",
36  "utils_d",
37  "flowutils_d",
38  "walldistance_d",
39  "bcpointers_d",
40  "initializeflow_d",
41  "turbutils_d",
42  "sa_d",
43  "fluxes_d",
44  "solverutils_d",
45  "residuals_d",
46  "surfaceintegrations_d",
47 ]
48 modSubToKeep = []
49 
50 for f in os.listdir(DIR_ORI):
51  if f.endswith(EXT):
52  # open original file in read mode
53  file_object_ori = open(os.path.join(DIR_ORI, f), "r")
54  print("\nParsing input file", file_object_ori.name)
55 
56  # read to whole file to string and reposition the pointer
57  # at the first byte for future reading
58  all_src = file_object_ori.read()
59  file_object_ori.seek(0)
60 
61  # First we want to determine if it is a 'useful' module or a
62  # 'useless' module. A useful module is one that has
63  # subroutines in it.
64  isModule = False
65  hasSubroutine = False
66  for line in file_object_ori:
67  line = line.lower()
68  if patt_module.match(line):
69  isModule = True
70  if patt_subroutine.match(line):
71  hasSubroutine = True
72  if patt_function.match(line):
73  hasSubroutine = True
74 
75  # If we have a module, close the input and cycle to next file.
76  if isModule and not hasSubroutine:
77  file_object_ori.close()
78  continue
79  elif isModule and hasSubroutine:
80  f = f.replace("_d", "_d")
81 
82  # open modified file in write mode
83  file_object_mod = open(os.path.join(DIR_MOD, f), "w")
84 
85  # Go back to the beginning
86  file_object_ori.seek(0)
87  inSubroutine = False
88 
89  for line in file_object_ori:
90  # Just deal with lower case string
91  line = line.lower()
92 
93  # Replace _cb on calls
94  if "_cd" in line:
95  line = line.replace("_cd", "")
96 
97  # Replace _d modules with normal -- except for the useful
98  # ones.
99  m = patt_modules.match(line)
100  if m:
101  found = False
102  for m in useful_modules:
103  if m in line:
104  found = True
105  if found:
106  line = line.replace("_d", "_d", 1)
107  else:
108  line = line.replace("_d", "")
109 
110  # # See if we need to modify the line with changing the
111  # # module names
112  # m = patt_module_start.match(line)
113  # if m:
114  # line = 'module %s_d2\n'%m.group(2)
115 
116  # m = patt_module_end.match(line)
117  # if m:
118  # line = 'end module %s_d2\n'%m.group(2)
119 
120  # Tapenade misses one function in inviscidupwindflux_d and we need to add it manually
121  if patt_subroutine.match(line) and "inviscidupwindflux_d" in line:
122  inSubroutine = True
123 
124  # If within the subroutine we just search for a very specific string append
125  if inSubroutine and "use flowutils_d, only : etot" in line:
126  line = line.strip("\n") + ", etot_d\n"
127 
128  if patt_subend.match(line):
129  inSubroutine = False
130 
131  file_object_mod.write(line)
132 
133  # close the files
134  file_object_ori.close()
135  file_object_mod.close()
136 
137  # success message
138  print(" Modified file saved", file_object_mod.name)