Write the output from ODE solve to a file

Hi All,

I’d like to know how to write the output from solve to a file. I’d prefer to write the solution sol to a
text or .mat file.

prob= ODEProblem(ODEFunction(fun, sparsity=sparsity,x0,(0.0,5.0))
@btime sol = solve($prob,QNDF())

Suggestions will be really appreciated.

1 Like

Saving the solution as a BSON or CSV is probably the most convenient:
if you really want a plain txt file, you could do so with
I/O and Network · The Julia Language and a loop over sol.


CSV is plain text. You can use dlmwrite from the DelimitedFiles standard library or, for greater performance, CSV.jl.

The MAT.jl package can write Matlab files, if you need that, but I wouldn’t recommend using that format except for interchange with Matlab. More generally, you can use HDF5.jl as a binary interchange format.


@frankschae @stevengj

prob= ODEProblem(ODEFunction(fun, sparsity=sparsity,x0,(0.0,5.0))
@btime sol = solve($prob,QNDF())
df = DataFrame(sol)

I tried the above. I’m not sure what’s the right way to access sol in the output @btime sol = solve($prob,QNDF()) .
I get the following error in the line df = DataFrame(sol)

ERROR: LoadError: UndefVarError: sol not defined

Suggestions on the right way to access the data present in the variable sol will be really helpful.

I think your code would work by putting the @btime macro to the rhs

sol = @btime solve($prob,QNDF())

However, if you have benchmarked the solvers already and if you only want to store the solution of the ODE solve and output a rough timing at the same time, then you could also use @time :

sol = @time solve(prob,QNDF())

which won’t run the solve call several times.