Open RuruX opened 2 years ago
You will need to call csdl.custom
from within a Model
subclass and construct a Simulator
from that Model
. You cannot construct a Simulator
from anything other than a Model
. Consider the following:
from csdl import Model
from csdl_om import Simulator
class M(Model):
def define(self):
a = self.declare_variable('a', val=1.)
b = self.declare_variable('b', val=-4.)
c = self.declare_variable('c', val=3.)
x = csdl.custom(a, b, c, op=ExampleImplicitSimple())
self.register_output('x', x)
sim = Simulator(M())
sim.run()
print(sim['x'])
This produces the following output:
======================
custom_implict_op_0003
======================
NL: Newton Converged in 5 iterations
[1.]
A few things to note here:
declare_variable
will overwrite the values in ExampleImplicitSimple
.csdl.custom
must share the same name as the inputs added using add_input
. They must also be in the same order as the calls to add_input
. For example, if you were to switch a
and b
such that csdl.custom(b, a, c, op=ExampleImplicitSimple())
, you would get different behavior, and an error if a
and b
are different shapes. In the future, the names will not need to match the names used within ExampleImplicitSimple
.csdl.custom
is not a Model
method, it cannot register outputs automatically. Just like any other standard library function, the output must be registered. Again, for now, the name must match the name defined in add_output
, so self.register_output('y', x)
would not produce the right behavior.Model.create_implicit_operation
when defining implicit relationships with a Model
subclass, the ExampleImplicitSimple
class needs to set its solver within the define
method, unless you instantiate it outside the call to csdl.custom
like so:e = ExampleImplicitSimple()
e.linear_solver = ScipyKrylov()
e.nonlinear_solver = NewtonSolver(solve_subsystems=False)
x = csdl.custom(a, b, c, op=e)
The reason I mention this is that you may want to leave the solvers out of the class definition until the user of your custom operation decides to use it, and you can leave it up to the user to decide what solvers to use.
I added the documentation label because this is something I have planned to document, but haven't yet gotten around to doing so.
Thank you so much for your explanation! It has exactly solved my problem. And looking forward to seeing this represented in the documentation.
Since I've labeled this a documentation issue and this hasn't been documented, I'm reopening it.
Hi Victor,
I was trying to build a
CustomImplicitOperation
object corresponding to the OpenMDAOImplicitComponent
class. I assume they are using the same methodology except for the syntax differences.However, I was having trouble to simulate the model using
csdl_om
, that the error popped up saying "TypeError: CSDL-OM only accepts CSDL Model specifications to construct a Simulator." Here is the complete error message when running theex_custom.py
in the CSDL repository, with simulator defined with it.And the complete code example I'm using is as below.
It seems that the
CustomImplicitOperation
object is not recognized by the simulator as a valid input, which should be amodel
object (here). Or it could also be me using the simulator wrong. Please let me know what you think.Best, Ru
Meta
Here are the versions of
CSDL
andCSDL-OM
I was using, which are both the latest as of the issue report.Backtrace
```
```