Troubleshooting
#
I made a model, but when I run my program, nothing happens.Assuming you created a class that inherits from Model
like so,
from csdl import Modelimport csdlimport numpy as np
# This is nothing more than a class definition.class Example(Model): def define(self): theta = self.declare_variable('theta', val=np.pi/4) x = csdl.cos(theta) y = csdl.sin(theta) self.register_output('x', x) self.register_output('y', y)
# After the class is defined, nothing happens.
then there is no way to execute the simulation because you did not issue a command to the compiler to generate a simulation implementation or run a simulation. In order to generate a simulation implementation, use
from backend import Simulator
# Generate an implementation.sim = Simulator(Example())# Run simulation.sim.run()# Access valuesprint(sim['x'])print(sim['y'])
Note that the Simulator
constructor calls the Model.define()
method automatically.
#
Why can't I print variable values?The Python print
function will print information about the CSDL
Variable
object, but not run time information.
This is because calling print
within Model.define
is a compile
time operation, not a run time operation.
To print variables at run time, the Model.print_var
method is
provided, which prints the CSDL variable name, shape, and value during
each simulation run.
#
I get a shape mismatch error, but my shapes matchCSDL variables do not support broadcasting.
Suppose you have a variable a
with shape (2, 1)
, and
b = self.create_output('b', shape=(3,))b[:1] = a
then this won't work because b[:1]
has shape (2,)
and a
has shape
(2,1)
.
Instead, use
b = self.create_output('b', shape=(3,))b[:1] = csdl.reshape(a, (2,))
For more complicated cases, you will need to use einsum.
#
I get an "Output not defined" error, but I defined my output.Either you forgot to define a value to your output using indexed assignment, or you made your variable reference a new object, neglecting the original output you created.
# create an outputx = self.create_output('x')# do some stuff without defining x...# bind x to a new object returned by f; now there is no reference to# the original outputx = f(y)# register output, which is not the same as the output returned by# previous call to create_outputself.register_output('x', x)
Also remember that you do not need to register/rename the output using a
call to register_output
.
x = self.create_output('x', shape=(2,))x[0] = f(y)x[1] = g(z)
#
How to deal with errors in the back endThe back end may emit error messages that look extremely unhelpful when
using CSDL.
In some cases, the back end will say there's an issue with a variable
called '_000H'
, which is a name that CSDL automatically generates for
outputs that the user does not register explicitly.
There may be a class that the back end uses with a similar name to a
standatd library function, or an operation with an automatically
generated name.
To debug issues like this, register outputs in your model that are most
likely to be inputs and outputs of the operations that the back end uses
to generate code.
This will not remove the error messages, but that '_000H'
will change
to the name you set for your output.
Eventually, you should be able to find the source of the bug.
This does involve some trial end error.
There are a few reasons you've encountered this error
- You used CSDL incorrectly (unlikely if the error is coming from the back end).
- There is a bug in the CSDL compiler back end (more likely than the first option).
- The CSDL compiler back end interacts with
csdl
incorrectly (more likely as the number of back ends increases).
If you're sure the issue is the last one, or if you've spent a long time trying to understand the bug and you've gotten nowhere, please file an issue on the GitHub issue tracker.