Esempio n. 1
0
    def setup(self):
        g = self.group

        # define a subsystem (this is a very simple example)
        group = Group()
        p = group.create_indep_var('p', val=7)
        q = group.create_indep_var('q', val=8)
        r = p + q
        group.register_output('r', r)

        # add child system
        g.add_subsystem('R', group, promotes=['*'])
        # declare output of child system as input to parent system
        r = g.declare_input('r')

        c = g.declare_input('c', val=18)

        # a == (3 + a - 2 * a**2)**(1 / 4)
        with g.create_group('coeff_a') as group:
            a = group.create_output('a')
            a.define((3 + a - 2 * a**2)**(1 / 4))
            group.nonlinear_solver = NonlinearBlockGS(iprint=0, maxiter=100)

        a = g.declare_input('a')

        with g.create_group('coeff_b') as group:
            group.create_indep_var('b', val=-4)

        b = g.declare_input('b')
        y = g.create_implicit_output('y')
        z = a * y**2 + b * y + c - r
        y.define_residual_bracketed(z, x1=0, x2=2)
Esempio n. 2
0
    def initialize(self):
        self.options.declare('out_name', types=str)
        self.options.declare('condition', types=Variable)
        self.options.declare('expr_true', types=Variable)
        self.options.declare('expr_false', types=Variable)
        self.options.declare('n2', types=bool, default=False)
        self.condition = Problem()
        self.condition.model = Group()
        self.ptrue = Problem()
        self.ptrue.model = Group()
        self.pfalse = Problem()
        self.pfalse.model = Group()

        # self.all_outputs: Set[str] = set()
        self.in_exprs_condition: Dict[Set[Input]] = dict()
        self.in_exprs_true: Dict[Set[Input]] = dict()
        self.in_exprs_false: Dict[Set[Input]] = dict()

        self.derivs = dict()
Esempio n. 3
0
    def setup(self):
        g = self.group

        # define a subsystem (this is a very simple example)
        group = Group()
        p = group.create_indep_var('p', val=[7, -7])
        q = group.create_indep_var('q', val=[8, -8])
        r = p + q
        group.register_output('r', r)

        # add child system
        g.add_subsystem('R', group, promotes=['*'])
        # declare output of child system as input to parent system
        r = g.declare_input('r', shape=(2, ))

        c = g.declare_input('c', val=[18, -18])

        # a == (3 + a - 2 * a**2)**(1 / 4)
        with g.create_group('coeff_a') as group:
            a = group.create_output('a')
            a.define((3 + a - 2 * a**2)**(1 / 4))
            group.nonlinear_solver = NonlinearBlockGS(iprint=0, maxiter=100)

        # store positive and negative values of `a` in an array
        ap = g.declare_input('a')
        an = -ap
        a = g.create_output('vec_a', shape=(2, ))
        a[0] = ap
        a[1] = an

        with g.create_group('coeff_b') as group:
            group.create_indep_var('b', val=[-4, 4])

        b = g.declare_input('b', shape=(2, ))
        y = g.create_implicit_output('y', shape=(2, ))
        z = a * y**2 + b * y + c - r
        y.define_residual_bracketed(
            z,
            x1=[0, 2.],
            x2=[2, np.pi],
        )
Esempio n. 4
0
    def setup(self):
        group = Group()
        a = group.declare_input('a', val=2)
        b = group.create_indep_var('b', val=12)
        group.register_output('prod', a * b)
        self.add_subsystem('sys', group, promotes=['*'])

        # These expressions do not lead to constructing any Component
        # objects
        x1 = self.declare_input('x1')
        x2 = self.declare_input('x2')
        y1 = x2 + x1
        y2 = x2 - x1
        y3 = x1 * x2
        y5 = x2**2
Esempio n. 5
0
    def setup(self):
        # Create independent variable
        x1 = self.create_indep_var('x1', val=40)

        # Powers
        y4 = x1**2

        # Create subsystem that depends on previously created
        # independent variable
        subgroup = Group()

        # This value is overwritten by connection from the main group
        a = subgroup.declare_input('x1', val=2)
        b = subgroup.create_indep_var('x2', val=12)
        subgroup.register_output('prod', a * b)
        self.add_subsystem('subsystem', subgroup, promotes=['*'])

        # declare inputs with default values
        # This value is overwritten by connection
        # from the subgroup
        x2 = self.declare_input('x2', val=3)

        # Simple addition
        y1 = x2 + x1
        self.register_output('y1', y1)

        # Simple subtraction
        self.register_output('y2', x2 - x1)

        # Simple multitplication
        self.register_output('y3', x1 * x2)

        # Powers
        y5 = x2**2

        # register outputs in reverse order to how they are defined
        self.register_output('y5', y5)
        self.register_output('y6', y1 + y5)
        self.register_output('y4', y4)
Esempio n. 6
0
    def setup(self):
        # x == (3 + x - 2 * x**2)**(1 / 4)
        group = Group()
        x = group.create_output('x')
        x.define((3 + x - 2 * x**2)**(1 / 4))
        group.nonlinear_solver = NonlinearBlockGS(maxiter=100)
        self.add_subsystem('cycle_1', group)

        # x == ((x + 3 - x**4) / 2)**(1 / 4)
        group = Group()
        x = group.create_output('x')
        x.define(((x + 3 - x**4) / 2)**(1 / 4))
        group.nonlinear_solver = NonlinearBlockGS(maxiter=100)
        self.add_subsystem('cycle_2', group)

        # x == 0.5 * x
        group = Group()
        x = group.create_output('x')
        x.define(0.5 * x)
        group.nonlinear_solver = NonlinearBlockGS(maxiter=100)
        self.add_subsystem('cycle_3', group)
    def setup(self):
        g = self.group

        # define a subsystem (this is a very simple example)
        group = Group()
        p = group.create_indep_var('p', val=7)
        q = group.create_indep_var('q', val=8)
        r = p + q
        group.register_output('r', r)

        # add child system
        g.add_subsystem('R', group, promotes=['*'])
        # declare output of child system as input to parent system
        r = g.declare_input('r')

        c = g.declare_input('c', val=18)

        # a == (3 + a - 2 * a**2)**(1 / 4)
        group = Group()
        a = group.create_output('a')
        a.define((3 + a - 2 * a**2)**(1 / 4))
        group.nonlinear_solver = NonlinearBlockGS(iprint=0, maxiter=100)
        g.add_subsystem('coeff_a', group, promotes=['*'])

        a = g.declare_input('a')

        group = Group()
        group.create_indep_var('b', val=-4)
        g.add_subsystem('coeff_b', group, promotes=['*'])

        b = g.declare_input('b')
        y = g.create_implicit_output('y')
        z = a * y**2 + b * y + c - r
        y.define_residual(z)
        self.linear_solver = ScipyKrylov()
        self.nonlinear_solver = NewtonSolver(
            solve_subsystems=False,
            maxiter=100,
        )
        # a == (3 + a - 2 * a**2)**(1 / 4)
        group = Group()
        a = group.create_output('a')
        a.define((3 + a - 2 * a**2)**(1 / 4))
        group.nonlinear_solver = NonlinearBlockGS(iprint=0, maxiter=100)
        g.add_subsystem('coeff_a', group, promotes=['*'])

        a = g.declare_input('a')

        group = Group()
        group.create_indep_var('b', val=-4)
        g.add_subsystem('coeff_b', group, promotes=['*'])

        b = g.declare_input('b')
        y = g.create_implicit_output('y')
        z = a * y**2 + b * y + c - r
        y.define_residual(z)
        self.linear_solver = ScipyKrylov()
        self.nonlinear_solver = NewtonSolver(
            solve_subsystems=False,
            maxiter=100,
        )


prob = Problem()
prob.model = Group()
prob.model.add_subsystem('example', ExampleWithSubsystemsInternalN2(n2=True, ))
prob.setup(force_alloc_complex=True)
prob.run_model()