def test(self): """ A simple example to compute the resultant force on an aircraft and demonstrate the AddSubtract component """ import numpy as np #from openmdao.api import Problem, Group, IndepVarComp from openconcept.utilities.math.add_subtract_comp import AddSubtractComp from openmdao.utils.assert_utils import assert_rel_error n = 3 p = Problem(model=Group()) ivc = IndepVarComp() #the vector represents forces at 3 analysis points (rows) in 2 dimensional plane (cols) ivc.add_output(name='thrust', shape=(n, 2), units='kN') ivc.add_output(name='drag', shape=(n, 2), units='kN') ivc.add_output(name='lift', shape=(n, 2), units='kN') ivc.add_output(name='weight', shape=(n, 2), units='kN') p.model.add_subsystem( name='ivc', subsys=ivc, promotes_outputs=['thrust', 'drag', 'lift', 'weight']) #construct an adder/subtracter here. create a relationship through the add_equation method adder = AddSubtractComp() adder.add_equation('total_force', input_names=['thrust', 'drag', 'lift', 'weight'], vec_size=n, length=2, scaling_factors=[1, -1, 1, -1], units='kN') #note the scaling factors. we assume all forces are positive sign upstream p.model.add_subsystem(name='totalforcecomp', subsys=adder) p.model.connect('thrust', 'totalforcecomp.thrust') p.model.connect('drag', 'totalforcecomp.drag') p.model.connect('lift', 'totalforcecomp.lift') p.model.connect('weight', 'totalforcecomp.weight') p.setup() #set thrust to exceed drag, weight to equal lift for this scenario p['thrust'][:, 0] = [500, 600, 700] p['drag'][:, 0] = [400, 400, 400] p['weight'][:, 1] = [1000, 1001, 1002] p['lift'][:, 1] = [1000, 1000, 1000] p.run_model() # print(p.get_val('totalforcecomp.total_force', units='kN')) # Verify the results expected_i = np.array([[100, 200, 300], [0, -1, -2]]).T assert_rel_error(self, p.get_val('totalforcecomp.total_force', units='kN'), expected_i)
def promote_add(self, sources, prom_name, promoted_sources=[], factors=None, vec_size=1, units=None, length=1, val=1.0, res_units=None, desc='', lower=None, upper=None, ref=1.0, ref0=0.0, res_ref=None): """Helper function called during setup""" add_index = self._n_auto_comps self._n_auto_comps += 1 adder = AddSubtractComp() n_inputs = len(sources) + len(promoted_sources) if factors is None: factors = np.ones(n_inputs) adder.add_equation( output_name=prom_name, input_names=['_temp' + str(i) for i in range(n_inputs)], scaling_factors=factors, vec_size=vec_size, units=units, length=length, val=val, res_units=res_units, desc=desc, lower=lower, upper=upper, ref=ref, ref0=ref0, res_ref=res_ref) adder_name = 'add' + str(add_index) prom_in = [] for i, promoted_source in enumerate(promoted_sources): prom_in.append(('_temp' + str(i + len(sources)), promoted_source)) self.add_subsystem(adder_name, adder, promotes_inputs=prom_in, promotes_outputs=['*']) for i, source in enumerate(sources): self.connect(source, adder_name + '._temp' + str(i))