Java Programming, trying to make an ODE solver library (New to object oriented coding!)

StackOverflow https://stackoverflow.com/questions/7693357

  •  07-02-2021
  •  | 
  •  

Question

I'm trying to program an ODE solver (similar to ODE45 in MATLAB). I want this solver to be "stand alone", ie I can pass it the name of the ODE I want it to solve, and it runs the code for that specific Differential equation.

So far I have my main class (where the bulk of the program is) and a class called ODEsolver (where the ODE solver is located). I want to be able to make a call from my main class to ODEsolver, and have ODE look at a method which is referenced by a String arguement (the name of the ODE I want to solve)

The way I envision it is:

public class Main {
  double y[];
  double x0,xf,y0;
  x0 = 0;
  xf = 10;
  y0 = 1;

  ODEsolver ode1 = new ODEsolver("name_of_ode_to_be_solved");

  y = ode1.ODE45(x0,xf,y0);


}

where ODE45 is the ODE solver in the ODEsolver class.

However I do not know how I would use the "name_of_ode_to_be_solved" to create a call to that method (the method is not in ODEsolver, it's either in main or some other class)

Thanks!

Was it helpful?

Solution

What you probably want to do is create an enum called ODE_TYPE then pass this to a Factory which will create a solver of that type. Something like this:

ODESolver solver = SolverFactory.Create(ODE_TYPE.FIRST_ORDER_LINEAR);

In this case ODESolver will be something called an Interface. You will then create sevaral concrete classes that implement this interface. One for each element in your enum.

I used something similar for my C++ DE solver fdtl.

It might also be the case that you can't generalize the construction enough to use the factory pattern. In this case you could still benefit from using an Interface you'd just create the concrete implementation directly. Like so,

ODESolver solver = new ODE45(x0, xf, y0);

you would then call

solver.solve();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top