Skip to content

Commit

Permalink
Add composite DVs (#119)
Browse files Browse the repository at this point in the history
* initial merge from old branch

* added some error checking

* renamed variables

* scale the scaling

* refactored slightly

* revert complex changes since they are incompatible with DVGeoMulti

* added docstrings and warning

* renamed compName to useCompositeNames

* flake8 fix

* renamed function to addCompositeDV, added docstring

* version bump
  • Loading branch information
ewu63 committed Mar 13, 2022
1 parent 80cf705 commit 740a4c0
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 25 deletions.
2 changes: 1 addition & 1 deletion pygeo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.9.0"
__version__ = "1.10.0"

from .pyNetwork import pyNetwork
from .pyGeo import pyGeo
Expand Down
6 changes: 3 additions & 3 deletions pygeo/constraints/areaConstraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ def getVarNames(self):
variables, but some constraints may extend this to include other variables.
"""
if self.DVGeo1 is not None:
varnamelist = self.DVGeo1.getVarNames()
varnamelist = self.DVGeo1.getVarNames(pyOptSparse=True)
if self.DVGeo2 is not None:
varnamelist.extend(self.DVGeo2.getVarNames())
varnamelist.extend(self.DVGeo2.getVarNames(pyOptSparse=True))
else:
varnamelist = self.DVGeo2.getVarNames()
varnamelist = self.DVGeo2.getVarNames(pyOptSparse=True)

return varnamelist

Expand Down
29 changes: 28 additions & 1 deletion pygeo/constraints/baseConstraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Imports
# ======================================================================
from abc import ABC, abstractmethod
from collections import OrderedDict
import numpy as np
from baseclasses.utils import Error

Expand Down Expand Up @@ -56,7 +57,7 @@ def getVarNames(self):
return the var names relevant to this constraint. By default, this is the DVGeo
variables, but some constraints may extend this to include other variables.
"""
return self.DVGeo.getVarNames()
return self.DVGeo.getVarNames(pyOptSparse=True)

def addConstraintsPyOpt(self, optProb, exclude_wrt=None):
"""
Expand Down Expand Up @@ -256,6 +257,32 @@ def _finalize(self):
# with-respect-to are just the keys of the jacobian
self.wrt = list(self.jac.keys())

# now map the jac to composite domain:
# we assume jac is always only wrt "local" DVs
if self.DVGeo.useComposite:
nDV = self.DVGeo.getNDV()
# for the jac, we need to "pad" the rest of the matrix with zero, then perform mat-mat product
newJac = np.zeros((self.ncon, nDV))
for i in range(self.ncon):
temp_dict = {}
# all_DVs just contains all the DVs so we can loop over them easily
all_DVs = OrderedDict({})
all_DVs.update(self.DVGeo.DV_listGlobal)
all_DVs.update(self.DVGeo.DV_listLocal)
all_DVs.update(self.DVGeo.DV_listSectionLocal)
all_DVs.update(self.DVGeo.DV_listSpanwiseLocal)

for dv in all_DVs.keys():
if dv in self.wrt:
temp_dict[dv] = self.jac[dv][i, :].flatten()
else:
temp_dict[dv] = np.zeros(all_DVs[dv].nVal)
newJac[i, :] = self.DVGeo.convertDictToSensitivity(temp_dict)
# now multiply by the mapping
newJac = newJac @ self.DVGeo.DVComposite.u
self.jac = {self.DVGeo.DVComposite.name: newJac}
self.wrt = [self.DVGeo.DVComposite.name]

def writeTecplot(self, handle):
"""
Write the visualization of this set of lete constraints
Expand Down
4 changes: 2 additions & 2 deletions pygeo/constraints/thicknessConstraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self, name, coords, lower, upper, scaled, scale, DVGeo, addToPyOpt,
# Now get the reference lengths
self.D0 = np.zeros(self.nCon)
for i in range(self.nCon):
self.D0[i] = np.linalg.norm(self.coords[2 * i] - self.coords[2 * i + 1])
self.D0[i] = geo_utils.norm.euclideanNorm(self.coords[2 * i] - self.coords[2 * i + 1])

def evalFunctions(self, funcs, config):
"""
Expand All @@ -42,7 +42,7 @@ def evalFunctions(self, funcs, config):
self.coords = self.DVGeo.update(self.name, config=config)
D = np.zeros(self.nCon)
for i in range(self.nCon):
D[i] = np.linalg.norm(self.coords[2 * i] - self.coords[2 * i + 1])
D[i] = geo_utils.norm.euclideanNorm(self.coords[2 * i] - self.coords[2 * i + 1])
if self.scaled:
D[i] /= self.D0[i]
funcs[self.name] = D
Expand Down
4 changes: 3 additions & 1 deletion pygeo/geo_utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ def convertTo1D(value, dim1):
if temp.shape[0] == dim1:
return value
else:
raise ValueError("The size of the 1D array was the incorrect shape")
raise ValueError(
"The size of the 1D array was the incorrect shape! " + f"Expected {dim1} but got {temp.size}"
)


def convertTo2D(value, dim1, dim2):
Expand Down
6 changes: 1 addition & 5 deletions pygeo/geo_utils/norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ def euclideanNorm(inVec):
CS derivatives.
"""
inVec = np.array(inVec)
temp = 0.0
for i in range(inVec.shape[0]):
temp += inVec[i] ** 2

return np.sqrt(temp)
return np.sqrt(inVec.dot(inVec))


def cross_b(a, b, crossb):
Expand Down

0 comments on commit 740a4c0

Please sign in to comment.