diff --git a/src/main/java/by/andd3dfx/math/Area.java b/src/main/java/by/andd3dfx/math/Area.java index 08beb49..ffe2369 100644 --- a/src/main/java/by/andd3dfx/math/Area.java +++ b/src/main/java/by/andd3dfx/math/Area.java @@ -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 */ @@ -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); + } } diff --git a/src/main/java/by/andd3dfx/math/pde/solver/AbstractEquationSolver.java b/src/main/java/by/andd3dfx/math/pde/solver/AbstractEquationSolver.java index 779000a..ce00c24 100644 --- a/src/main/java/by/andd3dfx/math/pde/solver/AbstractEquationSolver.java +++ b/src/main/java/by/andd3dfx/math/pde/solver/AbstractEquationSolver.java @@ -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; } diff --git a/src/main/java/by/andd3dfx/math/pde/solver/HyperbolicEquationSolver.java b/src/main/java/by/andd3dfx/math/pde/solver/HyperbolicEquationSolver.java index f9d979c..ded2d7b 100644 --- a/src/main/java/by/andd3dfx/math/pde/solver/HyperbolicEquationSolver.java +++ b/src/main/java/by/andd3dfx/math/pde/solver/HyperbolicEquationSolver.java @@ -9,7 +9,7 @@ public Solution 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]; @@ -32,7 +32,7 @@ public Solution 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)))); @@ -40,15 +40,15 @@ public Solution solve(HyperbolicEquation eqn, double h, doub // 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, @@ -63,7 +63,7 @@ public Solution 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); } diff --git a/src/main/java/by/andd3dfx/math/pde/solver/ParabolicEquationSolver.java b/src/main/java/by/andd3dfx/math/pde/solver/ParabolicEquationSolver.java index 07d1d3a..2109505 100644 --- a/src/main/java/by/andd3dfx/math/pde/solver/ParabolicEquationSolver.java +++ b/src/main/java/by/andd3dfx/math/pde/solver/ParabolicEquationSolver.java @@ -14,7 +14,7 @@ public Solution 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]; @@ -25,17 +25,17 @@ public Solution 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., @@ -48,7 +48,7 @@ public Solution 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); } diff --git a/src/main/java/by/andd3dfx/math/pde/solver/Solution.java b/src/main/java/by/andd3dfx/math/pde/solver/Solution.java index 65aa594..404e806 100644 --- a/src/main/java/by/andd3dfx/math/pde/solver/Solution.java +++ b/src/main/java/by/andd3dfx/math/pde/solver/Solution.java @@ -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"); } @@ -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"); } @@ -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)); } /** @@ -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; @@ -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)); } /** @@ -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; diff --git a/src/test/java/by/andd3dfx/math/AreaTest.java b/src/test/java/by/andd3dfx/math/AreaTest.java index e0db377..ea3b53e 100644 --- a/src/test/java/by/andd3dfx/math/AreaTest.java +++ b/src/test/java/by/andd3dfx/math/AreaTest.java @@ -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); + } }