Skip to content

Commit b339109

Browse files
improved placement for multiblock + improved number format.
1 parent b903629 commit b339109

15 files changed

Lines changed: 148495 additions & 9478 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod_id=formicapi
2020
mod_name=Formic API
2121
mod_license=MIT
2222

23-
mod_version=1.6.2
23+
mod_version=1.6.8
2424

2525
create_version = 6.0.2-50
2626
flywheel_version = 1.0.1
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,78 @@
11
package com.rae.formicapi;
22

33
import com.mojang.brigadier.CommandDispatcher;
4+
import com.mojang.brigadier.arguments.FloatArgumentType;
5+
import com.mojang.brigadier.context.CommandContext;
6+
import com.rae.formicapi.thermal_utilities.Plotting;
7+
import net.minecraft.Util;
48
import net.minecraft.commands.CommandSourceStack;
9+
import net.minecraft.commands.Commands;
10+
import net.minecraft.network.chat.Component;
11+
12+
import java.util.stream.DoubleStream;
513

614
public class CommandsInit {
715

816
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
17+
dispatcher.register(Commands.literal("plotIsentropes")
18+
.then(Commands.argument("sMin", FloatArgumentType.floatArg(0))
19+
.then(Commands.argument("sMax", FloatArgumentType.floatArg(0))
20+
.then(Commands.argument("sStep", FloatArgumentType.floatArg(0))
21+
.then(Commands.argument("pMin", FloatArgumentType.floatArg(0))
22+
.then(Commands.argument("pMax", FloatArgumentType.floatArg(0))
23+
.executes(CommandsInit::execute)
24+
)
25+
)
26+
)
27+
)
28+
)
29+
);
30+
}
31+
32+
private static int execute(CommandContext<CommandSourceStack> ctx) {
33+
float sMin = FloatArgumentType.getFloat(ctx, "sMin");
34+
float sMax = FloatArgumentType.getFloat(ctx, "sMax");
35+
float sStep = FloatArgumentType.getFloat(ctx, "sStep");
36+
37+
float pMin = FloatArgumentType.getFloat(ctx, "pMin");
38+
float pMax = FloatArgumentType.getFloat(ctx, "pMax");
39+
40+
// Parse comma-separated entropy values, e.g., "3.5,4.0,4.5"
41+
double[] entropies = DoubleStream.iterate(
42+
sMin,
43+
s -> s <= sMax + 1e-6,
44+
s -> s + sStep
45+
).toArray();
46+
//DistExecutor.unsafeRunWhenOn(
47+
// Dist.CLIENT,() -> () ->
48+
49+
Util.backgroundExecutor().execute(() -> {
50+
//Plotting.plotSaturationPressurePT("Water Saturation Pressure (P–H)", pMin, pMax);
51+
//Plotting.plotSaturationPressurePH("Water Saturation Pressure (P–H)", pMin, pMax);
52+
53+
Plotting.plotIsentropesPH(
54+
"Water Isentropes (P–H)",
55+
entropies,
56+
pMin,
57+
pMax
58+
);
59+
60+
Plotting.plotIsentropesPT(
61+
"Water Isentropes (P–T)",
62+
entropies,
63+
pMin,
64+
pMax
65+
);
66+
67+
68+
});
69+
70+
ctx.getSource().sendSuccess( () -> Component.literal("Isentrope plot generated!"),
71+
true
72+
);
73+
74+
return 1;
975
}
76+
77+
1078
}

src/main/java/com/rae/formicapi/FormicApiLang.java

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,27 @@
99
import net.createmod.catnip.lang.LangBuilder;
1010
import net.createmod.catnip.lang.LangNumberFormat;
1111
import net.minecraft.network.chat.Component;
12-
import net.minecraft.network.chat.MutableComponent;
13-
import net.minecraft.world.item.ItemStack;
14-
import net.minecraft.world.level.block.state.BlockState;
15-
import net.minecraftforge.fluids.FluidStack;
1612

17-
import java.util.ArrayList;
18-
import java.util.List;
13+
import java.util.Map;
14+
import java.util.TreeMap;
1915

2016
public class FormicApiLang extends Lang {
2117
//blatant copy of CreateLang
22-
/**
23-
* legacy-ish. Use CROWNSLang.translate and other builder methods where possible
24-
*/
25-
public static MutableComponent translateDirect(String key, Object... args) {
26-
Object[] args1 = LangBuilder.resolveBuilders(args);
27-
return Component.translatable(FormicAPI.MODID + "." + key, args1);
28-
}
29-
30-
public static List<Component> translatedOptions(String prefix, String... keys) {
31-
List<Component> result = new ArrayList<>(keys.length);
32-
for (String key : keys)
33-
result.add(translate((prefix != null ? prefix + "." : "") + key).component());
34-
return result;
18+
static TreeMap<Double, String> MULTIPLE_SYMBOLS = new TreeMap<>();
19+
static {
20+
MULTIPLE_SYMBOLS.put(1e18, "E");
21+
MULTIPLE_SYMBOLS.put(1e15, "P");
22+
MULTIPLE_SYMBOLS.put(1e12, "T");
23+
MULTIPLE_SYMBOLS.put(1e9, "G");
24+
MULTIPLE_SYMBOLS.put(1e6, "M");
25+
MULTIPLE_SYMBOLS.put(1e3, "k");
26+
MULTIPLE_SYMBOLS.put(1.0, "");
27+
MULTIPLE_SYMBOLS.put(1e-3, "m");
28+
MULTIPLE_SYMBOLS.put(1e-6, "µ");
29+
MULTIPLE_SYMBOLS.put(1e-9, "n");
30+
MULTIPLE_SYMBOLS.put(1e-12,"p");
31+
MULTIPLE_SYMBOLS.put(1e-15,"f");
32+
MULTIPLE_SYMBOLS.put(1e-18,"a");
3533
}
3634

3735
//
@@ -40,57 +38,34 @@ public static LangBuilder builder() {
4038
return new LangBuilder(FormicAPI.MODID);
4139
}
4240

43-
public static LangBuilder blockName(BlockState state) {
44-
return builder().add(state.getBlock()
45-
.getName());
46-
}
47-
48-
public static LangBuilder itemName(ItemStack stack) {
49-
return builder().add(stack.getHoverName()
50-
.copy());
51-
}
52-
53-
public static LangBuilder fluidName(FluidStack stack) {
54-
return builder().add(stack.getDisplayName()
55-
.copy());
56-
}
57-
58-
public static LangBuilder number(double d) {
59-
return builder().text(LangNumberFormat.format(d));
41+
public static LangBuilder numberWithSymbol(double d) {
42+
Map.Entry<Double, String> entry = MULTIPLE_SYMBOLS.floorEntry(d);
43+
if (entry == null) {
44+
builder().text(LangNumberFormat.format(d)+ " ");
45+
}
46+
return builder().text(LangNumberFormat.format(d/entry.getKey())+" "+entry.getValue());
6047
}
6148

6249
public static LangBuilder translate(String langKey, Object... args) {
6350
return builder().translate(langKey, args);
6451
}
6552

66-
public static LangBuilder text(String text) {
67-
return builder().text(text);
68-
}
69-
70-
@Deprecated // Use while implementing and replace all references with Lang.translate
71-
public static LangBuilder temporaryText(String text) {
72-
return builder().text(text);
73-
}
74-
7553
public static LangBuilder formatTemperature(float temperature) {
7654
Temperature unit = FormicAPIConfigs.CLIENT.units.temperature.get();
7755
return CreateLang.builder().add(Component.literal("T = "))
78-
.add(number(unit.convert(temperature)))
79-
.text(" ")
56+
.text(LangNumberFormat.format(unit.convert(temperature))+ " ")
8057
.add(unit.getSymbol());
8158
}
8259
public static LangBuilder formatPressure(float pressure) {
8360
Pressure unit = FormicAPIConfigs.CLIENT.units.pressure.get();
8461
return CreateLang.builder().add(Component.literal("P = "))
85-
.add(number(unit.convert(pressure)))
86-
.text(" ")
62+
.add(numberWithSymbol(unit.convert(pressure)))
8763
.add(unit.getSymbol());
8864
}
8965
public static LangBuilder formatRadiationFlux(float radiationFlux) {
9066
RadiationFlux unit = FormicAPIConfigs.CLIENT.units.radiationFlux.get();
9167
return CreateLang.builder().add(Component.literal("activity : "))
92-
.add(number(unit.convert(radiationFlux)))
93-
.text(" ")
68+
.add(numberWithSymbol(unit.convert(radiationFlux)))
9469
.add(unit.getSymbol());
9570
}
9671

src/main/java/com/rae/formicapi/config/UnitConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class UnitConfig extends ConfigBase {
1010
public final ConfigEnum<Temperature> temperature = e(Temperature.CELSIUS,"temperature", Comments.temperature);
1111
public final ConfigEnum<Pressure> pressure = e(Pressure.ATMOSPHERES,"pressure", Comments.pressure);
12-
public final ConfigEnum<RadiationFlux> radiationFlux = e(RadiationFlux.MEGA_BECQUERELS,"radiation_flux", Comments.radiationFlux);
12+
public final ConfigEnum<RadiationFlux> radiationFlux = e(RadiationFlux.BECQUERELS,"radiation_flux", Comments.radiationFlux);
1313

1414

1515
@Override

src/main/java/com/rae/formicapi/multiblock/MBItem.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected boolean canPlace(BlockPlaceContext pContext, @NotNull BlockState pStat
2929
Level lvl = pContext.getLevel();
3030
Direction facing = pContext.getClickedFace();
3131
Vec3i offset = main.getOffset(facing, false);//nope this isn't the correct offset to know where to verify the blocks
32-
BlockPos mainPos = pContext.getClickedPos().offset(offset);
32+
BlockPos mainPos = pContext.getClickedPos();//.offset(offset);
3333
boolean flag = true;
3434
Vec3i size = main.getSize(facing);
3535
for (int x = -offset.getX(); x < size.getX() - offset.getX();x++){
@@ -48,7 +48,7 @@ protected boolean canPlace(BlockPlaceContext pContext, @NotNull BlockState pStat
4848
break;
4949
}
5050
}
51-
return true;
51+
return flag;
5252
}
5353

5454
@Override

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

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,39 @@ public static float getX(float H, float P){
3131

3232

3333
private static @NotNull SpecificRealGazState isentropicPressureChange(float H1, float P1, float finalPressure) {
34-
float sTarget = (float) getS(H1, P1);
34+
float sTarget = getS(H1, P1);
3535

36-
float hFinal = getH(sTarget, finalPressure);
37-
float Tfinal = getT(hFinal,finalPressure);
38-
float xFinal = getX(hFinal,Tfinal);
39-
return new SpecificRealGazState(Tfinal, finalPressure, hFinal, xFinal);
36+
// Initial guess from table interpolation
37+
float h = getH(sTarget, finalPressure);
38+
39+
final float EPS = 1e-4f*P1; // derivative step
40+
final float TOL = 1e-1f; // convergence tolerance
41+
final int MAX_ITER = 20;
42+
43+
for (int i = 0; i < MAX_ITER; i++) {
44+
float s = getS(h, finalPressure);
45+
float f = s - sTarget;
46+
47+
if (Math.abs(f) < TOL) {
48+
break; // converged
49+
}
50+
51+
// Numerical derivative ds/dh
52+
float s2 = getS(h + EPS, finalPressure);
53+
float df = (s2 - s) / EPS;
54+
55+
// Safety check
56+
if (Math.abs(df) < 1e-8f) {
57+
break; // derivative too small → avoid explosion
58+
}
59+
60+
h -= f / df;
61+
}
62+
63+
float Tfinal = getT(h, finalPressure);
64+
float xFinal = getX(h, Tfinal);
65+
66+
return new SpecificRealGazState(Tfinal, finalPressure, h, xFinal);
4067
}
4168
/**
4269
* adiabatic reversible expansion
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.rae.formicapi.thermal_utilities;
2+
3+
import com.rae.formicapi.FormicAPI;
4+
import com.rae.formicapi.plotting.SimplePlot;
5+
import net.minecraftforge.api.distmarker.Dist;
6+
import net.minecraftforge.api.distmarker.OnlyIn;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
/**
12+
* Plotting utilities using the SimplePlot API
13+
*/
14+
@OnlyIn(Dist.CLIENT)
15+
public class Plotting {
16+
17+
/**
18+
* Plot isentropes (Pressure vs Enthalpy) using SimplePlot
19+
*/
20+
public static void plotIsentropesPH(String name, double[] entropies, float pMin, float pMax) {
21+
22+
SimplePlot plot = new SimplePlot()
23+
.title(name)
24+
.xlabel("Enthalpy (J/Kg)")
25+
.ylabel("Pressure (Pa)")
26+
.yLog(true)
27+
.width(800)
28+
.height(600);
29+
30+
for (double s : entropies) {
31+
List<Double> pressures = new ArrayList<>();
32+
List<Double> enthalpies = new ArrayList<>();
33+
34+
for (float p = pMin; p <= pMax; p *= 1.01f) {
35+
try {
36+
float h = FullTableBased.getH((float) s, p);
37+
if (!Float.isFinite(h)) continue;
38+
39+
pressures.add((double)p);
40+
enthalpies.add((double)h);
41+
} catch (Exception ignored) {}
42+
}
43+
44+
plot.addSeries(String.format("s=%.2f kJ/kg·K", s), enthalpies, pressures);
45+
}
46+
47+
plot.save("isentropes_ph.png");
48+
}
49+
50+
/**
51+
* Plot isentropes (Pressure vs Temperature) using SimplePlot
52+
*/
53+
public static void plotIsentropesPT(String name, double[] entropies, float pMin, float pMax) {
54+
55+
SimplePlot plot = new SimplePlot()
56+
.title(name)
57+
.xlabel("Temperature (K)")
58+
.ylabel("Pressure (Pa)")
59+
.yLog(true)
60+
.width(800)
61+
.height(600);
62+
63+
for (double s : entropies) {
64+
List<Double> pressures = new ArrayList<>();
65+
List<Double> temperatures = new ArrayList<>();
66+
67+
for (float p = pMin; p <= pMax; p *= 1.01f) {
68+
try {
69+
float T = FullTableBased.getT(p, (float)FullTableBased.getH((float) s, p));
70+
if (!Float.isFinite(T)) continue;
71+
72+
pressures.add((double)p);
73+
temperatures.add((double)T);
74+
} catch (Exception ignored) {}
75+
}
76+
77+
plot.addSeries(String.format("s=%.2f kJ/kg·K", s), temperatures, pressures);
78+
}
79+
List<Double> pressures = new ArrayList<>();
80+
List<Double> temperatures = new ArrayList<>();
81+
82+
/*for (float p = pMin; p <= pMax; p *= 1.01f) {
83+
try {
84+
float T = EOSLibrary.getPRWaterEOS().saturationTemperature(p);
85+
if (!Float.isFinite(T)) continue;
86+
87+
pressures.add((double)p);
88+
temperatures.add((double)T);
89+
} catch (Exception ignored) {}
90+
}
91+
92+
plot.addSeries(String.format("saturation"), temperatures, pressures);*/
93+
94+
95+
96+
plot.save("isentropes_pt.png");
97+
}
98+
}

src/main/java/com/rae/formicapi/units/RadiationFlux.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import net.minecraft.network.chat.Component;
55

66
public enum RadiationFlux implements IUnit{
7-
// Assuming base unit is MBq (MegaBecquerels)
8-
MEGA_BECQUERELS(1f),
9-
CURIES(1/37000f), // 1 Ci = 37 GBq = 37_000 MBq
7+
// Assuming base unit is Bq (Becquerels)
8+
BECQUERELS(1e6f),
9+
CURIES(1/(37e9f)), // 1 Ci = 37 GBq = 37_000 MBq
1010
// Approximations for absorbed dose rate assuming 1 MeV per decay and full absorption in 1 kg
11-
GRAY_PER_SECOND(1.602e-7f), // ~1 MeV per decay
12-
RAD_PER_SECOND(1.602e-5f), // 1 Gy = 100 rad
13-
SIEVERT_PER_SECOND(1.602e-7f); // Q factor assumed 1
11+
GRAY_PER_SECOND(1.602e-7f/1e6f), // ~1 MeV per decay
12+
RAD_PER_SECOND(1.602e-5f/1e6f), // 1 Gy = 100 rad
13+
SIEVERT_PER_SECOND(1.602e-7f/1e6f); // Q factor assumed 1
1414

1515
private final float a;
1616
final Component symbol;

0 commit comments

Comments
 (0)