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:
https://diffeq.sciml.ai/latest/features/io/#io
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.

3 Likes

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.

3 Likes

@frankschae @stevengj

prob= ODEProblem(ODEFunction(fun, sparsity=sparsity,x0,(0.0,5.0))
@btime sol = solve($prob,QNDF())
df = DataFrame(sol)
println(df)
CSV.write("out.csv",df)

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
Stacktrace:

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.