princemahajan / FLINT

Fortran Library for numerical INTegration of differential equations
https://princemahajan.github.io/FLINT/
Apache License 2.0
43 stars 9 forks source link

Do I have to do init every time before solving a ODE? #9

Closed CRquantum closed 2 years ago

CRquantum commented 2 years ago

Hi @princemahajan ,

I am using your nice code and I continue to understand more of your nice code! I just have a question, not an issue.

I need to solve basically the same ODEs many many times, say 10^5 times. The ODE can be, for simplicity,

YP(1) = AAA*Y(1)

So I can define this YP(1) = AAA*Y(1) as F in an Object say, testSysObj, which is of the type of extends(DiffEqSys).

Now, it is just that, the parameter AAA is changed each time. So you can say I actually solve 10^5 times different ODEs. But actually the ODE are basically the same, just the parameter AAA changed.

So my question is, each time AAA changes, so do I have to do init first, then integrate it? If so, will that init each time, slow down the code?

Currently I know it will be like below,

call testSysInit(testSysObj) ! give a new value of AAA in testSysObj.

call erkvar%Init(testSysObj, MAX_STEPS, Method=ERK_DOP853, ATol=[atol], RTol=[rtol],&
        InterpOn=.FALSE., EventsOn=.FALSE. )  ! init

call erkvar%Integrate(x0_test, y0_test, xf_test, yf_test, StepSz=stepsz, UseConstStepSz=CONST_STEPSZ, &
            IntStepsOn=.TRUE.,Xint = Xint, Yint = Yint, &
            EventStates=EventStates, EventMask = EvMask,StiffTest=stiffstatus)   ! integrate

Do you think it is possible to directly put the object testSysObj in the argument, and do integration like below

call erkvar%Integrate(testSysObj, x0_test, y0_test, xf_test, yf_test, StepSz=stepsz, UseConstStepSz=CONST_STEPSZ, &
            IntStepsOn=.TRUE.,Xint = Xint, Yint = Yint, &
            EventStates=EventStates, EventMask = EvMask,StiffTest=stiffstatus)   ! integrate

and it may speedup the code a little bit? Or, is it that the init stage actually is very cheap it does not really matter?

Thank you very much!

Best regards, Rong

princemahajan commented 2 years ago

I dont think you need to call the Init function again if a parameter is changed. Just change the parameter using the DiffEqSys object you have and call integrate again.

princemahajan commented 2 years ago

so you can do something like this:

testSysObj%AAA = <new_value>
call erkvar%Integrate(x0_test, y0_test, xf_test, yf_test, StepSz=stepsz, UseConstStepSz=CONST_STEPSZ, &
            IntStepsOn=.TRUE.,Xint = Xint, Yint = Yint, &
            EventStates=EventStates, EventMask = EvMask,StiffTest=stiffstatus)
CRquantum commented 2 years ago

Great thank you!