ADflow  v1.0
ADflow is a finite volume RANS solver tailored for gradient-based aerodynamic design optimization.
pyf_preprocessor.py
Go to the documentation of this file.
1 import sys
2 
3 header_str = """ pyf_processor.py is used to automatically process a
4 particular form of pyf file that allows the specification of both the
5 real and complex wrapping functions in one place. This is eliminates
6 duplication in the pyf files.
7 
8 pyf_processor.py is called in the following manner:
9 
10 python pyf_processor.py <real|complex> <pyf_file>
11 
12 The resulting "clean" pyf file is written to pyf_file.autogen. This
13 .autogen file is what should be passed to f2py to generate the actual
14 wrapper.
15 """
16 
17 if len(sys.argv) != 3:
18  print(header_str)
19  sys.exit(1)
20 # end if
21 
22 if sys.argv[1] in ["real", "complex"]:
23  mode = sys.argv[1]
24 else:
25  print(header_str)
26  sys.exit(1)
27 # end if
28 
29 try:
30  f = open(sys.argv[2], "r")
31  orig_lines = f.readlines()
32 except OSError:
33  print("Error opening/reading pyf file!")
34  sys.exit(1)
35 # end try
36 
37 # Open the new pyffile.
38 g = open(sys.argv[2] + ".autogen", "w")
39 
40 # Write the warning header
41 g.write("!" + "#" * 78 + "!" + "\n")
42 g.write("!" + " " * 27 + "DO NOT MODIFY THIS FILE!" + " " * 27 + "!" + "\n")
43 g.write("!" + " " * 27 + "MODIFY adflow.pyf INSTEAD!" + " " * 27 + "!" + "\n")
44 g.write("!" + "#" * 78 + "!" + "\n")
45 
46 # Start going through the lines:
47 cur_mode = "both"
48 for i in range(len(orig_lines)):
49  if "#ifdef USE_COMPLEX" in orig_lines[i]:
50  cur_mode = "complex"
51  elif "#ifndef USE_COMPLEX" in orig_lines[i]:
52  cur_mode = "real"
53  elif "#else" in orig_lines[i]:
54  # Flip the mode:
55  if cur_mode == "complex":
56  cur_mode = "real"
57  elif cur_mode == "real":
58  cur_mode = "complex"
59  else:
60  print("Error occured. Mismatched #else statement")
61  sys.exit(1)
62  # end if
63  elif "#endif" in orig_lines[i]:
64  cur_mode = "both"
65  else:
66  # We have a normal line to write:
67  if cur_mode == "both" or cur_mode == mode:
68  # We now have to check for real/integer types and process:
69  if "kind=inttype" in orig_lines[i]:
70  orig_lines[i] = orig_lines[i].replace("kind=inttype", "kind=4")
71 
72  if "real(kind=realtype)" in orig_lines[i]:
73  if mode == "real":
74  orig_lines[i] = orig_lines[i].replace("kind=realtype", "kind=8")
75  else:
76  orig_lines[i] = orig_lines[i].replace("real(kind=realtype)", "complex(kind=8)")
77 
78  if "real(kind=alwaysrealtype)" in orig_lines[i]:
79  orig_lines[i] = orig_lines[i].replace("kind=alwaysrealtype", "kind=8")
80 
81  # end if
82  g.write(orig_lines[i])
83  # end if
84  # end if
85 # end for
86 
87 g.close()