Skip to content

Commit e27908e

Browse files
authored
Merge pull request #2640 from shbhmexe/bugfix/su2py-correctness-tools
fix: Python 3 correctness and PEP 8 compliance improvements
2 parents dac92cc + fb220e5 commit e27908e

File tree

7 files changed

+35
-39
lines changed

7 files changed

+35
-39
lines changed

SU2_PY/SU2/io/data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def save_data(
196196
core_name=core_name,
197197
)
198198
# check for keys not in new data
199-
for key, value in data_dict_old.iteritems():
199+
for key, value in data_dict_old.items():
200200
if not (key in data_dict):
201201
data_dict[key] = value
202202
#: for each dict item
@@ -418,7 +418,7 @@ def append_nestdict(base_dict, add_dict):
418418
for key in add_dict.keys():
419419

420420
# ensure base_dict key exists and is a list
421-
if not base_dict.has_key(key):
421+
if key not in base_dict:
422422
if isinstance(add_dict[key], dict):
423423
base_dict[key] = {}
424424
else:

SU2_PY/SU2/io/state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def State_Factory(state=None, config=None):
111111
config = state
112112
state = None
113113

114-
if not state is None:
114+
if state is not None:
115115
assert isinstance(state, State), "input is must be a state instance"
116116
return state
117117

SU2_PY/SU2/io/tools.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,17 @@ def read_gradients(Grad_filename, scale=1.0):
4545
"""
4646

4747
# open file and skip first line
48-
gradfile = open(Grad_filename)
49-
gradfile.readline()
50-
51-
# read values
52-
grad_vals = []
53-
for line in gradfile:
54-
line = line.strip()
55-
if len(line) == 0:
56-
break
57-
grad_vals.append(float(line) * scale)
58-
#: for each line
59-
48+
with open(Grad_filename) as gradfile:
49+
gradfile.readline()
50+
51+
# read values
52+
grad_vals = []
53+
for line in gradfile:
54+
line = line.strip()
55+
if len(line) == 0:
56+
break
57+
grad_vals.append(float(line) * scale)
58+
# done
6059
return grad_vals
6160

6261

@@ -387,7 +386,7 @@ def read_aerodynamics(
387386
Func_Values[key] = history_data["TAVG_" + key][-1]
388387
else:
389388
# in steady cases take only last value.
390-
for key, value in Func_Values.iteritems():
389+
for key, value in Func_Values.items():
391390
if not history_data.get(key):
392391
raise KeyError(
393392
"Key "
@@ -641,7 +640,7 @@ def get_dvID(kindName):
641640
try:
642641
return id_map[kindName]
643642
except KeyError:
644-
raise Exception("Unrecognized Design Variable Name: %s", kindName)
643+
raise Exception("Unrecognized Design Variable Name: %s" % kindName)
645644

646645

647646
#: def get_dvID()
@@ -652,7 +651,10 @@ def get_dvID(kindName):
652651
# -------------------------------------------------------------------
653652

654653

655-
def get_gradFileFormat(grad_type, plot_format, kindID, special_cases=[]):
654+
def get_gradFileFormat(grad_type, plot_format, kindID, special_cases=None):
655+
656+
if special_cases is None:
657+
special_cases = []
656658

657659
# start header, build a list of strings and join at the end
658660
header = []
@@ -731,7 +733,7 @@ def get_gradFileFormat(grad_type, plot_format, kindID, special_cases=[]):
731733
write_format.append(r", %s, %s, %s")
732734
elif kindID == "CST":
733735
header.append(r',"Up/Down","Kulfan number", "Total Kulfan numbers"')
734-
write_format.append(r", %s, %s", "%s")
736+
write_format.append(r", %s, %s, %s")
735737
elif kindID == "FAIRING":
736738
header.append(r',"ControlPoint_Index","Theta_Disp","R_Disp"')
737739
write_format.append(r", %s, %s, %s")
@@ -1170,8 +1172,8 @@ def make_link(src, dst):
11701172
os.symlink(src, dst)
11711173

11721174

1173-
def restart2solution(config, state={}):
1174-
"""restart2solution(config,state={})
1175+
def restart2solution(config, state=None):
1176+
"""restart2solution(config, state=None)
11751177
moves restart file to solution file,
11761178
optionally updates state
11771179
direct or adjoint is read from config

SU2_PY/SU2/run/interface.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@
5656
mpi_Command = os.environ["SU2_MPI_COMMAND"]
5757
elif slurm_job:
5858
mpi_Command = "srun -n %i %s"
59-
elif not which("mpirun") is None:
59+
elif which("mpirun") is not None:
6060
mpi_Command = "mpirun -n %i %s"
61-
elif not which("mpiexec") is None:
61+
elif which("mpiexec") is not None:
6262
mpi_Command = "mpiexec -n %i %s"
6363
else:
6464
mpi_Command = ""

SU2_PY/SU2/run/projection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
# ----------------------------------------------------------------------
4242

4343

44-
def projection(config, state={}, step=1e-3):
44+
def projection(config, state=None, step=1e-3):
4545
"""info = SU2.run.projection(config,state,step=1e-3)
4646
4747
Runs an gradient projection with:
@@ -54,7 +54,7 @@ def projection(config, state={}, step=1e-3):
5454
5555
Inputs:
5656
config - an SU2 config
57-
state - only required when using external custom DV
57+
state - accepted for API compatibility; currently unused by this function
5858
step - a float or list of floats for geometry sensitivity
5959
finite difference step
6060

SU2_PY/SU2/util/plot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ def write_plot(filename, plot_format, data_plot, keys_plot=None):
8888
return
8989

9090

91-
def tecplot(filename, data_plot, keys_plot=[]):
91+
def tecplot(filename, data_plot, keys_plot=None):
9292
write_plot(filename, "TECPLOT", data_plot, keys_plot)
9393

9494

95-
def paraview(filename, data_plot, keys_plot=[]):
95+
def paraview(filename, data_plot, keys_plot=None):
9696
write_plot(filename, "CSV", data_plot, keys_plot)

SU2_PY/topology_optimization.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,9 @@ def obj_val(self, x):
120120
# write inputs
121121
self._write_input(x)
122122

123-
# clear previous output and run direct solver
124-
try:
123+
# clear previous output if present and run direct solver
124+
if os.path.exists(self._objValFile):
125125
os.remove(self._objValFile)
126-
except OSError:
127-
pass # Ignore error if file does not exist
128126

129127
try:
130128
sp.call(self._objValCommand, shell=True)
@@ -147,11 +145,9 @@ def obj_val(self, x):
147145
def obj_der(self, x):
148146
# inputs written in obj_val_driver
149147

150-
# clear previous output and run direct solver
151-
try:
148+
# clear previous output if present and run direct solver
149+
if os.path.exists(self._objDerFile):
152150
os.remove(self._objDerFile)
153-
except OSError:
154-
pass # Ignore error if file does not exist
155151
N = x.shape[0]
156152
y = np.ndarray((N,))
157153

@@ -197,11 +193,9 @@ def con_val(self, x):
197193
def con_der(self, x):
198194
# inputs written in obj_val_driver
199195

200-
# clear previous output and run solver
201-
try:
196+
# clear previous output if present and run solver
197+
if os.path.exists(self._conDerFile):
202198
os.remove(self._conDerFile)
203-
except OSError:
204-
pass # Ignore error if file does not exist
205199
N = x.shape[0]
206200
y = np.ndarray((N,))
207201

0 commit comments

Comments
 (0)