Skip to content

Commit

Permalink
Revert "Add print statements to test_result"
Browse files Browse the repository at this point in the history
This reverts commit 63cf257.
  • Loading branch information
bburke38 committed Sep 16, 2024
1 parent 63cf257 commit 6b1dc3e
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 266 deletions.
Empty file.
Empty file.
Empty file.
1 change: 0 additions & 1 deletion funtofem/driver/oneway_struct_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,6 @@ def _transfer_fixed_aero_loads(self):
# the mesh for the new structural shape
body.transfer_loads(scenario)
body.transfer_heat_flux(scenario)

return

def solve_forward(self):
Expand Down
322 changes: 104 additions & 218 deletions funtofem/interface/tacs_interface.py

Large diffs are not rendered by default.

14 changes: 3 additions & 11 deletions funtofem/model/composite_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ def __init__(
self.derivatives = {}
self.df_dgi = None

@property
def optim_derivatives(self):
optim_derivs = {var: self.derivatives[var] for var in self.derivatives if not(var.state)}
return optim_derivs

@classmethod
def external(cls, name, optim=False, plot_name=None):
return cls(
Expand All @@ -99,24 +94,21 @@ def vars_only(self) -> bool:
@property
def sparse_gradient(self):
"""used for adjacency constraints, vars only functions"""
self.evaluate_gradient()
np_array = np.array(
[self.derivatives[var] for var in self.optim_derivatives]
)
np_array = np.array([self.derivatives[var] for var in self.derivatives])
# return csr_matrix(np_array, shape=(1,np_array.shape[0]))
nvars = np_array.shape[0]
cols = np.array(
[
ivar
for ivar, var in enumerate(self.optim_derivatives)
for ivar, var in enumerate(self.derivatives)
if self.derivatives[var] != 0.0
]
)
rows = np.array([0 for _ in range(cols.shape[0])])
vals = np.array(
[
self.derivatives[var]
for ivar, var in enumerate(self.optim_derivatives)
for ivar, var in enumerate(self.derivatives)
if self.derivatives[var] != 0.0
]
)
Expand Down
5 changes: 1 addition & 4 deletions funtofem/model/funtofem_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def _send_flow_variables(self, base):
self.flow.set_variables(active_shape_vars, active_aero_vars)
return

def get_variables(self, names=None, all=False, optim=False):
def get_variables(self, names=None, all=False):
"""
Get all the coupled and uncoupled variable objects for the entire model.
Coupled variables only appear once.
Expand All @@ -272,9 +272,6 @@ def get_variables(self, names=None, all=False, optim=False):
var_list: list of variable objects
"""

if optim: # return all active and non-state variables
return [var for var in self.get_variables() if not (var.state)]

if names is None:
dv = []
for scenario in self.scenarios:
Expand Down
9 changes: 0 additions & 9 deletions funtofem/model/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def __init__(
active=True,
coupled=False,
id=0,
state=False,
):
"""
Variable object for FUNtoFEM
Expand All @@ -71,8 +70,6 @@ def __init__(
whether or not the design variable is coupled
id: int
id number of the design variable
state: bool
whether this variable is used as a state variable (for TACS only right now)
Examples
--------
Expand All @@ -85,7 +82,6 @@ def __init__(
self.upper = upper
self.active = active
self.coupled = coupled
self.state = state
self.id = id
self.scale = scale
self.analysis_type = analysis_type
Expand All @@ -100,7 +96,6 @@ def set_bounds(
scale=None,
active=None,
coupled=None,
state=None,
):
"""
Update the one or more of the attributes of the design variable
Expand All @@ -119,8 +114,6 @@ def set_bounds(
whether or not the design variable is active
coupled: bool
whether or not the design variable is coupled
state: bool
whether this variable is used as a state variable (for TACS only right now)
"""

if value is not None:
Expand All @@ -135,8 +128,6 @@ def set_bounds(
self.active = active
if coupled is not None:
self.coupled = coupled
if state is not None:
self.state = state

# return the object for method cascading
return self
Expand Down
41 changes: 18 additions & 23 deletions funtofem/optimization/optimization_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def __init__(

# add all variables (for off-scenario variables to derivatives dict for each function) to analysis functions
for func in self.model.get_functions():
for var in self.model.get_variables(optim=True):
for var in self.model.get_variables():
func.derivatives[var] = 0.0

# initialize funcs, sens in case of failure on first design iteration of hot start
Expand All @@ -143,7 +143,7 @@ def __init__(
self._sens = {}
for func in self.model.get_functions(optim=True):
self._sens[func.full_name] = {}
for var in self.model.get_variables(optim=True):
for var in self.model.get_variables():
if self.sparse:
self._sens[func.full_name][self.SPARSE_VARS_GROUP] = None
else:
Expand Down Expand Up @@ -176,7 +176,7 @@ def _gatekeeper(self, x_dict):
if self.sparse: # all vars in same group
regular_dict = {
var.name: float(x_dict[self.SPARSE_VARS_GROUP][ivar])
for ivar, var in enumerate(self.model.get_variables(optim=True))
for ivar, var in enumerate(self.model.get_variables())
}
else:
regular_dict = {key: float(x_dict[key]) for key in x_dict}
Expand Down Expand Up @@ -240,14 +240,13 @@ def _gatekeeper(self, x_dict):
for func in self.model.get_functions(optim=True)
if func._plot
}
if self.driver.solvers.flow is not None:
forward_res = self.driver.solvers.flow.get_forward_residual()
forward_steps = self.driver.solvers.flow._last_forward_step
adjoint_res = self.driver.solvers.flow.get_adjoint_residual()
adjoint_steps = self.driver.solvers.flow._last_adjoint_step
self._design_hdl.write(
f"Forward resid {forward_res:2.5e} in {forward_steps} steps, Adjoint resid {adjoint_res:2.5e} in {adjoint_steps} steps and {_total_time:.4f} {time_units}\n"
)
forward_res = self.driver.solvers.flow.get_forward_residual()
forward_steps = self.driver.solvers.flow._last_forward_step
adjoint_res = self.driver.solvers.flow.get_adjoint_residual()
adjoint_steps = self.driver.solvers.flow._last_adjoint_step
self._design_hdl.write(
f"Forward resid {forward_res:2.5e} in {forward_steps} steps, Adjoint resid {adjoint_res:2.5e} in {adjoint_steps} steps and {_total_time:.4f} {time_units}\n"
)
self._design_hdl.write(f"Functions = {plot_funcs}\n")
self._design_hdl.flush()

Expand Down Expand Up @@ -285,7 +284,7 @@ def _gatekeeper(self, x_dict):
if not func._plot:
continue
self._func_history[func.plot_name] += [func.value.real]
#self._plot_history()
self._plot_history()
return fail

def _run_complete_analysis(self):
Expand All @@ -295,12 +294,12 @@ def _run_complete_analysis(self):

# update the model design variables
if self.sparse:
variables = self.model.get_variables(optim=True)
variables = self.model.get_variables()
for ivar, val in enumerate(self._x_dict[self.SPARSE_VARS_GROUP]):
var = variables[ivar]
var.value = float(val)
else:
for var in self.model.get_variables(optim=True):
for var in self.model.get_variables():
for var_key in self._x_dict:
if var.full_name == var_key:
# assumes here that only pyoptsparse single variables (no var groups are made)
Expand All @@ -318,8 +317,6 @@ def _run_complete_analysis(self):
self._sens = {}
# get only functions with optim=True, set with func.optimize() method (can method cascade it)
for func in self.model.get_functions(optim=True):
if func.vars_only and self.sparse:
continue
self._funcs[func.full_name] = func.value.real
self._sens[func.full_name] = {}
# if self.sparse and isinstance(func, CompositeFunction) and func.vars_only:
Expand All @@ -328,13 +325,10 @@ def _run_complete_analysis(self):
# ) # linear constraints define their jacobians up front
if self.sparse:
self._sens[func.full_name][self.SPARSE_VARS_GROUP] = np.array(
[
func.derivatives[var].real
for var in self.model.get_variables(optim=True)
]
[func.derivatives[var].real for var in func.derivatives]
)
else:
for var in self.model.get_variables(optim=True):
for var in self.model.get_variables():
self._sens[func.full_name][var.full_name] = (
func.get_gradient_component(var).real
)
Expand All @@ -345,7 +339,8 @@ def register_to_problem(self, opt_problem):
add funtofem model variables and functions to a pyoptsparse optimization problem
"""
if self.sparse:
variables = self.model.get_variables(optim=True)
variables = self.model.get_variables()
values = np.array([var.value for var in variables])
opt_problem.addVarGroup(
self.SPARSE_VARS_GROUP,
len(variables),
Expand All @@ -356,7 +351,7 @@ def register_to_problem(self, opt_problem):
scale=np.array([var.scale for var in variables]),
)
else:
for var in self.model.get_variables(optim=True):
for var in self.model.get_variables():
opt_problem.addVar(
var.full_name,
lower=var.lower,
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 6b1dc3e

Please sign in to comment.