Expressing Cyclic Relationships with Explicit Outputs
We can also define a value in terms of itself. In the example below, consider three fixed point iterations. Each of these fixed point iterations is declared within its own subsystem so that a solver may be assigned to it. Note that variables are not rpomoted so that we can use the same name to refer to different variables, depending on which subsystem they belong to.
from csdl_om import Simulatorfrom csdl import Model, ScipyKrylov, NewtonSolver, NonlinearBlockGSimport numpy as np
class ExampleFixedPointIteration(Model):
def define(self): # x == (3 + x - 2 * x**2)**(1 / 4) m1 = Model() x = m1.declare_variable('a') r = m1.register_output('r', x - (3 + x - 2 * x**2)**(1 / 4))
# x == ((x + 3 - x**4) / 2)**(1 / 4) m2 = Model() x = m2.declare_variable('b') r = m2.register_output('r', x - ((x + 3 - x**4) / 2)**(1 / 4))
# x == 0.5 * x m3 = Model() x = m3.declare_variable('c') r = m3.register_output('r', x - 0.5 * x)
solve_fixed_point_iteration1 = self.create_implicit_operation( m1) solve_fixed_point_iteration1.declare_state('a', residual='r') solve_fixed_point_iteration1.nonlinear_solver = NonlinearBlockGS( maxiter=100) a = solve_fixed_point_iteration1()
solve_fixed_point_iteration2 = self.create_implicit_operation( m2) solve_fixed_point_iteration2.declare_state('b', residual='r') solve_fixed_point_iteration2.nonlinear_solver = NonlinearBlockGS( maxiter=100) b = solve_fixed_point_iteration2()
solve_fixed_point_iteration3 = self.create_implicit_operation( m3) solve_fixed_point_iteration3.declare_state('c', residual='r') solve_fixed_point_iteration3.nonlinear_solver = NonlinearBlockGS( maxiter=100) c = solve_fixed_point_iteration3()
sim = Simulator(ExampleFixedPointIteration())sim.run()
print('a', sim['a'].shape)print(sim['a'])print('b', sim['b'].shape)print(sim['b'])print('c', sim['c'].shape)print(sim['c'])
======================Model_implicit_op_00au======================NL: NLBGS Converged in 45 iterations
======================Model_implicit_op_00ay======================NL: NLBGS Converged in 24 iterations
======================Model_implicit_op_00aB======================NL: NLBGS Converged in 34 iterationsa (1,)[1.12412303]b (1,)[1.07989607]c (1,)[5.82076609e-11]