Skip to content

Commit

Permalink
Add methods xx/xn/xi/tx/tn/ti to Area class
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-punko committed Nov 2, 2024
1 parent 750f116 commit 8f96104
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 27 deletions.
54 changes: 54 additions & 0 deletions src/main/java/by/andd3dfx/math/Area.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,33 @@ public double tRight() {
return t().right();
}

/**
* Amount of time interval steps
*/
public int tn() {
return t().n();
}

/**
* Time that corresponds to the specified index
*
* @param i index of time interval
* @return time
*/
public double tx(int i) {
return t().x(i);
}

/**
* Index that corresponds to the specified time
*
* @param time time coordinate
* @return index of time interval
*/
public int ti(double time) {
return t().i(time);
}

/**
* Left border of space interval
*/
Expand All @@ -35,4 +62,31 @@ public double xLeft() {
public double xRight() {
return x().right();
}

/**
* Amount of space interval steps
*/
public int xn() {
return x().n();
}

/**
* Coordinate that corresponds to the specified index
*
* @param i index of space interval
* @return space coordinate
*/
public double xx(int i) {
return x().x(i);
}

/**
* Index that corresponds to the specified coordinate
*
* @param x space coordinate
* @return index of space interval
*/
public int xi(double x) {
return x().i(x);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ protected Area buildArea(HyperbolicEquation eqn, double h, double tau) {
*/
protected Matrix prepare(Equation eqn, Area area) {
// Create space for equation solution
var matrix = new Matrix(area.t().n() + 1, area.x().n() + 1);
var matrix = new Matrix(area.tn() + 1, area.xn() + 1);
// Set initial value
for (var i = 0; i <= area.x().n(); i++) {
matrix.set(0, i, eqn.gU0(area.x().x(i)));
for (var i = 0; i <= area.xn(); i++) {
matrix.set(0, i, eqn.gU0(area.xx(i)));
}
return matrix;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public Solution<HyperbolicEquation> solve(HyperbolicEquation eqn, double h, doub
var area = buildArea(eqn, h, tau);
var solution = prepare(eqn, area);

int N = area.x().n();
int N = area.xn();
var A = new double[N];
var B = new double[N];
var C = new double[N];
Expand All @@ -32,23 +32,23 @@ public Solution<HyperbolicEquation> solve(HyperbolicEquation eqn, double h, doub
_u = solution.get(0, i - 1),
u = solution.get(0, i),
u_ = solution.get(0, i + 1),
x = area.x().x(i);
x = area.xx(i);

solution.set(1, i, u + tau * (eqn.gdU_dt(x) + t_2 / eqn.gM(x, 0, u) * (
eqn.gK(x, 0, u) / h2 * (_u - 2 * u + u_) + eqn.gV(x, 0, u) / _2h * (u_ - _u) + eqn.gF(x, 0, u))));
}

// Finite-difference algorithm implementation
//
for (int j = 0; j <= area.t().n() - 2; j++) {
for (int j = 0; j <= area.tn() - 2; j++) {
for (int i = 1; i < N; i++) {
double
_u = solution.get(j, i - 1),
u = solution.get(j, i),
u_ = solution.get(j, i + 1),

x = area.x().x(i),
t = area.t().x(j),
x = area.xx(i),
t = area.tx(j),

Alpha = eqn.gK(x, t, u) - eqn.gV(x, t, u) * h_2,
Beta = eqn.gK(x, t, u) + eqn.gV(x, t, u) * h_2,
Expand All @@ -63,7 +63,7 @@ public Solution<HyperbolicEquation> solve(HyperbolicEquation eqn, double h, doub
}

int nj = j + 2;
var time = area.t().x(nj);
var time = area.tx(nj);
var U = progonka(eqn, h, time, A, B, C, F);
solution.set(nj, U);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public Solution<ParabolicEquation> solve(ParabolicEquation eqn, double h, double
);
var solution = prepare(eqn, area);

int N = area.x().n();
int N = area.xn();
var A = new double[N];
var B = new double[N];
var C = new double[N];
Expand All @@ -25,17 +25,17 @@ public Solution<ParabolicEquation> solve(ParabolicEquation eqn, double h, double

// Finite-difference algorithm implementation
//
for (int j = 0; j < area.t().n(); j++) {
for (int j = 0; j < area.tn(); j++) {
for (int i = 1; i < N; i++) {
double
_u = solution.get(j, i - 1),
u = solution.get(j, i),
u_ = solution.get(j, i + 1),

_x = area.x().x(i - 1),
x = area.x().x(i),
x_ = area.x().x(i + 1),
t = area.t().x(j),
_x = area.xx(i - 1),
x = area.xx(i),
x_ = area.xx(i + 1),
t = area.tx(j),

Alpha = (eqn.gK(x, t, u) + eqn.gK(x_, t, u_) + eqn.gV(x, t, u) * h) / 2.,
Beta = (eqn.gK(x, t, u) + eqn.gK(_x, t, _u) - eqn.gV(x, t, u) * h) / 2.,
Expand All @@ -48,7 +48,7 @@ public Solution<ParabolicEquation> solve(ParabolicEquation eqn, double h, double
}

int nj = j + 1;
var time = area.t().x(nj);
var time = area.tx(nj);
var U = progonka(eqn, h, time, A, B, C, F);
solution.set(nj, U);
}
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/by/andd3dfx/math/pde/solver/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public void sUt(String fileName, double[] t) {
}

var sb = new StringBuilder();
for (var i = 0; i < area.x().n(); i++) {
sb.append(area.x().x(i));
for (var i = 0; i < area.xn(); i++) {
sb.append(area.xx(i));
for (var t_i : t) {
sb.append(" ").append(solution.get(area.t().i(t_i), i));
sb.append(" ").append(solution.get(area.ti(t_i), i));
}
sb.append("\n");
}
Expand Down Expand Up @@ -53,10 +53,10 @@ public void sUx(String fileName, double[] x) {
}

var sb = new StringBuilder();
for (int i = 0; i < area.t().n(); i++) {
sb.append(area.t().x(i));
for (int i = 0; i < area.tn(); i++) {
sb.append(area.tx(i));
for (var x_i : x) {
sb.append(" ").append(solution.get(i, area.x().i(x_i)));
sb.append(" ").append(solution.get(i, area.xi(x_i)));
}
sb.append("\n");
}
Expand All @@ -80,7 +80,7 @@ public void sUx(String fileName, double x) {
* @return U(x) slice
*/
public Matrix gUt(double t) {
return gUt(area.t().i(t));
return gUt(area.ti(t));
}

/**
Expand All @@ -96,7 +96,7 @@ public Matrix gUt(int it) {
int N = solution.getN();
var matrix = new Matrix(2, N);
for (int i = 0; i < N; i++) {
matrix.setX(i, area.x().x(i));
matrix.setX(i, area.xx(i));
matrix.setY(i, solution.get(it, i));
}
return matrix;
Expand All @@ -109,7 +109,7 @@ public Matrix gUt(int it) {
* @return U(t) slice
*/
public Matrix gUx(double x) {
return gUx(area.x().i(x));
return gUx(area.xi(x));
}

/**
Expand All @@ -125,7 +125,7 @@ public Matrix gUx(int ix) {
int M = solution.getM();
var matrix = new Matrix(2, M);
for (int i = 0; i < M; i++) {
matrix.setX(i, area.t().x(i));
matrix.setX(i, area.tx(i));
matrix.setY(i, solution.get(i, ix));
}
return matrix;
Expand Down
62 changes: 61 additions & 1 deletion src/test/java/by/andd3dfx/math/AreaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,72 @@
class AreaTest {

@Test
void xLeftRightNTLeftRight() {
void xLeft() {
var area = new Area(new Interval(3, 8, 10), new Interval(0, 10, 10));

assertThat(area.xLeft()).isEqualTo(3);
}

@Test
void xRight() {
var area = new Area(new Interval(3, 8, 10), new Interval(0, 10, 10));

assertThat(area.xRight()).isEqualTo(8);
}

@Test
void tLeft() {
var area = new Area(new Interval(3, 8, 10), new Interval(0, 10, 10));

assertThat(area.tLeft()).isEqualTo(0);
}

@Test
void tRight() {
var area = new Area(new Interval(3, 8, 10), new Interval(0, 10, 10));

assertThat(area.tRight()).isEqualTo(10);
}

@Test
void xn() {
var area = new Area(new Interval(3, 8, 10), new Interval(0, 10, 20));

assertThat(area.xn()).isEqualTo(10);
}

@Test
void xx() {
var area = new Area(new Interval(3, 8, 10), new Interval(0, 10, 20));

assertThat(area.xx(3)).isEqualTo(4.5d);
}

@Test
void xi() {
var area = new Area(new Interval(3, 8, 10), new Interval(0, 10, 20));

assertThat(area.xi(5.5)).isEqualTo(5);
}

@Test
void tn() {
var area = new Area(new Interval(3, 8, 10), new Interval(0, 10, 20));

assertThat(area.tn()).isEqualTo(20);
}

@Test
void tx() {
var area = new Area(new Interval(3, 8, 10), new Interval(0, 10, 20));

assertThat(area.tx(3)).isEqualTo(1.5d);
}

@Test
void ti() {
var area = new Area(new Interval(3, 8, 10), new Interval(0, 10, 20));

assertThat(area.ti(5.5)).isEqualTo(11);
}
}

0 comments on commit 8f96104

Please sign in to comment.