Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3 updates #2

Merged
merged 4 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions multiPoint.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
#!/usr/bin/python
"""
multiPoint.py -- A python utility for aiding complex multi-point optimizations
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -701,27 +700,27 @@ 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


# 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
# end for
# 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)
Expand All @@ -738,5 +737,5 @@ def _createCommunicators(self):
# mutliPoint Test
#==============================================================================
if __name__ == '__main__':
import testMP
from . import testMP

1 change: 0 additions & 1 deletion multiPointSparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
-------
v. 1.0 - First implementation
"""
from __future__ import print_function
# =============================================================================
# Imports
# =============================================================================
Expand Down
14 changes: 7 additions & 7 deletions testMP.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

20 changes: 10 additions & 10 deletions testMPSparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from pywarp import *
from pygeo import *
from pyspline import *
import multiPointSparse
from . import multiPointSparse
from pyoptsparse import Optimization, pySNOPT

# ================================================================
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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])

Expand All @@ -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)
Expand All @@ -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]
Expand All @@ -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 -----------
Expand All @@ -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

Expand Down Expand Up @@ -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,
Expand Down