0

I use C++ and odeint to solve a set of differential equations. I compile the code in Matlab using mex and g++ on Mac OS X. For some time everything worked perfectly, but now something curious is happening:

I can run the same simulation (with the same parameters) twice and in one run the results are fine and in the other a whole column (or multiple columns) of the output is NaN (this also influences the other outputs hinting at a problem occurring during the integration process).

I tried switching between various solvers. Now I am using runge_kutta_fehlberg78 with a fixed step size. (One possibility for NaN output is that the initial step size of an adaptive solver is too large.)

What can be the cause of this? Especially the randomness makes me wonder.

Edit: I am starting to suspect that the problem has something to do with Matlab. I compiled a version without the Matlab interface with Xcode as a normal executable and so far I didn't have a issue with NaN results. Its hard to tell if the problem is truly resolved and I don't understand why that would resolve it.

simmmons
  • 211
  • 1
  • 4
  • 1
    How much of the the code did you write? Did you do any memory allocation? This behavior makes think you could allocating/accessing memory improperly. – horchler May 28 '15 at 15:15
  • I wrote all of the code myself. As for the memory allocation, I just use standard types, such as ublas::vector, std::list etc. and didn't handle memory allocation myself. – simmmons May 28 '15 at 15:40
  • 1
    Yes, it is nearly impoosible to answer this question without further details. Indeed, the randomness suggest some problems with memory management. How is your setup? Do you call the c++ code from matlab and pass "matlab vectors" for the state to it? – headmyshoulder May 28 '15 at 21:15
  • Just let me know what further information you need. Compiling it as a standalone executable that saves the results to a file works fine. I pass only a few scalars from Matlab to the C++ code and then a matrix (time vs variables) back to Matlab. The matrix is allocated after the ODE system is solved. Saving to text yields the same result as passing the results back to Matlab. I even switched compilers from gcc to clang with no effect. – simmmons May 29 '15 at 00:32
  • 1
    Do you have code for this? Maybe you can put it online on gist.github.com – headmyshoulder May 29 '15 at 05:54
  • 1
    The code is quite large. It will take a little time to analyze it. – headmyshoulder May 29 '15 at 22:28
  • Thanks for taking the time. – simmmons May 30 '15 at 00:58
  • 1
    Hi, I had a brief look and could not find anything problematic. Do you use mexFunction correctly from matlab (correct number of arguments, correct arguments, etc.) ? – headmyshoulder Jun 01 '15 at 07:37
  • Hi, thanks. Yes I'm passing the correct number and types of argument. Even if I wouldn't could that be a cause for unpredictable results? I need to find a way to debug when calling the function from matlab. Were you able to run the code from Matlab? – simmmons Jun 01 '15 at 14:15
  • No, unfortunately not. I don't have matlab here. But I run your main with valgrind and it reports several problems. Maybe this might be a hint for further analysis. – headmyshoulder Jun 01 '15 at 16:50
  • Thanks, I'll look into that. – simmmons Jun 01 '15 at 20:25

0 Answers0