def test_options(self): """Verify that the PetscKSP specific options are declared.""" group = Group() group.linear_solver = PetscKSP() assert (group.linear_solver.options['ksp_type'] == 'fgmres')
def __init__(self, nTurbines, direction_id=0, datasize=0, differentiable=True, use_rotor_components=False, nSamples=0, wake_model=floris_wrapper, wake_model_options=None): super(RotorSolveGroup, self).__init__() if wake_model_options is None: wake_model_options = { 'differentiable': differentiable, 'use_rotor_components': use_rotor_components, 'nSamples': nSamples } from openmdao.core.mpi_wrap import MPI # set up iterative solvers epsilon = 1E-6 if MPI: self.ln_solver = PetscKSP() else: self.ln_solver = ScipyGMRES() self.nl_solver = NLGaussSeidel() self.ln_solver.options['atol'] = epsilon self.add('CtCp', CPCT_Interpolate_Gradients_Smooth(nTurbines, direction_id=direction_id, datasize=datasize), promotes=[ 'gen_params:*', 'yaw%i' % direction_id, 'wtVelocity%i' % direction_id, 'Cp_out' ]) # TODO refactor the model component instance self.add('floris', wake_model(nTurbines, direction_id=direction_id, wake_model_options=wake_model_options), promotes=([ 'model_params:*', 'wind_speed', 'axialInduction', 'turbineXw', 'turbineYw', 'rotorDiameter', 'yaw%i' % direction_id, 'hubHeight', 'wtVelocity%i' % direction_id ] if (nSamples == 0) else [ 'model_params:*', 'wind_speed', 'axialInduction', 'turbineXw', 'turbineYw', 'rotorDiameter', 'yaw%i' % direction_id, 'hubHeight', 'wtVelocity%i' % direction_id, 'wsPositionX', 'wsPositionY', 'wsPositionZ', 'wsArray%i' % direction_id ])) self.connect('CtCp.Ct_out', 'floris.Ct')
def test_specify_ksp_type(self): import numpy as np from openmdao.api import Problem, Group, IndepVarComp, NonlinearBlockGS, PetscKSP, \ ExecComp from openmdao.test_suite.components.sellar import SellarDis1withDerivatives, \ SellarDis2withDerivatives prob = Problem() model = prob.model = Group() model.add_subsystem('px', IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) model.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) model.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) model.nonlinear_solver = NonlinearBlockGS() model.linear_solver = PetscKSP() model.linear_solver.options['ksp_type'] = 'gmres' prob.setup() prob.run_model() wrt = ['z'] of = ['obj'] J = prob.compute_totals(of=of, wrt=wrt, return_format='flat_dict') assert_rel_error(self, J['obj', 'z'][0][0], 9.61001056, .00001) assert_rel_error(self, J['obj', 'z'][0][1], 1.78448534, .00001)
def setup(self): self.add_input('a', val=10., units='m') rank = self.comm.rank GLOBAL_SIZE = 15 sizes, offsets = evenly_distrib_idxs(self.comm.size, GLOBAL_SIZE) self.add_output('states', shape=int(sizes[rank])) self.add_output('out_var', shape=1) self.local_size = sizes[rank] self.linear_solver = PetscKSP() self.linear_solver.precon = LinearUserDefined(self.mysolve)
def test_specify_precon_left(self): import numpy as np from openmdao.api import Problem, Group, IndepVarComp, DirectSolver, PetscKSP, \ NewtonSolver, ExecComp from openmdao.test_suite.components.sellar import SellarDis1withDerivatives, \ SellarDis2withDerivatives prob = Problem() model = prob.model = Group() model.add_subsystem('px', IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) model.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) model.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) model.nonlinear_solver = NewtonSolver() model.linear_solver = PetscKSP() model.linear_solver.precon = DirectSolver() model.linear_solver.options['precon_side'] = 'left' model.linear_solver.options['ksp_type'] = 'richardson' prob.setup() prob.run_model() assert_rel_error(self, prob['y1'], 25.58830273, .00001) assert_rel_error(self, prob['y2'], 12.05848819, .00001)
def test_method(self): p = Problem() p.model.add_subsystem('des_vars', IndepVarComp('a', val=10., units='m'), promotes=['*']) p.model.add_subsystem('icomp', DistribStateImplicit(), promotes=['*']) model = p.model model.linear_solver = PetscKSP() model.linear_solver.precon = LinearRunOnce() p.setup(vector_class=PETScVector, mode='rev', check=False) p.run_model() jac = p.compute_totals(of=['out_var'], wrt=['a'], return_format='dict') assert_rel_error(self, 15.0, jac['out_var']['a'][0][0])
def test_feature_rtol(self): prob = Problem() model = prob.model = Group() model.add_subsystem('px', IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) model.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) model.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) model.nonlinear_solver = NonlinearBlockGS() model.linear_solver = PetscKSP() model.linear_solver.options['rtol'] = 1.0e-20 prob.setup() prob.run_model() wrt = ['z'] of = ['obj'] J = prob.compute_totals(of=of, wrt=wrt, return_format='flat_dict') assert_rel_error(self, J['obj', 'z'][0][0], 9.61001055699, .00001) assert_rel_error(self, J['obj', 'z'][0][1], 1.78448533563, .00001)
def test_specify_precon(self): prob = Problem() model = prob.model = Group() model.add_subsystem('px', IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) model.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) model.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) model.nonlinear_solver = NewtonSolver() model.linear_solver = PetscKSP() model.linear_solver.precon = LinearBlockGS() model.linear_solver.precon.options['maxiter'] = 2 prob.setup() prob.run_model() assert_rel_error(self, prob['y1'], 25.58830273, .00001) assert_rel_error(self, prob['y2'], 12.05848819, .00001)
def setup(self): self._mpi_proc_allocator.parallel = self.parallel if self.parallel: self.nonlinear_solver = NewtonSolver() self.linear_solver = PetscKSP() else: self.nonlinear_solver = NonlinearBlockGS() self.linear_solver = LinearBlockGS() self.add_subsystem('C1', ExecComp('z = 1 / 3. * y + x0'), promotes=['x0']) self.add_subsystem('C2', ExecComp('z = 1 / 4. * y + x1'), promotes=['x1']) if self.parallel: self.connect('C1.z', 'C2.y') self.connect('C2.z', 'C1.y') else: self.connect('C1.z', 'C2.y', src_indices=[self.comm.rank]) self.connect('C2.z', 'C1.y', src_indices=[self.comm.rank]) self.parallel = not self.parallel
def test_feature(self): import numpy as np from openmdao.api import Problem, ImplicitComponent, IndepVarComp, LinearRunOnce, PetscKSP, PETScVector, LinearUserDefined from openmdao.utils.array_utils import evenly_distrib_idxs class CustomSolveImplicit(ImplicitComponent): def setup(self): self.add_input('a', val=10., units='m') rank = self.comm.rank GLOBAL_SIZE = 15 sizes, offsets = evenly_distrib_idxs(self.comm.size, GLOBAL_SIZE) self.add_output('states', shape=int(sizes[rank])) self.add_output('out_var', shape=1) self.local_size = sizes[rank] self.linear_solver = PetscKSP() self.linear_solver.precon = LinearUserDefined( solve_function=self.mysolve) def solve_nonlinear(self, i, o): o['states'] = i['a'] local_sum = np.zeros(1) local_sum[0] = np.sum(o['states']) tmp = np.zeros(1) o['out_var'] = tmp[0] def apply_nonlinear(self, i, o, r): r['states'] = o['states'] - i['a'] local_sum = np.zeros(1) local_sum[0] = np.sum(o['states']) global_sum = np.zeros(1) r['out_var'] = o['out_var'] - tmp[0] def apply_linear(self, i, o, d_i, d_o, d_r, mode): if mode == 'fwd': if 'states' in d_o: d_r['states'] += d_o['states'] local_sum = np.array([np.sum(d_o['states'])]) global_sum = np.zeros(1) self.comm.Allreduce(local_sum, global_sum, op=MPI.SUM) d_r['out_var'] -= global_sum if 'out_var' in d_o: d_r['out_var'] += d_o['out_var'] if 'a' in d_i: d_r['states'] -= d_i['a'] elif mode == 'rev': if 'states' in d_o: d_o['states'] += d_r['states'] tmp = np.zeros(1) if self.comm.rank == 0: tmp[0] = d_r['out_var'].copy() self.comm.Bcast(tmp, root=0) d_o['states'] -= tmp if 'out_var' in d_o: d_o['out_var'] += d_r['out_var'] if 'a' in d_i: d_i['a'] -= np.sum(d_r['states']) def mysolve(self, d_outputs, d_residuals, mode): r""" Apply inverse jac product. The model is assumed to be in an unscaled state. If mode is: 'fwd': d_residuals \|-> d_outputs 'rev': d_outputs \|-> d_residuals Parameters ---------- d_outputs : Vector unscaled, dimensional quantities read via d_outputs[key] d_residuals : Vector unscaled, dimensional quantities read via d_residuals[key] mode : str either 'fwd' or 'rev' Returns ------- None or bool or (bool, float, float) The bool is the failure flag; and the two floats are absolute and relative error. """ # Note: we are just preconditioning with Identity as a proof of concept. if mode == 'fwd': d_outputs.set_vec(d_residuals) elif mode == 'rev': d_residuals.set_vec(d_outputs) return False, 0., 0. prob = Problem() prob.model.add_subsystem('des_vars', IndepVarComp('a', val=10., units='m'), promotes=['*']) prob.model.add_subsystem('icomp', CustomSolveImplicit(), promotes=['*']) model = prob.model model.linear_solver = PetscKSP() model.linear_solver.precon = LinearRunOnce() prob.setup(vector_class=PETScVector, mode='rev', check=False) prob.run_model() jac = prob.compute_totals(of=['out_var'], wrt=['a'], return_format='dict') assert_rel_error(self, 15.0, jac['out_var']['a'][0][0])
def setup(self): ap = self.options['ap'] self.solver, self.mesh, self.dvgeo, self.dvcon = self.options[ 'setup_cb'](self.comm) # necessary to get some memory properly allocated #self.solver.getResidual(ap) des_vars, constraints = get_dvs_and_cons(ap, self.dvgeo, self.dvcon) geo_vars, _ = get_dvs_and_cons(geo=self.dvgeo) self.des_var_names = des_vars self.geo_var_names = geo_vars self.constraint_names = constraints des_var_names = [dv[0][0] for dv in des_vars] geo_var_names = [dv[0][0] for dv in geo_vars] if self.options['owns_indeps']: indeps = self.add_subsystem('indeps', IndepVarComp(), promotes=['*']) for (args, kwargs) in des_vars: name = args[0] size = args[1] value = kwargs['value'] if 'units' in kwargs: indeps.add_output(name, value, units=kwargs['units']) else: indeps.add_output(name, value) states = OM_STATES_COMP(ap=ap, dvgeo=self.dvgeo, solver=self.solver, use_OM_KSP=self.options['use_OM_KSP']) self.add_subsystem('states', states, promotes_inputs=des_var_names) # this lets the OpenMDAO KSPsolver converge the adjoint using the ADflow PC if self.options['use_OM_KSP']: states.linear_solver = PetscKSP(iprint=2, atol=1e-8, rtol=1e-8, maxiter=300, ksp_type='gmres') states.linear_solver.precon = LinearUserDefined() functionals = OM_FUNC_COMP(ap=ap, dvgeo=self.dvgeo, solver=self.solver) self.add_subsystem('functionals', functionals, promotes_inputs=des_var_names, max_procs=self.comm.size) self.connect('states.states', 'functionals.states') if self.dvcon is not None: geocon = OM_GEOCON_COMP(dvgeo=self.dvgeo, dvcon=self.dvcon) self.add_subsystem('geocon', geocon, promotes_inputs=geo_var_names) for cons in self.dvcon.constraints.values(): for name, con in cons.items(): if self.comm.rank == 0: print('adding nonlinear dvcon constarint: ', name) self.add_constraint('geocon.%s' % name, lower=con.lower, upper=con.upper, ref=1 / con.scale, vectorize_derivs=True) for name, con in self.dvcon.linearCon.items(): if self.comm.rank == 0: print('adding linear dvcon constraint: ', name) self.add_constraint('geocon.%s' % name, lower=con.lower, upper=con.upper, linear=True, vectorize_derivs=True)