FAQ

Meta

Q: What makes CasADi different from other CAS systems?

CasADi makes no attempt to be a complete CAS in terms of the types of operations supported. There is only a limited support for operations such as symbolic simplifications, integrals, limits etc. On the other hand, CasADi contains state-of-the-art implementations of several flavors of automatic differentiation and uses a memory efficient way of representing symbolic expressions, inspired from this field. This makes it possible to manipulate very large expressions that conventional computer algebra systems fail to handle. This includes expressions defined by recursions that cannot be simplified, for example:

```from casadi import *
x=SX.sym("x")
y=x
for i in range(100):
y = sin(y)*y```

In CasADi, this code executes in the blink of a second, whereas conventional computer algebra systems fail to construct the expression altogether. For comparison, test for example:

• In Matlab using the Symbolic Math Toolbox:
```x=sym('x');
y=x;
for i=1:100
y = sin(y)*y;
end
```
• In Maple (version 15):
``````y := x;
for k from 1 to 100 do
y := sin(y)*y;
end do;
``````
• In Sympy (version ??):
```from sympy import *
x=symbols("x")
y=x
for i in range(100):
y = sin(y)*y```
• In Maxima:
``````x: 'x; y: x; for i from 1 thru 100 do ((y: sin(y)*y)); end;
``````

Other important features are the support for symbolic sparse matrices and highly efficient virtual machines for numerically evaluating functions defined by expression graphs made up by scalar valued operations (as above) or sparse matrix valued operations.

Installing

**Q: The build system is not able locate DlsMat or experiences linking problems with some IDA-libraries **

A: Make sure that you are using Sundials 2.4, the latest version at the time of writing.

Q: How do I point the build system to another flavour of Python?

A: introduce a "SET(CMAKE_USE_PYTHON_VERSION myversionnumber)" into the main CMakeLists.txt file.

Background: the SWIG build system relies on !PythonLibs (PYTHONLIBS_FOUND, PYTHON_LIBRARIES, PYTHON_INCLUDE_PATH) and !PythonInterp (PYTHONINTERP_FOUND, PYTHON_EXECUTABLE).

Q: On Windows, using CMake, I get an error:

``````Error running link command: The parameter is incorrect
NMAKE : fatal error U1077: '"C:\Program Files (x86)\CMake 2.8\bin\cmake.exe"' :return code '0x2'
``````

A: The NMake approach is not recommended. Try the first approach.

Q: Whenever I attempt to use the Python interface, I get an error:

``````ImportError: /usr/lib/libcoinhsl.so.0: undefined symbol: metis_nodend_
``````
• `ImportError: libmpichcxx.so.2: cannot open shared object file: No such file or directory` ** `sudo apt-get install libmpich2-dev`

Using

Q: Help! I got segmentation fault. What do do?

A: First find out where the segmentation fault took place, for example using gdb (here it helps if you've built CasADi in debug mode!). gdb works from the command line and you can also use it for Python scripts:

``````gdb python
run example_name.py
bt
``````

Another excellent debugging tool in Linux is Valgrind. Valgrind, as gdb, can be used through Python (though in this case you might need to suppress some errors).

``````valgrind python example_name.py
``````

If you have compiled CasADi using either Visual C++ or Eclipse, use the debugging facilities available in these tools.

If the segmentation fault took place inside CasADi, please notify the developers.

**Q: My attempts to use KINSol solver always result in "The linear solver's setup function failed in an unrecoverable manner." **

A: This could indicate that you should provide (better) initial conditions. Use `output(0)` to set them.

Q: I want to evaluate a function/integrate/solve for different numerical arguments and collect output as I go, but I end up with all the output being identical

A: Note that copies in Python are shallow by default and fx.output() gives a reference/pointer to an internal data structure. So if you want save fx.output(), you need to make a deep copy using for example DMatrix(fx.output()) before doing the next evaluation.

Q: ExternalFunction doesn't find my library, though it is in the working directory

A: On linux, try prefixing the library name with "./"

Q: In using the IdasIntegrator, I get error `Module "IDASolve" returned flag -3 ("IDA_ERR_FAIL"). Consult Idas documentation.`

A: Consult Idas documentation. (Tip: Have you made sure that the DAE Jacobian is non-singular, i.e. index-1?)

Q: In using the IdasIntegrator, I get an error `Module "IDACalcIC" returned flag -14 ("IDA_NO_RECOVERY"). Consult Idas documentation.`

A: Consult Idas documentation. (Tip: Have you made sure that the DAE Jacobian is non-singular, i.e. index-1?)

Q: In using IpoptSolver with MA57 linear_solver, I get the following error:

``````Tried to obtain MA57 from shared library "libhsl.so", but the following error occured:
/usr/lib/libhsl.so: undefined symbol: dgemm_
``````

A: Verify that the symbol is indeed missing: `strings /usr/lib/libhsl.so | grep dgemm_`.

Q: I compiled swig using WITH_SWIG_SPLIT, and I'm seeing strange behaviour.

A: The SWIG manual warns about the use of static libraries. Set ENABLE_SHARED to ON if you haven't already.

``````terminate called after throwing an instance of 'swig::stop_iteration'
``````

A: We don't know how to resolve this error. Fall back to WITH_SWIG_SPLIT=OFF.

Q: The derivatives I get out of CasADi are wrong. `ipoptsolver.setOption('derivative_check','first-order')` reveals this.

A: There are some non-CasADi related reasons for sensitivities going wrong:

• #320 - The sundials interface might require setting `fsens_abstol`, `fsens_reltol` and `fsens_err_con` to non-default values in some special cases.

While we give priority to fixing bugs dealing with sensitivity and we build in regression tests, it is not impossible to find a genuine bug. Please report this.

A: Right, that's a genuine bug. Please do notify us to make CasADi better.

Q: I am a CasADi developer. How do I fix the above error?

A: The most important thing is to get a stacktrace, so you can see where things went wrong. For C++ code, just use gdb. For python code, first put `CasadiOptions.setCatchErrorsPython(False)` somewhere high in the python code. The error will now crash the python interface, and you will be able to use gdb to get a stacktrace.

Q: I'm seeing 'Conversion to double from casadi.SX is not possible.' in Matlab A: This typically happens when you are pre-allocating a numeric array `X=zeros(n,m)`, and then assigning CasADi symbols to it `X(1)=casadi_object`. Either use `X=SX.zeros(n,m)`, or pre-allocate a cell `X=cell(n,m)` and apply `casadi.blockcat(X)` when you are finished with assigning.

How do I ...

... get debugging information?

A: Use option `verbose` and option `monitor`.

`f.setOption("verbose",True)`
`f.setOption("monitor",["eval_f"])`

In the Ipopt interface, for example, `addMonitor` allows you to monitor:

• `eval_f`
• `eval_grad_f`
• `eval_jac_g`

The API docs provides a table with available monitors for each function. Monitors are added to the source code ad-hoc when the need arises.

A: Use `printme` to inspect the numerical value of symbolic variables when they are evaluated.

A: Use graphdrawing to see a graphical representation of an SX or MX graph.

... get a scalar from DMatrix?

A: You can always use toScalar() member function In python, the float() global function works as well. In normal circumstances, you don't need this in python. The 1x1 DMatrix will be interpreted as float when required. Notify the developers if you have a non-working use case. In octave, you may need to resort to toScalar() for now.

... plot intermediate results of an nlp on the screen interactively

A: You need to use the Python interface. First follow these instructions.

Some python snippets you may need:

```import matplotlib
matplotlib.interactive(True)

ion()```
``````axis.relim()
axis.autoscale_view()
``````
``````lines.set_ydata
``````

Strangely, animated plotting does not seem to work in Spyder.

... capture the output (stdout) generated from casadi in python

A: `casadi.tools.io` provides functionality to do this

The following context manager can be used to capture stdout generated in python:

```with capture_stdout() as out:
print "foo"```
Clone this wiki locally
You can’t perform that action at this time.
Press h to open a hovercard with more details.