Skip to content

Commit 975ffc5

Browse files
refactoring and javadoc
1 parent 636b0a2 commit 975ffc5

16 files changed

+466
-336
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.rae.formicapi.math.data;
2+
3+
import java.util.Map;
4+
import java.util.TreeMap;
5+
6+
public class OneDTabulatedFunction {
7+
private final TreeMap<Float, Float> table;
8+
private final float step;
9+
private final StepMode mode;
10+
private final boolean clamp;
11+
12+
/**
13+
* @param table Sorted data: x -> y
14+
* @param step Distance between values in transformed space (e.g., log(x) if LOGARITHMIC)
15+
* @param mode The axis step mode (linear, log, etc.)
16+
* @param clamp Clamp outside instead of extrapolating.
17+
*/
18+
public OneDTabulatedFunction(TreeMap<Float, Float> table, float step, StepMode mode, boolean clamp) {
19+
this.table = table;
20+
this.step = step;
21+
this.mode = mode;
22+
this.clamp = clamp;
23+
}
24+
25+
private float evaluate(double input, TreeMap<Float, Float> table) {
26+
if (table.isEmpty()) {
27+
throw new IllegalStateException("Function table is empty");
28+
}
29+
30+
float index = (float) (mode.forward.applyAsDouble(input) / step);
31+
int lowerIndex = (int) Math.floor(index);
32+
float frac = index - lowerIndex;
33+
34+
float T1 = (float) mode.inverse.applyAsDouble(lowerIndex * step);
35+
float T2 = (float) mode.inverse.applyAsDouble((lowerIndex + 1) * step);
36+
37+
// Safeguard in case floating-point precision causes a missing key
38+
if (!table.containsKey(T1) || !table.containsKey(T2)) {
39+
Map.Entry<Float, Float> lower = table.floorEntry((float) input);
40+
Map.Entry<Float, Float> upper = table.ceilingEntry((float) input);
41+
42+
if (lower == null || upper == null) {
43+
return table.get(table.firstKey());
44+
}
45+
46+
float T_lower = lower.getKey();
47+
float T_upper = upper.getKey();
48+
if (T_lower == T_upper) {
49+
return table.get(T_lower);
50+
}
51+
float fracAlt = (float) ((input - T_lower) / (T_upper - T_lower));
52+
53+
return lower.getValue() * (1 - fracAlt) + upper.getValue() * fracAlt;
54+
}
55+
56+
float P1 = table.get(T1);
57+
float P2 = table.get(T2);
58+
59+
return P1 * (1 - frac) + P2 * frac;
60+
}
61+
62+
public float evaluate(float output) {
63+
return evaluate(output, table);
64+
}
65+
66+
private float extrapolateBelow(TreeMap<Float, Float> searchMap) {
67+
Map.Entry<Float, Float> lower = searchMap.firstEntry();
68+
Map.Entry<Float, Float> upper = searchMap.higherEntry(lower.getKey());
69+
if (upper == null) return lower.getValue();
70+
return linear(searchMap, lower, upper);
71+
}
72+
73+
private float extrapolateAbove(TreeMap<Float, Float> searchMap) {
74+
Map.Entry<Float, Float> upper = searchMap.lastEntry();
75+
Map.Entry<Float, Float> lower = searchMap.lowerEntry(upper.getKey());
76+
if (lower == null) return upper.getValue();
77+
return linear(searchMap, lower, upper);
78+
}
79+
80+
private float linear(TreeMap<Float, Float> map, Map.Entry<Float, Float> a, Map.Entry<Float, Float> b) {
81+
float x1 = a.getKey();
82+
float x2 = b.getKey();
83+
float y1 = a.getValue();
84+
float y2 = b.getValue();
85+
float query = map.firstKey(); // doesn't matter in this context
86+
float t = (query - x1) / (x2 - x1);
87+
return y1 * (1 - t) + y2 * t;
88+
}
89+
90+
91+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.rae.formicapi.math.data;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.TreeMap;
7+
import java.util.function.Function;
8+
9+
public class ReversibleOneDTabulatedFunction {
10+
11+
private final OneDTabulatedFunction f;
12+
private final OneDTabulatedFunction inverse_f;
13+
14+
public ReversibleOneDTabulatedFunction(Function<Float, Float> f, float min, float max, StepMode stepMode, float step, StepMode inverseStepMode, float inverseStep) {
15+
TreeMap<Float, Float> populated = populate(f, min, max, stepMode, step);
16+
this.f = new OneDTabulatedFunction(populated, step, stepMode, true);
17+
inverse_f = new OneDTabulatedFunction(invertTable(populated, inverseStepMode, inverseStep), inverseStep, inverseStepMode, true);
18+
}
19+
20+
private TreeMap<Float, Float> populate(Function<Float, Float> f, float min, float max, StepMode stepMode, float step) {
21+
TreeMap<Float, Float> treeMap = new TreeMap<>();
22+
for (float x = min; x < max; x= (float) stepMode.inverse.applyAsDouble(stepMode.forward.applyAsDouble(x) + step)) {
23+
try {
24+
float P = f.apply(x);
25+
treeMap.put(x, P);
26+
} catch (Exception ignored) {
27+
}
28+
}
29+
return treeMap;
30+
}
31+
32+
33+
/**
34+
* Returns a reversed version of the map: y -> x
35+
*/
36+
private TreeMap<Float, Float> invertTable(TreeMap<Float, Float> original, StepMode stepMode, float step) {
37+
TreeMap<Float, Float> inverted = new TreeMap<>();
38+
// Step 2: Build regular grid in log(P) space
39+
List<Map.Entry<Float, Float>> entries = new ArrayList<>(original.entrySet());
40+
41+
// Determine the log pressure range
42+
float logP_start = (float) stepMode.forward.applyAsDouble(entries.get(0).getValue());
43+
float logP_end = (float) stepMode.forward.applyAsDouble(entries.get(entries.size() - 1).getValue());
44+
int numSteps = (int) ((logP_end - logP_start) / step);
45+
46+
47+
for (int i = 0; i < numSteps; i++) {
48+
float targetLogP = logP_start + i * step;
49+
50+
// Find where targetLogP fits between logP1 and logP2
51+
for (int j = 0; j < entries.size() - 1; j++) {
52+
float T1 = entries.get(j).getKey();
53+
float T2 = entries.get(j + 1).getKey();
54+
float logP1 = (float) stepMode.forward.applyAsDouble(entries.get(j).getValue());
55+
float logP2 = (float) stepMode.forward.applyAsDouble(entries.get(j + 1).getValue());
56+
57+
if (targetLogP >= logP1 && targetLogP <= logP2 && logP1 != logP2) {
58+
// Linear interpolation in logP
59+
float t = (targetLogP - logP1) / (logP2 - logP1);
60+
float T_interp = T1 + t * (T2 - T1);
61+
62+
inverted.put((float) stepMode.inverse.applyAsDouble(targetLogP), T_interp);
63+
break;
64+
}
65+
}
66+
}
67+
return inverted;
68+
}
69+
70+
public float getF(float x) {
71+
return f.evaluate(x);
72+
}
73+
74+
public float getInverseF(float x) {
75+
return inverse_f.evaluate(x);
76+
}
77+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.rae.formicapi.math.data;
2+
3+
import java.util.function.DoubleUnaryOperator;
4+
5+
public enum StepMode {
6+
LINEAR(
7+
x -> x,
8+
x -> x
9+
),
10+
LOGARITHMIC(
11+
Math::log,
12+
Math::exp
13+
);
14+
15+
public final DoubleUnaryOperator forward;
16+
public final DoubleUnaryOperator inverse;
17+
18+
StepMode(DoubleUnaryOperator forward, DoubleUnaryOperator inverse) {
19+
this.forward = forward;
20+
this.inverse = inverse;
21+
}
22+
}

src/main/java/com/rae/formicapi/thermal_utilities/EOSLibrary.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.rae.formicapi.thermal_utilities;
22

3+
import com.rae.formicapi.thermal_utilities.eos.PengRobinsonEOS;
4+
import com.rae.formicapi.thermal_utilities.eos.VanDerWaalsEOS;
5+
36
public class EOSLibrary {
47
//you can find a,b values at https://en.wikipedia.org/wiki/Van_der_Waals_constants_(data_page)
58
public static PengRobinsonEOS getPRWaterEOS() {

src/main/java/com/rae/formicapi/thermal_utilities/WaterTableBasedTransformationHelper.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)