Skip to content

Commit

Permalink
Add comparison of numeric & analytic solutions. Still need to add las…
Browse files Browse the repository at this point in the history
…t of it
  • Loading branch information
andrei-punko committed Oct 16, 2024
1 parent 42721a4 commit b4cc451
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/main/java/by/andd3dfx/math/Interval.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package by.andd3dfx.math;

import lombok.ToString;

@ToString
public class Interval {

private double left;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package by.andd3dfx.math.pde.equation;

import by.andd3dfx.math.Interval;
import by.andd3dfx.math.pde.border.BorderConditionType1;
import by.andd3dfx.util.FileUtil;
import org.junit.jupiter.api.Test;

/**
* <pre>
* Solution of diffusion problem:
* - plate with thickness d=1 mm
* - constant concentration C=1.0 on left border and C=0.0 on right border
* - constant diffusion coefficient D
* </pre>
*/
class ParabolicEquationTest {

private final double C_LEFT = 1.0;
private final double THICKNESS = 1e-3; // 1mm
private final double TIME = 1; // 1sec
private final double DIFFUSION_COEFFICIENT = 1e-4;

// We allow difference between numeric & analytic solution no more than 5% of max concentration value
private final double EPSILON = C_LEFT / 20.;

@Test
void solve() {
var h = THICKNESS / 1000.0;
Expand All @@ -20,10 +30,21 @@ void solve() {

diffusionEquation.solve(h, tau);

diffusionEquation.sUt("./build/res-numeric.txt", TIME);
FileUtil.saveFunc(new Interval(0, THICKNESS, h),
// Save numeric solution to file
var numericU = diffusionEquation.gUt(TIME);
FileUtil.save(numericU, "./build/res-numeric.txt", true);

// Save analytic solution to file
FileUtil.saveFunc(diffusionEquation.area.x(),
(x) -> analyticSolution(x, TIME), "./build/res-analytic.txt");
// TODO compare numeric & analytic solutions

// Compare numeric & analytic solutions
for (var i = 0; i < numericU.getN(); i++) {
var numericY = numericU.y(i);
var analyticY = analyticSolution(numericU.x(i), TIME);
// TODO uncomment assert below after addition of analytic solution
// assertThat(Math.abs(numericY - analyticY)).isLessThanOrEqualTo(EPSILON);
}
}

private ParabolicEquation buildParabolicEquation() {
Expand All @@ -45,6 +66,7 @@ protected double gK(double x, double t, double U) {

private double analyticSolution(double x, double t) {
// TODO add analytic solution
return 0;
var A = 1.5e+3;
return C_LEFT * Math.exp(-A * x);
}
}

0 comments on commit b4cc451

Please sign in to comment.