Skip to content

Commit

Permalink
Avoid code duplication: move it into parent Equation class
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-punko committed Oct 25, 2024
1 parent 933918f commit fcbec9d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 48 deletions.
27 changes: 27 additions & 0 deletions src/main/java/by/andd3dfx/math/pde/equation/Equation.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import by.andd3dfx.math.Interval;
import by.andd3dfx.math.Matrix;
import by.andd3dfx.math.pde.border.BorderCondition;
import by.andd3dfx.math.pde.border.BorderConditionType1;
import by.andd3dfx.math.pde.border.BorderConditionType2;
import by.andd3dfx.math.pde.border.BorderConditionType3;

/**
* Base class. Used to avoid code duplication in child classes
Expand Down Expand Up @@ -118,4 +121,28 @@ protected void progonka(double[] A, double[] B, double[] C, double[] F, double m
Y[i] = Alpha[i + 1] * Y[i + 1] + Beta[i + 1];
}
}

protected void useBorderConditions(double h, double[] Nu, double t, double[] Mu) {
if (leftBorderCondition instanceof BorderConditionType1 condition) {
Nu[1] = condition.gU(t);
} else if (leftBorderCondition instanceof BorderConditionType2 condition) {
Mu[1] = 1;
Nu[1] = -h * condition.gdU_dx(t);
} else if (leftBorderCondition instanceof BorderConditionType3 condition) {
var lh = condition.gH();
Mu[1] = 1 / (1 + h * lh);
Nu[1] = h * lh * condition.gTheta(t) / (1 + h * lh);
}

if (rightBorderCondition instanceof BorderConditionType1 condition) {
Nu[2] = condition.gU(t);
} else if (rightBorderCondition instanceof BorderConditionType2 condition) {
Mu[2] = 1;
Nu[2] = h * condition.gdU_dx(t);
} else if (rightBorderCondition instanceof BorderConditionType3 condition) {
var rh = condition.gH();
Mu[2] = 1 / (1 - h * rh);
Nu[2] = -h * rh * condition.gTheta(t) / (1 - h * rh);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package by.andd3dfx.math.pde.equation;

import by.andd3dfx.math.pde.border.BorderCondition;
import by.andd3dfx.math.pde.border.BorderConditionType1;
import by.andd3dfx.math.pde.border.BorderConditionType2;
import by.andd3dfx.math.pde.border.BorderConditionType3;

/**
* Hyperbolic equation (described oscillation processes):
Expand Down Expand Up @@ -95,27 +92,7 @@ public Solution solve(double h, double tau) {
double[] Nu = new double[3];
double t = area.t().x(nj);

if (leftBorderCondition instanceof BorderConditionType1 condition) {
Nu[1] = condition.gU(t);
} else if (leftBorderCondition instanceof BorderConditionType2 condition) {
Mu[1] = 1;
Nu[1] = -h * condition.gdU_dx(t);
} else if (leftBorderCondition instanceof BorderConditionType3 condition) {
var lh = condition.gH();
Mu[1] = 1 / (1 + h * lh);
Nu[1] = h * lh * condition.gTheta(t) / (1 + h * lh);
}

if (rightBorderCondition instanceof BorderConditionType1 condition) {
Nu[2] = condition.gU(t);
} else if (rightBorderCondition instanceof BorderConditionType2 condition) {
Mu[2] = 1;
Nu[2] = h * condition.gdU_dx(t);
} else if (rightBorderCondition instanceof BorderConditionType3 condition) {
var rh = condition.gH();
Mu[2] = 1 / (1 - h * rh);
Nu[2] = -h * rh * condition.gTheta(t) / (1 - h * rh);
}
useBorderConditions(h, Nu, t, Mu);

progonka(A, B, C, F, Mu[1], Nu[1], Mu[2], Nu[2], U);
solution.set(nj, U);
Expand Down
26 changes: 2 additions & 24 deletions src/main/java/by/andd3dfx/math/pde/equation/ParabolicEquation.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package by.andd3dfx.math.pde.equation;

import by.andd3dfx.math.pde.border.BorderCondition;
import by.andd3dfx.math.pde.border.BorderConditionType1;
import by.andd3dfx.math.pde.border.BorderConditionType2;
import by.andd3dfx.math.pde.border.BorderConditionType3;

/**
* Parabolic equation (described heat/mass transfer):
Expand Down Expand Up @@ -75,31 +72,12 @@ public Solution solve(double h, double tau) {
double[] Nu = new double[3];
double t = area.t().x(nj);

if (leftBorderCondition instanceof BorderConditionType1 condition) {
Nu[1] = condition.gU(t);
} else if (leftBorderCondition instanceof BorderConditionType2 condition) {
Mu[1] = 1;
Nu[1] = -h * condition.gdU_dx(t);
} else if (leftBorderCondition instanceof BorderConditionType3 condition) {
var lh = condition.gH();
Mu[1] = 1 / (1 + h * lh);
Nu[1] = h * lh * condition.gTheta(t) / (1 + h * lh);
}

if (rightBorderCondition instanceof BorderConditionType1 condition) {
Nu[2] = condition.gU(t);
} else if (rightBorderCondition instanceof BorderConditionType2 condition) {
Mu[2] = 1;
Nu[2] = h * condition.gdU_dx(t);
} else if (rightBorderCondition instanceof BorderConditionType3 condition) {
var rh = condition.gH();
Mu[2] = 1 / (1 - h * rh);
Nu[2] = -h * rh * condition.gTheta(t) / (1 - h * rh);
}
useBorderConditions(h, Nu, t, Mu);

progonka(A, B, C, F, Mu[1], Nu[1], Mu[2], Nu[2], U);
solution.set(nj, U);
}
return new Solution(this, solution);
}

}

0 comments on commit fcbec9d

Please sign in to comment.