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

Generate code for Single pendulum example #2990

Open cd155 opened 2 years ago

cd155 commented 2 years ago

Currently, the single pendulum example didn't generate the code. I did a quick try, but it happened there were some errors.

For example, the sample variable pendDisplacementAngle end up in both inputs and outputs. The pendDisplacementAngle is the displacement angle of the pendulum, so it is a result we want to get, and it shouldn't be in inputs.

https://github.com/JacquesCarette/Drasil/blob/adfc15f13c7cf81101c1dbab97a6aa61f10a4ec6/code/drasil-example/sglpendulum/lib/Drasil/SglPendulum/Unitals.hs#L24-L28

Hi @smiths, I can't find the original SRS for the single pendulum by searching in the issue tracker. Do you know where I can find it?

I think the problem we haven't gotten the single pendulum example to generate code is because the SRS says the result is θ over time. We could make the result a list of θ by giving an initial value of θ_i. If we do, we have to define a time step.

On the other hand, should the θ(t) be a function that makes more sense? We discussed that the result of ODE solvers should be a function rather than a bunch of points. This sparks me to think the single pendulum example should also return a function.

cd155 commented 2 years ago

Finally, I found out #2907 needs to be done before we can generate the code for the single pendulum example.

JacquesCarette commented 2 years ago

I'm sure you're right, but I don't understand why details of a derivation would be needed to generate code?

Most definitely, the above lines are wrong, pendDisplacementAngle should not be in both input and output. And yes, that angle is a function of time.

smiths commented 2 years ago

@cd155 the original double pendulum example is here:

https://github.com/Zhang-Zhi-ZZ/CAS741Project/blob/master/Double%20Pendulum/docs/SRS/SRS.pdf

A student did it as part of my CAS 741 course. I had a look just now and noticed that the instance model shows theta_1 and theta_2 as input. To be unambiguous, the input should actually be theta_1(0) and theta_2(0) - the angles at time equals to zero. (I'm assuming that t = 0 at the start of the simulation. This is probably where the mistake comes from that you noticed. The input is the initial angle; the output is the angle as a function of time.

Olu took that example to make a double pendulum example in Drasil, and then she simplified that for the single pendulum example. As far as I know, a manual version of the single pendulum problem was never created.

I agree with @JacquesCarette that #2907 shouldn't be blocking anything. We don't need the derivation to generate code.

cd155 commented 2 years ago

@smiths @JacquesCarette thanks for pointing it out. After looking at the SRS again, #2907 shouldn't block the ticket. Here are a few things that need to be added to make single pendulum generate codes

  1. we need to define the time step. In ODE solver, the time step is integrated inside of libraries, so we just give the time step, and then it will start the iteration. In the single pendulum example, somehow defining the time step is inevitable.
  2. Connect the definition of calculation of angular frequency with angular frequency chuck (this part should be easy)
smiths commented 2 years ago

@cd155, it is fine to add the time step for the ODE solver. @JacquesCarette is correct that the "true" output is a function of time, but for practical reasons, the output is often given as a discretized version of that function. Let's get things working, and then we can work to improve them.

cd155 commented 2 years ago

@smiths One more question I have is that what differential equation we are going to solve? The simple pendulum, in the instance model, has

is it an ordinary differential equation? how to make it be θ' = something?

I realized the above ↑↑ equation is derived from

This is just a single second-order ode, so it would be fairly straightforward to solve this equation.

smiths commented 2 years ago

The first equation you give is the closed-form (or analytical) solution to the ODE. The second one is the ODE. (See https://www.acs.psu.edu/drussell/Demos/Pendulum/Pendulum.html)

The SRS is written with the closed-from solution. An ODE solver is not part of the requirements for this SRS. If you want to solve numerically, you would remove the instance model with the closed-form solution and make the ODE the instance model.

cd155 commented 2 years ago

Solving the single pendulum numerically by using ode solvers seems not very excited, and it is just another example like PDcontroller solving a second order ode.

If I want to generate code for an analytical solution, does Drasil ready for that? It seems to me this is different than other examples, we are not just giving the inputs, and getting the output. We actually have to define how to do iteration in Drasil, and how to collect data.

cd155 commented 2 years ago

A more straightforward way is to give a time, eg 30, in the input file, and then return a theta (a number).

smiths commented 2 years ago

Yes, Drasil is ready for analytical solutions. GlassBR is full of equations of this sort. The difference would be if you wanted to return a sequence of theta values. If you evaluate the function at one time, as you propose, this is really easy. It might be better to think about returning the angle at discrete time steps, like we do for some of the ODE solutions. It isn't as general as returning a function, but it is something that is often done for scientific programs.