Exemple #1
0
    def __init__(self):
        super(SellarDerivativesGrouped, self).__init__()

        self.add('px', IndepVarComp('x', 1.0), promotes=['*'])
        self.add('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['*'])

        mda = self.add('mda', Group(), promotes=['*'])
        mda.ln_solver = ScipyGMRES()
        mda.add('d1', SellarDis1withDerivatives(), promotes=['*'])
        mda.add('d2', SellarDis2withDerivatives(), promotes=['*'])

        self.add('obj_cmp',
                 ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)',
                          z=np.array([0.0, 0.0]),
                          x=0.0,
                          y1=0.0,
                          y2=0.0),
                 promotes=['*'])

        self.add('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['*'])
        self.add('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['*'])

        mda.nl_solver = NLGaussSeidel()
        mda.d1.fd_options['force_fd'] = True
        mda.d2.fd_options['force_fd'] = True

        self.ln_solver = ScipyGMRES()
Exemple #2
0
    def __init__(self):
        super(SellarStateConnection, self).__init__()

        self.add('px', IndepVarComp('x', 1.0), promotes=['*'])
        self.add('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['*'])

        sub = self.add('sub', Group(), promotes=['*'])
        sub.ln_solver = ScipyGMRES()

        subgrp = sub.add('state_eq_group', Group(), promotes=['*'])
        subgrp.ln_solver = ScipyGMRES()
        subgrp.add('state_eq', StateConnection())

        sub.add('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1'])
        sub.add('d2', SellarDis2withDerivatives(), promotes=['z', 'y1'])

        self.connect('state_eq.y2_command', 'd1.y2')
        self.connect('d2.y2', 'state_eq.y2_actual')

        self.add('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)',
                                     z=np.array([0.0, 0.0]), x=0.0, y1=0.0, y2=0.0),
                 promotes=['x', 'z', 'y1', 'obj'])
        self.connect('d2.y2', 'obj_cmp.y2')

        self.add('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1'])
        self.add('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2'])
        self.connect('d2.y2', 'con_cmp2.y2')

        self.nl_solver = Newton()
Exemple #3
0
    def __init__(self):
        super(SellarDerivatives, self).__init__()

        self.add('px', IndepVarComp('x', 1.0), promotes=['x'])
        self.add('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z'])

        self.add('d1',
                 SellarDis1withDerivatives(),
                 promotes=['x', 'z', 'y1', 'y2'])
        self.add('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2'])

        self.add('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'])

        self.add('con_cmp1',
                 ExecComp('con1 = 3.16 - y1'),
                 promotes=['con1', 'y1'])
        self.add('con_cmp2',
                 ExecComp('con2 = y2 - 24.0'),
                 promotes=['con2', 'y2'])

        self.nl_solver = NLGaussSeidel()
        self.ln_solver = ScipyGMRES()
Exemple #4
0
    def test_driver_records_metadata(self):
        size = 3

        prob = Problem(Group(), impl=impl)

        G1 = prob.root.add('G1', ParallelGroup())
        G1.add('P1', IndepVarComp('x', np.ones(size, float) * 1.0))
        G1.add('P2', IndepVarComp('x', np.ones(size, float) * 2.0))

        prob.root.add('C1', ABCDArrayComp(size))

        prob.root.connect('G1.P1.x', 'C1.a')
        prob.root.connect('G1.P2.x', 'C1.b')

        prob.driver.add_recorder(self.recorder)

        self.recorder.options['record_metadata'] = True
        prob.setup(check=False)

        prob.cleanup()

        expected = (
            list(prob.root.params.iteritems()),
            list(prob.root.unknowns.iteritems()),
            list(prob.root.resids.iteritems()),
        )

        self.assertMetadataRecorded(expected)
Exemple #5
0
    def __init__(self):
        super(SellarNoDerivatives, self).__init__()

        self.add('px', IndepVarComp('x', 1.0), promotes=['x'])
        self.add('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z'])

        cycle = self.add('cycle', Group(), promotes=['x', 'z', 'y1', 'y2'])
        cycle.ln_solver = ScipyGMRES()
        cycle.add('d1', SellarDis1(), promotes=['x', 'z', 'y1', 'y2'])
        cycle.add('d2', SellarDis2(), promotes=['z', 'y1', 'y2'])

        self.add('obj_cmp',
                 ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)',
                          z=np.array([0.0, 0.0]),
                          x=0.0),
                 promotes=['x', 'z', 'y1', 'y2', 'obj'])

        self.add('con_cmp1',
                 ExecComp('con1 = 3.16 - y1'),
                 promotes=['con1', 'y1'])
        self.add('con_cmp2',
                 ExecComp('con2 = y2 - 24.0'),
                 promotes=['con2', 'y2'])

        self.nl_solver = NLGaussSeidel()
        self.cycle.d1.deriv_options['type'] = 'fd'
        self.cycle.d2.deriv_options['type'] = 'fd'
Exemple #6
0
    def test_unknown_vec(self):

        top = Problem()
        top.root = Group()
        top.root.add('comp', ArrayComp2D(), promotes=['x', 'y'])
        top.root.add('p1',
                     IndepVarComp('x', np.array([[1.0, 2.0], [3.0, 4.0]])),
                     promotes=['x'])
        top.root.add('comp2', SimpleComp())
        top.root.add('p2', IndepVarComp('x', 3.0))
        top.root.connect('p2.x', 'comp2.x')

        top.setup(check=False)
        top.run()

        unknowns = ComplexStepSrcVecWrapper(top.root.comp.unknowns)

        # Unknowns are always complex
        y = unknowns['y']
        self.assertTrue(y.dtype == np.complex)
        self.assertEquals(y[0, 1], 46.0 + 0j)

        # Set an unknown
        unknowns['y'][0, 1] = 13.0 + 17.0j
        self.assertEquals(unknowns['y'][0, 1], 13.0 + 17.0j)

        # Extract flat var
        cval = unknowns.flat('y')
        self.assertEquals(cval[1], 13.0 + 17.0j)
        self.assertEquals(cval.shape[0], 4)

        unknowns = ComplexStepSrcVecWrapper(top.root.comp2.unknowns)

        # Unknowns are always complex
        y = unknowns['y']
        self.assertTrue(y.dtype == np.complex)
        self.assertEquals(y, 6.0 + 0j)

        # Set an unknown
        unknowns['y'] = 13.0 + 17.0j
        self.assertEquals(unknowns['y'], 13.0 + 17.0j)

        # Extract flat var
        cval = unknowns.flat('y')
        self.assertEquals(cval, 13.0 + 17.0j)
        self.assertEquals(cval.shape[0], 1)

        # Make sure all other functions work for coverage
        self.assertEquals(len(unknowns), 1)
        self.assertTrue('y' in unknowns)
        plist = [z for z in unknowns]
        self.assertEquals(plist, ['y'])
        self.assertEquals(unknowns.keys(), top.root.comp2.unknowns.keys())
        self.assertEquals(unknowns.metadata('y'),
                          top.root.comp2.unknowns.metadata('y'))
        plist1 = [z for z in unknowns.iterkeys()]
        plist2 = [z for z in top.root.comp2.unknowns.iterkeys()]
Exemple #7
0
    def __init__(self):
        super(FanIn, self).__init__()

        self.add('p1', IndepVarComp('x1', 1.0))
        self.add('p2', IndepVarComp('x2', 1.0))
        self.add('comp1', ExecComp(['y=-2.0*x']))
        self.add('comp2', ExecComp(['y=5.0*x']))
        self.add('comp3', ExecComp(['y=3.0*x1+7.0*x2']))

        self.connect("comp1.y", "comp3.x1")
        self.connect("comp2.y", "comp3.x2")
        self.connect("p1.x1", "comp1.x")
        self.connect("p2.x2", "comp2.x")
Exemple #8
0
    def test_basic(self):
        size = 3

        prob = Problem(Group(), impl=impl)

        G1 = prob.root.add('G1', ParallelGroup())
        G1.add('P1', IndepVarComp('x', np.ones(size, float) * 1.0))
        G1.add('P2', IndepVarComp('x', np.ones(size, float) * 2.0))

        prob.root.add('C1', ABCDArrayComp(size))

        prob.root.connect('G1.P1.x', 'C1.a')
        prob.root.connect('G1.P2.x', 'C1.b')

        prob.driver.add_recorder(self.recorder)

        self.recorder.options['record_params'] = True
        self.recorder.options['record_resids'] = True

        prob.setup(check=False)

        t0, t1 = run(prob)
        prob.cleanup()

        coordinate = ['Driver', (1, )]

        expected_params = [
            ("C1.a", [1.0, 1.0, 1.0]),
            ("C1.b", [2.0, 2.0, 2.0]),
        ]

        expected_unknowns = [
            ("G1.P1.x", np.array([1.0, 1.0, 1.0])),
            ("G1.P2.x", np.array([2.0, 2.0, 2.0])),
            ("C1.c", np.array([3.0, 3.0, 3.0])),
            ("C1.d", np.array([-1.0, -1.0, -1.0])),
            ("C1.out_string", "_C1"),
            ("C1.out_list", [1.5]),
        ]
        expected_resids = [
            ("G1.P1.x", np.array([0.0, 0.0, 0.0])),
            ("G1.P2.x", np.array([0.0, 0.0, 0.0])),
            ("C1.c", np.array([0.0, 0.0, 0.0])),
            ("C1.d", np.array([0.0, 0.0, 0.0])),
            ("C1.out_string", ""),
            ("C1.out_list", []),
        ]

        self.assertIterationDataRecorded(
            ((coordinate, (t0, t1), expected_params, expected_unknowns,
              expected_resids), ), self.eps, prob.root)
    def __init__(self):
        super(ConvergeDivergeGroups, self).__init__()

        self.add('p', IndepVarComp('x', 2.0))

        sub1 = self.add('sub1', Group())
        sub1.add('comp1', Comp1())

        sub2 = sub1.add('sub2', Group())
        sub2.add('comp2', Comp2())
        sub2.add('comp3', Comp3())
        sub1.add('comp4', Comp4())

        sub3 = self.add('sub3', Group())
        sub3.add('comp5', Comp5())
        sub3.add('comp6', Comp6())
        self.add('comp7', Comp7())

        self.connect("p.x", "sub1.comp1.x1")
        self.connect('sub1.comp1.y1', 'sub1.sub2.comp2.x1')
        self.connect('sub1.comp1.y2', 'sub1.sub2.comp3.x1')
        self.connect('sub1.sub2.comp2.y1', 'sub1.comp4.x1')
        self.connect('sub1.sub2.comp3.y1', 'sub1.comp4.x2')
        self.connect('sub1.comp4.y1', 'sub3.comp5.x1')
        self.connect('sub1.comp4.y2', 'sub3.comp6.x1')
        self.connect('sub3.comp5.y1', 'comp7.x1')
        self.connect('sub3.comp6.y1', 'comp7.x2')
Exemple #10
0
    def __init__(self):
        super(FanInGrouped, self).__init__()

        self.add('p1', IndepVarComp('x1', 1.0))
        self.add('p2', IndepVarComp('x2', 1.0))
        self.add('p3', IndepVarComp('x3', 1.0))
        sub = self.add('sub', ParallelGroup())

        sub.add('comp1', ExecComp(['y=-2.0*x']))
        sub.add('comp2', ExecComp(['y=5.0*x']))
        self.add('comp3', ExecComp(['y=3.0*x1+7.0*x2']))

        self.connect("sub.comp1.y", "comp3.x1")
        self.connect("sub.comp2.y", "comp3.x2")
        self.connect("p1.x1", "sub.comp1.x")
        self.connect("p2.x2", "sub.comp2.x")
Exemple #11
0
    def test_simple_float(self):

        prob = Problem()
        prob.root = root = Group()
        root.add('x_param', IndepVarComp('x', 17.0), promotes=['x'])
        root.add('y_param', IndepVarComp('y', 19.0), promotes=['y'])
        root.add('mycomp', Paraboloid(), promotes=['x', 'y', 'f_xy'])

        # This will give poor FD, but good CS
        root.mycomp.fd_options['step_size'] = 1.0e1
        root.mycomp.fd_options['force_fd'] = True
        root.mycomp.fd_options['form'] = 'complex_step'

        prob.setup(check=False)
        prob.run()

        J = prob.calc_gradient(['x'], ['f_xy'], mode='fwd', return_format='dict')
        assert_rel_error(self, J['f_xy']['x'][0][0], 47.0, 1e-6)
Exemple #12
0
    def __init__(self):
        super(FanOut, self).__init__()

        self.add('p', IndepVarComp('x', 1.0))
        self.add('comp1', ExecComp(['y=3.0*x']))
        self.add('comp2', ExecComp(['y=-2.0*x']))
        self.add('comp3', ExecComp(['y=5.0*x']))

        self.connect("comp1.y", "comp2.x")
        self.connect("comp1.y", "comp3.x")
        self.connect("p.x", "comp1.x")
Exemple #13
0
    def test_driver_doesnt_records_metadata(self):
        size = 3

        prob = Problem(Group(), impl=impl)

        G1 = prob.root.add('G1', ParallelGroup())
        G1.add('P1', IndepVarComp('x', np.ones(size, float) * 1.0))
        G1.add('P2', IndepVarComp('x', np.ones(size, float) * 2.0))

        prob.root.add('C1', ABCDArrayComp(size))

        prob.root.connect('G1.P1.x', 'C1.a')
        prob.root.connect('G1.P2.x', 'C1.b')

        prob.driver.add_recorder(self.recorder)

        self.recorder.options['record_metadata'] = False
        prob.setup(check=False)

        prob.cleanup()

        self.assertMetadataRecorded(None)
Exemple #14
0
    def __init__(self):
        super(SingleDiamond, self).__init__()

        self.add('p', IndepVarComp('x', 2.0))

        self.add('comp1', Comp1())
        self.add('comp2', Comp2())
        self.add('comp3', Comp3())
        self.add('comp4', Comp4())

        self.connect("p.x", "comp1.x1")
        self.connect('comp1.y1', 'comp2.x1')
        self.connect('comp1.y2', 'comp3.x1')
        self.connect('comp2.y1', 'comp4.x1')
        self.connect('comp3.y1', 'comp4.x2')
Exemple #15
0
    def __init__(self):
        super(ExampleByObjGroup, self).__init__()

        self.G2 = self.add('G2', Group())
        self.C1 = self.G2.add('C1', IndepVarComp('x', 'foo'))

        self.G1 = self.G2.add('G1', Group())
        self.C2 = self.G1.add('C2', SimplePassByObjComp())

        self.G3 = self.add('G3', Group())
        self.C3 = self.G3.add('C3', SimplePassByObjComp())
        self.C4 = self.G3.add('C4', SimplePassByObjComp())

        self.G2.connect('C1.x', 'G1.C2.x')
        self.connect('G2.G1.C2.y', 'G3.C3.x')
        self.G3.connect('C3.y', 'C4.x')
Exemple #16
0
    def __init__(self):
        super(ExampleGroup, self).__init__()

        self.G2 = self.add('G2', Group())
        self.C1 = self.G2.add('C1', IndepVarComp('x', 5.))

        self.G1 = self.G2.add('G1', Group())
        self.C2 = self.G1.add('C2', ExecComp('y=x*2.0', x=3., y=5.5))

        self.G3 = self.add('G3', Group())
        self.C3 = self.G3.add('C3', ExecComp('y=x*2.0', x=3., y=5.5))
        self.C4 = self.G3.add('C4', ExecComp('y=x*2.0', x=3., y=5.5))

        self.G2.connect('C1.x', 'G1.C2.x')
        self.connect('G2.G1.C2.y', 'G3.C3.x')
        self.G3.connect('C3.y', 'C4.x')
Exemple #17
0
    def __init__(self):
        super(FanOutGrouped, self).__init__()

        self.add('p', IndepVarComp('x', 1.0))
        self.add('comp1', ExecComp(['y=3.0*x']))
        sub = self.add('sub', ParallelGroup())
        sub.add('comp2', ExecComp(['y=-2.0*x']))
        sub.add('comp3', ExecComp(['y=5.0*x']))

        self.add('c2', ExecComp(['y=x']))
        self.add('c3', ExecComp(['y=x']))
        self.connect('sub.comp2.y', 'c2.x')
        self.connect('sub.comp3.y', 'c3.x')

        self.connect("comp1.y", "sub.comp2.x")
        self.connect("comp1.y", "sub.comp3.x")
        self.connect("p.x", "comp1.x")
Exemple #18
0
    def setUp(self):

        self.prob = Problem()
        self.prob.root = Group()

        self.prob.root.add('tt',TubeTemp())

        params = (
            ('P', 0.304434211, {'units':'psi'}),
            ('T', 1710., {'units':'degR'})
        )
        self.prob.root.add('des_vars', IndepVarComp(params), promotes=['*'])
        self.prob.root.connect('P', 'tt.nozzle_air.P')
        self.prob.root.connect('T', 'tt.nozzle_air.T')
        self.prob.root.connect('P', 'tt.bearing_air.P')
        self.prob.root.connect('T', 'tt.bearing_air.T')

        self.prob.setup(check=True)
Exemple #19
0
    def __init__(self):
        super(TubeLimitFlow, self).__init__()
        self.add('Mach_param', IndepVarComp('Mach', 1.0), promotes=['Mach'])
        #self.add('Mach_con', ConstraintComp('Mach > 0.0'))
        self.add('tube_struct', TubeStructural())
        self.add('inlet', InletGeom())
        self.add('AR_comp',
                 AreaRatio(),
                 promotes=['Mach_bypass', 'bypass_area'])
        self.add('tube_thermo', TubeThermo())
        self.add('tube_total', SetTotal(init_reacts=AIR_MIX, mode='T'))
        self.add('tube_static', SetStaticMN(init_reacts=AIR_MIX))
        self.add('bypass_total', SetTotal(init_reacts=AIR_MIX, mode='T'))
        self.add('bypass_static', SetStaticMN(init_reacts=AIR_MIX))
        self.add('tube_aero',
                 TubeAero(),
                 promotes=['tube_area', 'W_tube', 'W_kant', 'W_excess'])

        self.connect('Mach', 'AR_comp.Mach')
        #self.connect('Mach', 'Mach_con.Mach')
        self.connect('tube_struct.tube_r', 'AR_comp.tube_r')
        self.connect('inlet.area_out', 'AR_comp.inlet_area')
        self.connect('Mach', 'tube_thermo.Mach')
        self.connect('tube_thermo.Pt', 'tube_total.P')
        self.connect('tube_thermo.Tt', 'tube_total.T')
        self.connect('tube_thermo.Pt', 'bypass_total.P')
        self.connect('tube_thermo.Tt', 'bypass_total.T')
        self.connect('tube_total.S', 'tube_static.S')
        self.connect('tube_total.h', 'tube_static.ht')
        self.connect('tube_total.P', 'tube_static.Pt')
        self.connect('tube_total.gamma', 'tube_static.gamt')
        self.connect('bypass_total.S', 'bypass_static.S')
        self.connect('bypass_total.h', 'bypass_static.ht')
        self.connect('bypass_total.P', 'bypass_static.Pt')
        self.connect('bypass_total.gamma', 'bypass_static.gamt')
        self.connect('Mach', 'tube_static.MN_target')
        self.connect('Mach_bypass', 'bypass_static.MN_target')
        self.connect('tube_static.V', 'tube_aero.velocity_tube')
        self.connect('bypass_static.V', 'tube_aero.velocity_bypass')
        self.connect('tube_total.rho', 'tube_aero.rho_tube')
        self.connect('bypass_total.rho', 'tube_aero.rho_bypass')
        self.connect('bypass_area', 'tube_aero.bypass_area')
        self.connect('tube_struct.tube_r', 'tube_aero.tube_r')
Exemple #20
0
    def __init__(self):
        super(FanOutAllGrouped, self).__init__()

        self.add('p', IndepVarComp('x', 1.0))
        sub1 = self.add('sub1', Group())
        sub1.add('comp1', ExecComp(['y=3.0*x']))
        sub2 = self.add('sub2', Group())
        sub2.add('comp2', ExecComp(['y=-2.0*x']))
        sub3 = self.add('sub3', Group())
        sub3.add('comp3', ExecComp(['y=5.0*x']))

        self.add('c2', ExecComp(['y=x']))
        self.add('c3', ExecComp(['y=x']))
        self.connect('sub2.comp2.y', 'c2.x')
        self.connect('sub3.comp3.y', 'c3.x')

        self.connect("sub1.comp1.y", "sub2.comp2.x")
        self.connect("sub1.comp1.y", "sub3.comp3.x")
        self.connect("p.x", "sub1.comp1.x")
Exemple #21
0
    def setup(self, compname, inputs, state0):

        self.model.root.add('comp',
                            eval('%s(NTIME)' % compname),
                            promotes=['*'])

        for item in inputs + state0:
            pshape = self.model.root.comp._params_dict[item]['shape']
            self.model.root.add('p_%s' % item,
                                IndepVarComp(item, np.zeros((pshape))),
                                promotes=['*'])

        self.model.setup(check=False)

        for item in inputs + state0:
            val = self.model['%s' % item]
            if hasattr(val, 'shape'):
                shape1 = val.shape
                self.model['%s' % item] = np.random.random(shape1)
            else:
                self.model['%s' % item] = np.random.random()
Exemple #22
0
    def test_array2D(self):

        prob = Problem()
        prob.root = root = Group()
        root.add('x_param', IndepVarComp('x', np.ones((2, 2))), promotes=['*'])
        root.add('mycomp', ArrayComp2D(), promotes=['x', 'y'])

        root.mycomp.deriv_options['step_size'] = 1.0e-1
        root.mycomp.deriv_options['type'] = 'cs'

        prob.setup(check=False)
        prob.run()

        J = prob.calc_gradient(['x'], ['y'], mode='fwd', return_format='dict')
        Jbase = prob.root.mycomp._jacobian_cache
        diff = np.linalg.norm(J['y']['x'] - Jbase['y', 'x'])
        assert_rel_error(self, diff, 0.0, 1e-8)

        J = prob.calc_gradient(['x'], ['y'], mode='rev', return_format='dict')
        diff = np.linalg.norm(J['y']['x'] - Jbase['y', 'x'])
        assert_rel_error(self, diff, 0.0, 1e-8)
Exemple #23
0
    def __init__(self):
        super(ConvergeDiverge, self).__init__()

        self.add('p', IndepVarComp('x', 2.0))

        self.add('comp1', Comp1())
        self.add('comp2', Comp2())
        self.add('comp3', Comp3())
        self.add('comp4', Comp4())
        self.add('comp5', Comp5())
        self.add('comp6', Comp6())
        self.add('comp7', Comp7())

        self.connect("p.x", "comp1.x1")
        self.connect('comp1.y1', 'comp2.x1')
        self.connect('comp1.y2', 'comp3.x1')
        self.connect('comp2.y1', 'comp4.x1')
        self.connect('comp3.y1', 'comp4.x2')
        self.connect('comp4.y1', 'comp5.x1')
        self.connect('comp4.y2', 'comp6.x1')
        self.connect('comp5.y1', 'comp7.x1')
        self.connect('comp6.y1', 'comp7.x2')
Exemple #24
0
    def test_OMModelWrapper(self):
        logging.debug('')
        logging.debug('test_OMModelWrapper')

        this_dir_path = os.path.realpath(os.path.dirname(__file__))
        addl_lib_path = os.path.join(this_dir_path, 'VehicleDesign.mo')

        problem = Problem()
        root = problem.root = Group()
        test_model = OMModelWrapper('SimAcceleration', addl_lib_path)
        root.add('simacceleration', test_model)
        root.add('simtime', IndepVarComp('duration', val='15.0'))
        root.connect('simtime.duration', 'simacceleration.stopTime')

        dump_filename = 'dump.log'
        # Delete old dumpfile
        try:
            os.remove(dump_filename)
        except OSError:
            pass

        recorder = DumpRecorder(dump_filename)
        recorder.options['record_params'] = True
        recorder.options['record_metadata'] = True
        recorder.options['record_resids'] = True
        problem.driver.add_recorder(recorder)

        problem.setup()
        problem.run()

        problem.driver.recorders[0].close()

        self.assertTrue(os.path.exists(dump_filename))
        with open(dump_filename) as dumpf:
            dump = dumpf.readlines()
        self.assertTrue('  simtime.duration: 15.0\n' in dump)
        self.assertTrue(
            '  simacceleration.accel_time: [  0.   0.   0. ...,  15.  15.  15.]\n'
            in dump)
Exemple #25
0
    def test_unit_convert(self):

        prob = Problem()
        prob.root = Group()
        prob.root.add('src', SrcComp())
        prob.root.add('tgtF', TgtCompF())
        prob.root.add('tgtC', TgtCompC())
        prob.root.add('tgtK', TgtCompK())
        prob.root.add('px1', IndepVarComp('x1', 100.0), promotes=['x1'])
        prob.root.connect('x1', 'src.x1')
        prob.root.connect('src.x2', 'tgtF.x2')
        prob.root.connect('src.x2', 'tgtC.x2')
        prob.root.connect('src.x2', 'tgtK.x2')

        prob.setup(check=False)
        prob.run()

        p1 = ComplexStepTgtVecWrapper(prob.root.tgtF.params)
        p2 = ComplexStepTgtVecWrapper(prob.root.tgtC.params)
        p3 = ComplexStepTgtVecWrapper(prob.root.tgtK.params)

        assert_rel_error(self, p1['x2'], 212.0, 1.0e-6)
        assert_rel_error(self, p2['x2'], 100.0, 1.0e-6)
        assert_rel_error(self, p3['x2'], 373.15, 1.0e-6)
Exemple #26
0
from openmdao.components.indep_var_comp import IndepVarComp

if __name__ == "__main__":
    # -------------------------
    # --- Default Test Case ---
    # -------------------------
    p1 = Problem()
    p1.root = Group()
    #p1.root.add("freestream",Freestream())
    p1.root.add("fs", FlowStart())

    params = (
        ('P', 0.5, {
            "units": "psi"
        }),
        ('T', 291.0, {
            "units": "K"
        }),
        ('MN', 0.8),
    )
    p1.root.add('des_vars', IndepVarComp(params))
    p1.root.connect('des_vars.P', 'fs.P')
    #p1.root.connect('des_vars.MN', 'fs.MN')
    p1.root.connect('des_vars.T', 'fs.T')

    p1.setup(check=False)
    p1.run()
    p1.root.list_connections()
    print("total P: ", p1["fs.Fl_O:tot:P"])
    print("static P: ", p1["fs.Fl_O:stat:P"])
Exemple #27
0
    def __init__(self,
                 n,
                 m,
                 solar_raw1=None,
                 solar_raw2=None,
                 comm_raw=None,
                 power_raw=None,
                 initial_params=None):

        super(CADRE, self).__init__()

        self.ln_solver.options['mode'] = 'auto'

        # Analysis parameters
        self.n = n
        self.m = m
        h = 43200.0 / (self.n - 1)

        # User-defined initial parameters
        if initial_params is None:
            initial_params = {}
        if 't1' not in initial_params:
            initial_params['t1'] = 0.0
        if 't2' not in initial_params:
            initial_params['t2'] = 43200.0
        if 't' not in initial_params:
            initial_params['t'] = np.array(range(0, n)) * h
        if 'CP_Isetpt' not in initial_params:
            initial_params['CP_Isetpt'] = 0.2 * np.ones((12, self.m))
        if 'CP_gamma' not in initial_params:
            initial_params['CP_gamma'] = np.pi / 4 * np.ones((self.m, ))
        if 'CP_P_comm' not in initial_params:
            initial_params['CP_P_comm'] = 0.1 * np.ones((self.m, ))

        if 'iSOC' not in initial_params:
            initial_params['iSOC'] = np.array([0.5])

        # Fixed Station Parameters for the CADRE problem.
        # McMurdo station: -77.85, 166.666667
        # Ann Arbor: 42.2708, -83.7264
        if 'LD' not in initial_params:
            initial_params['LD'] = 5000.0
        if 'lat' not in initial_params:
            initial_params['lat'] = 42.2708
        if 'lon' not in initial_params:
            initial_params['lon'] = -83.7264
        if 'alt' not in initial_params:
            initial_params['alt'] = 0.256

        # Initial Orbital Elements
        if 'r_e2b_I0' not in initial_params:
            initial_params['r_e2b_I0'] = np.zeros((6, ))

        # Some initial setup.
        self.add('p_t1',
                 IndepVarComp('t1', initial_params['t1']),
                 promotes=['*'])
        self.add('p_t2',
                 IndepVarComp('t2', initial_params['t2']),
                 promotes=['*'])
        self.add('p_t', IndepVarComp('t', initial_params['t']), promotes=['*'])

        # Design parameters
        self.add('p_CP_Isetpt',
                 IndepVarComp('CP_Isetpt', initial_params['CP_Isetpt']),
                 promotes=['*'])
        self.add('p_CP_gamma',
                 IndepVarComp('CP_gamma', initial_params['CP_gamma']),
                 promotes=['*'])
        self.add('p_CP_P_comm',
                 IndepVarComp('CP_P_comm', initial_params['CP_P_comm']),
                 promotes=['*'])
        self.add('p_iSOC',
                 IndepVarComp('iSOC', initial_params['iSOC']),
                 promotes=['*'])

        # These are broadcast params in the MDP.
        #self.add('p_cellInstd', IndepVarComp('cellInstd', np.ones((7, 12))),
        #         promotes=['*'])
        #self.add('p_finAngle', IndepVarComp('finAngle', np.pi / 4.), promotes=['*'])
        #self.add('p_antAngle', IndepVarComp('antAngle', 0.0), promotes=['*'])

        self.add('param_LD',
                 IndepVarComp('LD', initial_params['LD']),
                 promotes=['*'])
        self.add('param_lat',
                 IndepVarComp('lat', initial_params['lat']),
                 promotes=['*'])
        self.add('param_lon',
                 IndepVarComp('lon', initial_params['lon']),
                 promotes=['*'])
        self.add('param_alt',
                 IndepVarComp('alt', initial_params['alt']),
                 promotes=['*'])
        self.add('param_r_e2b_I0',
                 IndepVarComp('r_e2b_I0', initial_params['r_e2b_I0']),
                 promotes=['*'])

        # Add Component Models
        self.add("BsplineParameters", BsplineParameters(n, m), promotes=['*'])
        self.add("Attitude_Angular", Attitude_Angular(n), promotes=['*'])
        self.add("Attitude_AngularRates",
                 Attitude_AngularRates(n, h),
                 promotes=['*'])
        self.add("Attitude_Attitude", Attitude_Attitude(n), promotes=['*'])
        self.add("Attitude_Roll", Attitude_Roll(n), promotes=['*'])
        self.add("Attitude_RotationMtx",
                 Attitude_RotationMtx(n),
                 promotes=['*'])
        self.add("Attitude_RotationMtxRates",
                 Attitude_RotationMtxRates(n, h),
                 promotes=['*'])

        # Not needed?
        #self.add("Attitude_Sideslip", Attitude_Sideslip(n))

        self.add("Attitude_Torque", Attitude_Torque(n), promotes=['*'])
        self.add("BatteryConstraints", BatteryConstraints(n), promotes=['*'])
        self.add("BatteryPower", BatteryPower(n), promotes=['*'])
        self.add("BatterySOC", BatterySOC(n, h), promotes=['*'])
        self.add("Comm_AntRotation", Comm_AntRotation(n), promotes=['*'])
        self.add("Comm_AntRotationMtx", Comm_AntRotationMtx(n), promotes=['*'])
        self.add("Comm_BitRate", Comm_BitRate(n), promotes=['*'])
        self.add("Comm_DataDownloaded",
                 Comm_DataDownloaded(n, h),
                 promotes=['*'])
        self.add("Comm_Distance", Comm_Distance(n), promotes=['*'])
        self.add("Comm_EarthsSpin", Comm_EarthsSpin(n), promotes=['*'])
        self.add("Comm_EarthsSpinMtx", Comm_EarthsSpinMtx(n), promotes=['*'])
        self.add("Comm_GainPattern",
                 Comm_GainPattern(n, comm_raw),
                 promotes=['*'])
        self.add("Comm_GSposEarth", Comm_GSposEarth(n), promotes=['*'])
        self.add("Comm_GSposECI", Comm_GSposECI(n), promotes=['*'])
        self.add("Comm_LOS", Comm_LOS(n), promotes=['*'])
        self.add("Comm_VectorAnt", Comm_VectorAnt(n), promotes=['*'])
        self.add("Comm_VectorBody", Comm_VectorBody(n), promotes=['*'])
        self.add("Comm_VectorECI", Comm_VectorECI(n), promotes=['*'])
        self.add("Comm_VectorSpherical",
                 Comm_VectorSpherical(n),
                 promotes=['*'])

        # Not needed?
        #self.add("Orbit_Initial", Orbit_Initial())

        self.add("Orbit_Dynamics", Orbit_Dynamics(n, h), promotes=['*'])
        self.add("Power_CellVoltage",
                 Power_CellVoltage(n, power_raw),
                 promotes=['*'])
        self.add("Power_SolarPower", Power_SolarPower(n), promotes=['*'])
        self.add("Power_Total", Power_Total(n), promotes=['*'])

        # Not needed?
        #self.add("ReactionWheel_Motor", ReactionWheel_Motor(n))

        self.add("ReactionWheel_Power", ReactionWheel_Power(n), promotes=['*'])
        self.add("ReactionWheel_Torque",
                 ReactionWheel_Torque(n),
                 promotes=['*'])
        self.add("ReactionWheel_Dynamics",
                 ReactionWheel_Dynamics(n, h),
                 promotes=['*'])
        self.add("Solar_ExposedArea",
                 Solar_ExposedArea(n, solar_raw1, solar_raw2),
                 promotes=['*'])
        self.add("Sun_LOS", Sun_LOS(n), promotes=['*'])
        self.add("Sun_PositionBody", Sun_PositionBody(n), promotes=['*'])
        self.add("Sun_PositionECI", Sun_PositionECI(n), promotes=['*'])
        self.add("Sun_PositionSpherical",
                 Sun_PositionSpherical(n),
                 promotes=['*'])
        self.add("ThermalTemperature",
                 ThermalTemperature(n, h),
                 promotes=['*'])
class TakeLast(Component):
    def __init__(self):
        super(TakeLast, self).__init__()
        self.add_param('input', val=0.1)
        self.add_output('output', val=0.1, pass_by_obj=True)

    def solve_nonlinear(self, params, unknowns, resids):
        unknowns['output'] = params['input'][-1]


if __name__ == "__main__":
    top = Problem()

    root = top.root = Group()

    root.add('simprop', IndepVarComp('simtime', val="100"))
    root.add('p1', IndepVarComp('rho', val=1.0))
    root.add('p2', IndepVarComp('jturbine', val=1.0))
    root.add('p3', IndepVarComp('ratio', val=1.0))
    root.add(
        'modelica',
        OMModelWrapper(
            'WindPowerPlants.Examples.GenericPlantRayleigh',
            '/Users/adam/repo/WindPowerPlants/WindPowerPlants/package.mo'))
    root.add('tl_peakPowerOutput', TakeLast())
    root.add('tl_integratedEnergy', TakeLast())

    top.driver = UniformDriver(5000)
    top.driver.add_desvar('p1.rho', low=1.0, high=1.5)
    top.driver.add_desvar('p2.jturbine',
                          low=13000000 * 0.7,
Exemple #29
0
    if 'petsc' in sys.argv:
        from openmdao.core.petsc_impl import PetscImpl
        impl = PetscImpl
    else:
        from openmdao.core.basic_impl import BasicImpl
        impl = BasicImpl

    g = Group()
    p = Problem(impl=impl, root=g)

    if 'gmres' in sys.argv:
        from openmdao.solvers.scipy_gmres import ScipyGMRES
        p.root.ln_solver = ScipyGMRES()

    g.add("P", IndepVarComp('x', numpy.ones(vec_size)))

    p.driver.add_desvar("P.x")

    par = g.add("par", ParallelGroup())
    for pt in range(pts):
        ptname = "G%d" % pt
        ptg = par.add(ptname, Group())
        create_dyncomps(ptg,
                        num_comps,
                        2,
                        2,
                        2,
                        var_factory=lambda: numpy.zeros(vec_size))
        g.connect("P.x", "par.%s.C0.p0" % ptname)
Exemple #30
0
    def test_param_vec(self):

        top = Problem()
        top.root = Group()
        top.root.add('comp', ArrayComp2D(), promotes=['x', 'y'])
        top.root.add('p1',
                     IndepVarComp('x', np.array([[1.0, 2.0], [3.0, 4.0]])),
                     promotes=['x'])
        top.root.add('comp2', SimpleComp())
        top.root.add('p2', IndepVarComp('x', 3.0))
        top.root.connect('p2.x', 'comp2.x')

        top.setup(check=False)
        top.run()

        params = ComplexStepTgtVecWrapper(top.root.comp.params)

        # Get a param that isn't complex-stepped
        x = params['x']
        self.assertTrue(x.dtype == np.float)
        self.assertEquals(x[0, 1], 2.0)

        # Get a param that is now complex
        params.set_complex_var('x')
        x = params['x']
        self.assertTrue(x.dtype == np.complex)
        self.assertEquals(x[0, 1], 2.0 + 0j)

        # Apply complex step and get param
        params.step_complex(1, 4.0)
        x = params['x']
        self.assertEquals(x[0, 1], 2.0 + 4j)

        # Unset complex
        params.set_complex_var(None)
        x = params['x']
        self.assertEquals(x[0, 1], 2.0)

        params = ComplexStepTgtVecWrapper(top.root.comp2.params)

        # Get a param that isn't complex-stepped
        x = params['x']
        self.assertTrue(x.dtype == np.float)
        self.assertEquals(x, 3.0)

        # Get a param that is now complex
        params.set_complex_var('x')
        x = params['x']
        self.assertTrue(x.dtype == np.complex)
        self.assertEquals(x, 3.0 + 0j)

        # Apply complex step and get param
        params.step_complex(0, 4.0)
        self.assertEquals(x, 3.0 + 4j)

        # Make sure all other functions work for coverage
        self.assertEquals(len(params), 1)
        self.assertTrue('x' in params)
        plist = [z for z in params]
        self.assertEquals(plist, ['x'])
        self.assertEquals(params.keys(), top.root.comp2.params.keys())
        self.assertEquals(params.metadata('x'),
                          top.root.comp2.params.metadata('x'))
        plist1 = [z for z in params.iterkeys()]
        plist2 = [z for z in top.root.comp2.params.iterkeys()]