JacquesCarette / Drasil

Generate all the things (focusing on research software)
https://jacquescarette.github.io/Drasil
BSD 2-Clause "Simplified" License
142 stars 26 forks source link

Teach Drasil about ODEs with events #2878

Open cd155 opened 2 years ago

cd155 commented 2 years ago

In the #2875 , we have the NewDEModel containing ODE internal data represent. One concept to can be added is ODE with events.

The NewDEModel doesn't know how to capture the knowledge of ODE with events, that's why SWHS still using DEModel.

For example, in the SWHS, it has multiple cases on the right-hand side.

In the current example, it uses completeCase to handle it, completeCase is a relation type.

htPCMRel = sy pcmE $= completeCase [case1, case2, case3]
  where ...
cd155 commented 2 years ago

To record the feedback about ODE with events from Dr.Smith

The IM:eBalanceOnPCM ODE has three cases because the PCM heats differently depending on whether it is solid, melting, or all liquid. The cases where it is all solid or all liquid are pretty easy to understand. The thermal related material properties change, but the form of the ODE is the same. The case that is different is when the PCM is melting. In this case dT_P/dt = 0 because all of the thermal energy that is added is going to melting the PCM. No energy is “left” for raising the temperature, so the temperature at the beginning of melting (the melting point) and at the end of melting is the same. Once all the PCM is melted, the added energy will go toward raising the temperature of the liquid PCM.

Cases like this are considered to be ODEs with events. When the event occurs the form of the ODE changes. A good overview of this is given here:

https://www.sciencedirect.com/science/article/pii/S0898122100000456

If we want to “teach” Drasil about ODEs with events, then we need to add the notion of an associated event function. In the paper I pointed to above, the event function is g(t, y). An event occurs at t when g(t, y(t*)) = 0.

The condition for the first “switch” is when T_P < T_melt. The function g in this case would be g = T_melt - T_P. (You can see this in the Matlab code for SWHS here: https://github.com/smiths/swhs/blob/master/src/Program/Matlab/event1.m).

It seems to me that teaching Drasil about events for ODEs wouldn’t be trivial. We have to introduce the function g as a parameter. Moreover, we need to consider cases where there is more than one event function (like SWHS). The direction that the condition occurs from is also relevant. We only heat the PCM, but in general, the ODE would be different if it is heating or cooling. You’ll also see that the “canonical” form of g is different from how people usually communicate equations. This means the “model" and the display of the model will be different.

We should definitely teach Drasil about ODEs with events, but I think for now, we should follow Jacques' suggestion of continuing to use “other” as the model type for SWHS. Drasil just doesn’t know enough yet to understand an ODE with events.

When we look seriously at ODEs with events, a simple abstraction is available in the MIS document for SWHS in the ODE Solver Module, which starts on page 13.

JacquesCarette commented 2 years ago

I agree, teaching Drasil about ODEs with events is beyond the scope of your work. Personally, I would actually change SWHS back to OthModel. This will let you delete DEModel. @smiths ?

smiths commented 2 years ago

Yes, I agree.

balacij commented 1 year ago

Out of curiosity, what is meant by "events"? I'm not quite sure I fully understand.

smiths commented 1 year ago

An event is when the ODE changes. A classic example is a bouncing ball. You drop the ball from a given height. The event occurs when the ball hits the ground. Once the ball hits the ground the initial conditions change because when the ball bounces up the velocity in the y-direction has changed direction. Another example is modelling the temperature in a room with a thermostat connected to a heat source. When the temperature gets too low, the heat source comes and the ODE changes. SWHS is another example. There is one ODE when the PCM is solid, another when it is melting, and another when it is fully melted. When there are events the ODE solver needs to determine when the event occurs. For instance, the solver needs to determine when the ball hits the ground.

A mathematical characterization of event location for ODEs can be found in Shampine and Thompson 1999. Matlab supports ODEs with events.