Skip to content

Commit

Permalink
for #3410
Browse files Browse the repository at this point in the history
  • Loading branch information
hqnghi88 committed May 8, 2022
1 parent 478c5fe commit aabf346
Showing 1 changed file with 37 additions and 22 deletions.
Expand Up @@ -27,6 +27,7 @@
import msi.gama.util.IMap;
import msi.gaml.expressions.IExpression;
import msi.gaml.operators.Cast;
import msi.gaml.operators.Maths;
import ummisco.gama.dev.utils.DEBUG;
import ummisco.gaml.extensions.maths.ode.statements.SystemOfEquationsStatement;

Expand All @@ -37,16 +38,15 @@ public abstract class Solver {

/** The integrator. */
final FirstOrderIntegrator integrator;

/** The count. */
int count;

Double lastT = -999d;
// final double step;

/**
* Instantiates a new solver.
*
* @param step the step
* @param integrator the integrator
* @param step the step
* @param integrator the integrator
* @param integrated_val the integrated val
*/
Solver(final double step, final FirstOrderIntegrator integrator, final IMap<String, IList<Double>> integrated_val) {
Expand All @@ -56,14 +56,22 @@ public abstract class Solver {
integrator.addStepHandler(new StepHandler() {

@Override
public void init(final double t0, final double[] y0, final double t) {}
public void init(final double t0, final double[] y0, final double t) {
}

@Override
public void handleStep(final StepInterpolator interpolator, final boolean isLast) {
final double time = interpolator.getCurrentTime();
final double[] y = interpolator.getInterpolatedState();
count++;
storeValues(time, y, integrated_val);

if (lastT < 0) {
storeIntegratedValues(time, y, integrated_val);
} else {
if (Maths.abs(lastT - time) > 0.01) {
storeIntegratedValues(time, y, integrated_val);
}
}
lastT = time;
}
});
}
Expand All @@ -73,10 +81,10 @@ public void handleStep(final StepInterpolator interpolator, final boolean isLast
/**
* Solve.
*
* @param scope the scope
* @param seq the seq
* @param initialTime the initial time
* @param finalTime the final time
* @param scope the scope
* @param seq the seq
* @param initialTime the initial time
* @param finalTime the final time
* @param integrationValues the integration values
*/
// of equations;
Expand All @@ -86,12 +94,13 @@ public void solve(final IScope scope, final SystemOfEquationsStatement seq, fina
seq.executeInScope(scope, () -> {
final Map<Integer, IAgent> equationAgents = seq.getEquationAgents(scope);

// GamaMap<Integer, GamaPair<IAgent, SingleEquationStatement>> myEQ = seq.getEquations(scope.getAgent());
// GamaMap<Integer, GamaPair<IAgent, SingleEquationStatement>> myEQ =
// seq.getEquations(scope.getAgent());
final IMap<Integer, GamaPair<IAgent, IExpression>> myVar = seq.getVariableDiff(scope.getAgent());
/*
* prepare initial value of variables 1. loop through variables expression 2. if its equaAgents != null, it
* mean variable of external equation, set current scope to this agent scope 3. get value 4. return to
* previous scope
* prepare initial value of variables 1. loop through variables expression 2. if
* its equaAgents != null, it mean variable of external equation, set current
* scope to this agent scope 3. get value 4. return to previous scope
*/

final double[] y = new double[myVar.size()];
Expand All @@ -117,7 +126,9 @@ public void solve(final IScope scope, final SystemOfEquationsStatement seq, fina
} catch (final Exception ex1) {
DEBUG.OUT(ex1.getMessage());
} finally {
if (pushed) { scope.pop(a); }
if (pushed) {
scope.pop(a);
}
}
}

Expand All @@ -127,7 +138,9 @@ public void solve(final IScope scope, final SystemOfEquationsStatement seq, fina
GamaListFactory.create(Double.class));
}

if (scope.getClock().getCycle() == 0) { storeValues(initialTime, y, integrationValues); }
if (scope.getClock().getCycle() == 0) {
storeIntegratedValues(initialTime, y, integrationValues);
}
if (y.length > 0) {
try {
integrator.integrate(seq, initialTime, y, finalTime, y);
Expand All @@ -145,16 +158,18 @@ public void solve(final IScope scope, final SystemOfEquationsStatement seq, fina
/**
* Store values.
*
* @param time the time
* @param y the y
* @param time the time
* @param y the y
* @param integrationValues the integration values
*/
void storeValues(final double time, final double[] y, final IMap<String, IList<Double>> integrationValues) {
void storeIntegratedValues(final double time, final double[] y,
final IMap<String, IList<Double>> integrationValues) {
if (integrationValues != null) {
integrationValues.valueAt(y.length).add(time);

for (int i = 0; i < y.length; i++) {
integrationValues.valueAt(i).add(y[i]);
}
integrationValues.valueAt(y.length).add(time);
}

}
Expand Down

0 comments on commit aabf346

Please sign in to comment.