From d3c2c2c0e869dba8bc690bc9bd68c9e16b100cbe Mon Sep 17 00:00:00 2001 From: Neil Wu Date: Tue, 7 Apr 2020 19:12:41 -0400 Subject: [PATCH] Python 3 updates (#2) * py3 syntax fixes * removed relative imports * removed future imports --- multiPoint.py | 29 ++++++++++++++--------------- multiPointSparse.py | 1 - testMP.py | 14 +++++++------- testMPSparse.py | 20 ++++++++++---------- 4 files changed, 31 insertions(+), 33 deletions(-) diff --git a/multiPoint.py b/multiPoint.py index 868500f..d3adce4 100644 --- a/multiPoint.py +++ b/multiPoint.py @@ -1,4 +1,3 @@ -from __future__ import print_function #!/usr/bin/python """ multiPoint.py -- A python utility for aiding complex multi-point optimizations @@ -217,7 +216,7 @@ def createCommunicators(self): # end if cumSets = numpy.zeros(self.setCount+1,'intc') - for i in xrange(self.setCount): + for i in range(self.setCount): cumSets[i+1] = cumSets[i] + setSizes[i] # end for @@ -286,7 +285,7 @@ def createDirectories(self, root_dir): pt_dirs = {} for key in self.pSet.keys(): pt_dirs[key] = [] - for i in xrange(self.pSet[key].nMembers): + for i in range(self.pSet[key].nMembers): dir_name = root_dir + '/%s_%d'%(self.pSet[key].setName,i) pt_dirs[key].append(dir_name) @@ -420,7 +419,7 @@ def fun_obj(self, x): len(res[func]))) # end if - for i in xrange(self.pSet[key].nMembers): + for i in range(self.pSet[key].nMembers): if self.pSet[key].groupID == i and \ self.pSet[key].comm.rank == 0: val[i] = self.pSet[key].gcomm.bcast( @@ -538,7 +537,7 @@ def sens(self, x, f_obj, f_con): res[func].shape[1])) # end if - for i in xrange(self.pSet[key].nMembers): + for i in range(self.pSet[key].nMembers): if self.pSet[key].groupID == i and \ self.pSet[key].comm.rank == 0: val[i] = self.pSet[key].gcomm.bcast( @@ -608,7 +607,7 @@ def sens(self, x, f_obj, f_con): iCount = 0 for key in self.functionals: if key != 'fail': - for i in xrange(len(self.functionals[key])): + for i in range(len(self.functionals[key])): if numpy.mod(iCount, self.gcomm.size) == self.gcomm.rank: refVal = self.functionals[key][i] self.functionals[key][i] += 1e-40j @@ -621,7 +620,7 @@ def sens(self, x, f_obj, f_con): self.functionals[key][i] = refVal g_obj += d_obj_df * derivatives[key][i, :] - for j in xrange(len(d_con_df)): + for j in range(len(d_con_df)): g_con[j, :] += d_con_df[j] * derivatives[key][i, :] # end for # end if @@ -701,7 +700,7 @@ def _createCommunicators(self): # Create a cumulative size array cumGroups = numpy.zeros(self.nMembers + 1,'intc') - for i in xrange(self.nMembers): + for i in range(self.nMembers): cumGroups[i+1] = cumGroups[i] + self.memberSizes[i] # end for @@ -709,7 +708,7 @@ def _createCommunicators(self): # Determine the member_key (m_key) for each processor m_key = None - for i in xrange(self.nMembers): + for i in range(self.nMembers): if self.gcomm.rank >= cumGroups[i] and \ self.gcomm.rank < cumGroups[i+1]: m_key = i @@ -717,11 +716,11 @@ def _createCommunicators(self): # end for #if m_key is None: - #print '[%d] Split is Screwed!'%(MPI.COMM_WORLD.rank) - #print '[%d] cumGroups:'%(MPI.COMM_WORLD.rank),cumGroups - #print '[%d] nMmembers:'%(MPI.COMM_WORLD.rank),self.nMembers - #print '[%d] Rank :'%(MPI.COMM_WORLD.rank),self.gcomm.rank - #print '[%d] Size :'%(MPI.COMM_WORLD.rank),self.gcomm.size + #print('[%d] Split is Screwed!'%(MPI.COMM_WORLD.rank)) + #print('[%d] cumGroups:'%(MPI.COMM_WORLD.rank),cumGroups) + #print('[%d] nMmembers:'%(MPI.COMM_WORLD.rank),self.nMembers) + #print('[%d] Rank :'%(MPI.COMM_WORLD.rank),self.gcomm.rank) + #print('[%d] Size :'%(MPI.COMM_WORLD.rank),self.gcomm.size) self.comm = self.gcomm.Split(m_key) @@ -738,5 +737,5 @@ def _createCommunicators(self): # mutliPoint Test #============================================================================== if __name__ == '__main__': - import testMP + from . import testMP diff --git a/multiPointSparse.py b/multiPointSparse.py index 114b138..c502bf5 100644 --- a/multiPointSparse.py +++ b/multiPointSparse.py @@ -14,7 +14,6 @@ ------- v. 1.0 - First implementation """ -from __future__ import print_function # ============================================================================= # Imports # ============================================================================= diff --git a/testMP.py b/testMP.py index 1f37e1f..7d5a793 100644 --- a/testMP.py +++ b/testMP.py @@ -18,7 +18,7 @@ # Extension modules # ============================================================================= from mdo_import_helper import MPI, mpiPrint -import multiPoint +from . import multiPoint # First create multipoint object on the communicator that will contain # all multipoint processes. This is often MPI.COMM_WORLD. @@ -232,14 +232,14 @@ def constraints(funcs, printOK): obj_value, con_values, fail = MP.fun_obj(x) if MPI.COMM_WORLD.rank == 0: - print 'obj_value:',obj_value - print 'con_values:',con_values - print 'Fail Flag:',fail + print('obj_value:',obj_value) + print('con_values:',con_values) + print('Fail Flag:',fail) g_obj, g_con, fail = MP.sens(x, obj_value, con_values) if MPI.COMM_WORLD.rank == 0: - print 'g_obj',g_obj - print 'g_con',g_con - print 'Fail Flag',fail + print('g_obj',g_obj) + print('g_con',g_con) + print('Fail Flag',fail) diff --git a/testMPSparse.py b/testMPSparse.py index 926b96c..e648bfe 100644 --- a/testMPSparse.py +++ b/testMPSparse.py @@ -16,7 +16,7 @@ from pywarp import * from pygeo import * from pyspline import * -import multiPointSparse +from . import multiPointSparse from pyoptsparse import Optimization, pySNOPT # ================================================================ @@ -162,7 +162,7 @@ def twist(val,geo): # Set all the twist values - for i in xrange(nTwist): + for i in range(nTwist): geo.rot_z[0].coef[i] = val[i] return @@ -255,9 +255,9 @@ def twist(val,geo): # Note we CAN add "constraints" to control points that may not be added # as above. -for ivol in xrange(FFD.nVol): +for ivol in range(FFD.nVol): sizes = FFD.topo.l_index[ivol].shape - for k in xrange(sizes[2]): # Go out the 'z' or 'k' direction + for k in range(sizes[2]): # Go out the 'z' or 'k' direction up_ind.append(FFD.topo.l_index[ivol][0,-1,k]) # Le control points low_ind.append(FFD.topo.l_index[ivol][0,0,k]) @@ -274,8 +274,8 @@ def twist(val,geo): def cruiseObj(x): if MPI.COMM_WORLD.rank == 0: - print 'Fun Obj:' - print x + print('Fun Obj:') + print(x) # Set geometric design variables from optimizer DVGeo.setValues(x, scaled=True) @@ -285,7 +285,7 @@ def cruiseObj(x): funcs = {} funcs['fail'] = False - for i in xrange(nFlowCases): + for i in range(nFlowCases): if i%nGroup == ptID: fc = flowCases[i] aeroProblems[fc]._flows.alpha = x['alpha_'+fc] @@ -310,7 +310,7 @@ def cruiseSens(x, fobj, fcon): fail = 0 funcSens = {} - for i in xrange(nFlowCases): + for i in range(nFlowCases): if i%nGroup == ptID: fc = flowCases[i] # --------- cl Adjoint ----------- @@ -336,7 +336,7 @@ def objCon(funcs): # Assemble the objective and any additional constraints: funcs['cd'] = 0.0 - for i in xrange(nFlowCases): + for i in range(nFlowCases): fc = flowCases[i] funcs['cd'] += funcs['cd_'+fc]/nFlowCases @@ -373,7 +373,7 @@ def objCon(funcs): # Check opt problem: if MPI.COMM_WORLD.rank == 0: - print opt_prob + print(opt_prob) opt_prob.printSparsity() # The MP object needs the 'obj' and 'sens' function for each proc set,