1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
//! Phase Change Simulation Modifier
//!
//! Temperature-driven phase transitions between solid, liquid, and gas:
//! - **Melting** (solid → liquid): Surface softens, material flows downward
//! - **Vaporization** (liquid → gas): Material expands, then dissipates
//! - **Solidification** (liquid → solid): Material grows rigid, surface freezes
//! - **Condensation** (gas → liquid): Gas contracts back to liquid
//!
//! # Physics Model
//!
//! Each cell tracks: temperature, phase state, and a latent-heat buffer.
//! Transitions use the enthalpy method (Voller & Cross 1981; Carslaw &
// LIMITATION(COV-THERM-069): with the heat capacity normalised to 1 the latent heats are in kelvin-equivalent units
//! Jaeger ch. XI for the Stefan problem): with the heat capacity normalised
//! to 1 the latent heats are in kelvin-equivalent units, and each `update`
//! moves temperature above a transition point into the buffer (or releases
//! it back on cooling) until the buffer reaches the latent heat. The cell
//! therefore sits **on the plateau** `T = T_m` while it melts and
//! `T + latent` is conserved exactly by the transition step; the melt time
//! is set by how fast diffusion / applied heat feed the cell, not by the
//! step size. Phase transitions modify the SDF: liquid flows downward
//! (mass-conserving transfer), gas expands outward.
//!
//! Author: Moroya Sakamoto
use crate::coupled_field::{CoupledField, CoupledFieldError, CoupledScalar};
use crate::math::Fix128;
use crate::sim_field::ScalarField3D;
use crate::sim_modifier::PhysicsModifier;
use crate::sim_modifier::{observe_max, observe_sum, StateReader, StateWriter};
use crate::world_participant::{
ObservationSink, Participant, ParticipantFault, ParticipantKind, StateError, SubstepCtx,
};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
// ============================================================================
// Phase State
// ============================================================================
/// Phase of matter
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum Phase {
/// Rigid — no SDF modification
Solid = 0,
/// Flows downward under gravity
Liquid = 1,
/// Expands outward, dissipates over time
Gas = 2,
}
// ============================================================================
// Configuration
// ============================================================================
/// Phase change modifier configuration
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PhaseChangeConfig {
/// Temperature at which solid melts to liquid
pub melt_temperature: f32,
/// Temperature at which liquid vaporizes to gas
pub boil_temperature: f32,
/// Latent heat of fusion in kelvin-equivalent units (`L_f / c_p`):
/// the temperature rise a cell must supply, and hold at
/// `melt_temperature`, before it turns liquid.
pub latent_heat_fusion: f32,
/// Latent heat of vaporization in kelvin-equivalent units (`L_v / c_p`).
pub latent_heat_vaporization: f32,
/// Thermal diffusion rate
pub diffusion_rate: f32,
/// Ambient temperature
pub ambient_temperature: f32,
/// Cooling rate toward ambient
pub cooling_rate: f32,
/// Liquid flow speed (gravity-driven downward): fraction of a liquid
/// cell's offset moved to the cell below per second, and (× 0.1) the
/// rate at which liquid softens the surface (offset per second).
pub liquid_flow_speed: f32,
/// Gas expansion rate (SDF offset per second)
pub gas_expansion_rate: f32,
/// Gas dissipation rate (material removal per second)
pub gas_dissipation_rate: f32,
/// Maximum SDF offset from phase changes
pub max_offset: f32,
}
impl Default for PhaseChangeConfig {
fn default() -> Self {
Self {
melt_temperature: 200.0,
boil_temperature: 500.0,
latent_heat_fusion: 50.0,
latent_heat_vaporization: 100.0,
diffusion_rate: 0.5,
ambient_temperature: 20.0,
cooling_rate: 0.1,
liquid_flow_speed: 1.0,
gas_expansion_rate: 0.5,
gas_dissipation_rate: 0.3,
max_offset: 3.0,
}
}
}
// ============================================================================
// Phase Change Modifier
// ============================================================================
/// Phase-change SDF modifier with solid/liquid/gas transitions
pub struct PhaseChangeModifier {
/// Configuration
pub config: PhaseChangeConfig,
/// Temperature field
pub temperature: ScalarField3D,
/// Phase state per cell (stored as f32: 0=Solid, 1=Liquid, 2=Gas)
pub phase: ScalarField3D,
/// Latent heat buffer (energy absorbed/released during phase transition)
pub latent_heat: ScalarField3D,
/// Accumulated SDF offset (positive = material removed)
pub sdf_offset: ScalarField3D,
/// Whether modifier is enabled
pub enabled: bool,
}
impl PhaseChangeModifier {
/// Create a new phase change modifier
#[must_use]
pub fn new(
config: PhaseChangeConfig,
resolution: usize,
min: (f32, f32, f32),
max: (f32, f32, f32),
) -> Self {
Self {
config,
temperature: ScalarField3D::new_filled(
resolution,
resolution,
resolution,
min,
max,
config.ambient_temperature,
),
phase: ScalarField3D::new(resolution, resolution, resolution, min, max),
latent_heat: ScalarField3D::new(resolution, resolution, resolution, min, max),
sdf_offset: ScalarField3D::new(resolution, resolution, resolution, min, max),
enabled: true,
}
}
/// Apply heat at a position
pub fn apply_heat_at(&mut self, x: f32, y: f32, z: f32, amount: f32, radius: f32) {
self.temperature.splat(x, y, z, amount, radius);
}
/// Get temperature at a point
#[must_use]
pub fn temperature_at(&self, x: f32, y: f32, z: f32) -> f32 {
self.temperature.sample(x, y, z)
}
/// Get phase at a point
#[must_use]
pub fn phase_at(&self, x: f32, y: f32, z: f32) -> Phase {
let v = self.phase.sample(x, y, z);
if v < 0.5 {
Phase::Solid
} else if v < 1.5 {
Phase::Liquid
} else {
Phase::Gas
}
}
/// Process phase transitions (enthalpy method).
///
/// The latent buffer holds the latent enthalpy stored in the cell, in
/// kelvin-equivalent units: `0..L_f` while a solid melts,
/// `L_f..L_f + L_v` while a liquid boils, `L_f + L_v` for gas. Heat above
/// a transition temperature is moved from `temperature` into the buffer
/// (melting / boiling) and back out of it on cooling (freezing /
/// condensing), so `temperature + latent_heat` is conserved cell by cell
/// and the cell sits on the plateau until the buffer fills or empties.
/// Independent of `dt`: the step only redistributes enthalpy already
/// present in the cell.
fn process_transitions(&mut self) {
let melt_t = self.config.melt_temperature;
let boil_t = self.config.boil_temperature;
let lh_fusion = crate::det_math::max(self.config.latent_heat_fusion, 0.0);
let lh_vaporization = crate::det_math::max(self.config.latent_heat_vaporization, 0.0);
let lh_total = lh_fusion + lh_vaporization;
let n = self.temperature.cell_count();
for i in 0..n {
let mut temp = self.temperature.data[i];
let mut phase_val = self.phase.data[i];
let mut lh = self.latent_heat.data[i];
// Solid → Liquid: superheat above T_m fills the fusion buffer
if phase_val < 0.5 && temp > melt_t {
let absorbed =
crate::det_math::max(crate::det_math::min(temp - melt_t, lh_fusion - lh), 0.0);
temp -= absorbed;
lh += absorbed;
if lh >= lh_fusion {
phase_val = 1.0;
lh = lh_fusion;
}
}
// Liquid → Gas: superheat above T_b fills the vaporization buffer
if (0.5..1.5).contains(&phase_val) && temp > boil_t {
let absorbed =
crate::det_math::max(crate::det_math::min(temp - boil_t, lh_total - lh), 0.0);
temp -= absorbed;
lh += absorbed;
if lh >= lh_total {
phase_val = 2.0;
lh = lh_total;
}
}
// Gas → Liquid: subcooling below T_b drains the vaporization buffer
if phase_val >= 1.5 && temp < boil_t && lh > lh_fusion {
let released = crate::det_math::min(boil_t - temp, lh - lh_fusion);
temp += released;
lh -= released;
if lh <= lh_fusion {
phase_val = 1.0;
lh = lh_fusion;
}
}
// Liquid: a partly-boiled cell that cools first gives back the
// vaporization progress, then subcooling below T_m drains the
// fusion buffer until the cell freezes
if (0.5..1.5).contains(&phase_val) {
if temp < boil_t && lh > lh_fusion {
let released = crate::det_math::min(boil_t - temp, lh - lh_fusion);
temp += released;
lh -= released;
}
if temp < melt_t && lh > 0.0 {
let released = crate::det_math::min(melt_t - temp, lh);
temp += released;
lh -= released;
}
if temp < melt_t && lh <= 0.0 {
phase_val = 0.0;
lh = 0.0;
}
}
// Solid: a partly-melted cell that cools gives its progress back
if phase_val < 0.5 && temp < melt_t && lh > 0.0 {
let released = crate::det_math::min(melt_t - temp, lh);
temp += released;
lh -= released;
}
self.temperature.data[i] = temp;
self.phase.data[i] = phase_val;
self.latent_heat.data[i] = lh;
}
}
/// Accumulate SDF offsets based on phase state
fn accumulate_offsets(&mut self, dt: f32) {
let liquid_flow = self.config.liquid_flow_speed;
let gas_expand = self.config.gas_expansion_rate;
let gas_dissipate = self.config.gas_dissipation_rate;
let max_offset = self.config.max_offset;
let n = self.sdf_offset.cell_count();
for i in 0..n {
let phase_val = self.phase.data[i];
if phase_val >= 1.5 {
// Gas: expand and dissipate (increase SDF = material removed)
self.sdf_offset.data[i] = crate::det_math::min(
(gas_expand + gas_dissipate).mul_add(dt, self.sdf_offset.data[i]),
max_offset,
);
} else if phase_val >= 0.5 {
// Liquid: slight material softening
self.sdf_offset.data[i] = crate::det_math::min(
(liquid_flow * 0.1).mul_add(dt, self.sdf_offset.data[i]),
max_offset,
);
}
// Solid: no offset change
}
// Liquid gravity flow: move a fraction `liquid_flow · dt` (≤ 1) of
// each liquid cell's offset to the cell below. The amount removed
// from the source equals the amount the destination accepts (its
// `max_offset` head-room), so the column total is conserved.
if liquid_flow > 0.0 {
let fraction = crate::det_math::min(liquid_flow * dt, 1.0);
let nx = self.sdf_offset.nx;
let ny = self.sdf_offset.ny;
let nz = self.sdf_offset.nz;
for iz in 0..nz {
for ix in 0..nx {
for iy in 1..ny {
let idx = self.phase.index(ix, iy, iz);
let below_idx = self.phase.index(ix, iy - 1, iz);
if !(0.5..1.5).contains(&self.phase.data[idx]) {
continue;
}
let source = self.sdf_offset.data[idx];
if source <= 0.0 {
continue;
}
let room =
crate::det_math::max(max_offset - self.sdf_offset.data[below_idx], 0.0);
let transfer = crate::det_math::min(source * fraction, room);
if transfer > 0.0 {
self.sdf_offset.data[below_idx] += transfer;
self.sdf_offset.data[idx] = source - transfer;
}
}
}
}
}
}
}
impl PhysicsModifier for PhaseChangeModifier {
#[inline]
fn modify_distance(&self, x: f32, y: f32, z: f32, original_dist: f32) -> f32 {
if !self.enabled {
return original_dist;
}
let offset = self.sdf_offset.sample(x, y, z);
original_dist + offset
}
fn update(&mut self, dt: f32) {
if !self.enabled {
return;
}
// 1. Diffuse temperature
self.temperature.diffuse(dt, self.config.diffusion_rate);
// 2. Cool toward ambient
self.temperature.decay_toward(
self.config.ambient_temperature,
self.config.cooling_rate,
dt,
);
// 3. Process phase transitions (enthalpy redistribution, dt-free)
self.process_transitions();
// 4. Accumulate SDF offsets
self.accumulate_offsets(dt);
}
fn name(&self) -> &'static str {
"phase_change"
}
fn is_active(&self) -> bool {
self.enabled
}
}
// ============================================================================
// Coupling channel
// ============================================================================
/// The temperature field is shared through a deterministic `Fix128` channel.
///
/// The counterpart of the impl on [`crate::thermal::ThermalModifier`]: the two
/// modifiers hold independent copies of the same physical quantity, and
/// [`crate::coupled_field::reconcile_mean`] is what makes a point have one
/// temperature instead of two.
impl CoupledScalar for PhaseChangeModifier {
fn coupled_name(&self) -> &'static str {
"temperature"
}
fn coupled_channel(&self) -> Result<CoupledField, CoupledFieldError> {
CoupledField::try_matching(&self.temperature)
}
fn publish(&self, out: &mut CoupledField) -> Result<(), CoupledFieldError> {
out.copy_from_f32(&self.temperature)
}
fn adopt(&mut self, src: &CoupledField) -> Result<(), CoupledFieldError> {
src.write_to_f32(&mut self.temperature)
}
fn check_adopt(&self, src: &CoupledField) -> Result<(), CoupledFieldError> {
src.require_same_grid_f32(&self.temperature)
}
}
// ============================================================================
// World participant
// ============================================================================
impl PhaseChangeModifier {
/// Snapshot tag of this type as a world participant: `"PHAS"`, the four
/// ASCII bytes read big endian. Never changes.
pub const PARTICIPANT_KIND: ParticipantKind =
ParticipantKind::new(u32::from_be_bytes(*b"PHAS"));
fn decode_state(bytes: &[u8]) -> Result<Self, StateError> {
let mut r = StateReader::new(bytes)?;
let config = PhaseChangeConfig {
melt_temperature: r.f32()?,
boil_temperature: r.f32()?,
latent_heat_fusion: r.f32()?,
latent_heat_vaporization: r.f32()?,
diffusion_rate: r.f32()?,
ambient_temperature: r.f32()?,
cooling_rate: r.f32()?,
liquid_flow_speed: r.f32()?,
gas_expansion_rate: r.f32()?,
gas_dissipation_rate: r.f32()?,
max_offset: r.f32()?,
};
let enabled = r.bool()?;
let temperature = r.field()?;
let phase = r.field()?;
let latent_heat = r.field()?;
let sdf_offset = r.field()?;
r.finish()?;
Ok(Self {
config,
temperature,
phase,
latent_heat,
sdf_offset,
enabled,
})
}
}
/// One `update(h)` per world substep (`h` converted with
/// [`Fix128::to_f32`]); see the module documentation of
/// [`crate::sim_modifier`] for the payload and what is not coupled yet.
///
/// Observations: channel 0 the highest cell temperature (absent for a grid
/// without cells), channel 1 the number of cells that are not solid
/// (stored phase ≥ 0.5), channel 2 the total SDF offset.
impl Participant for PhaseChangeModifier {
fn kind(&self) -> ParticipantKind {
Self::PARTICIPANT_KIND
}
fn substep(&mut self, _ctx: &mut SubstepCtx<'_>, h: Fix128) -> Result<(), ParticipantFault> {
PhysicsModifier::update(self, h.to_f32());
Ok(())
}
fn observe(&self, out: &mut ObservationSink) {
observe_max(out, 0, &self.temperature);
let fluid = self.phase.data.iter().filter(|&&p| p >= 0.5).count();
out.push(1, Fix128::from_int(fluid as i64));
observe_sum(out, 2, &self.sdf_offset);
}
fn write_state(&self, out: &mut Vec<u8>) {
let mut w = StateWriter::new(out);
let c = &self.config;
for v in [
c.melt_temperature,
c.boil_temperature,
c.latent_heat_fusion,
c.latent_heat_vaporization,
c.diffusion_rate,
c.ambient_temperature,
c.cooling_rate,
c.liquid_flow_speed,
c.gas_expansion_rate,
c.gas_dissipation_rate,
c.max_offset,
] {
w.f32(v);
}
w.bool(self.enabled);
w.field(&self.temperature);
w.field(&self.phase);
w.field(&self.latent_heat);
w.field(&self.sdf_offset);
}
fn check_state(&self, bytes: &[u8]) -> Result<(), StateError> {
Self::decode_state(bytes).map(drop)
}
fn read_state(&mut self, bytes: &[u8]) {
match Self::decode_state(bytes) {
Ok(state) => *self = state,
Err(e) => panic!("read_state called with a payload check_state refuses: {e:?}"),
}
}
}
// ============================================================================
// Tests
// ============================================================================
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_phase_change_no_heat() {
let config = PhaseChangeConfig::default();
let modifier = PhaseChangeModifier::new(config, 8, (-2.0, -2.0, -2.0), (2.0, 2.0, 2.0));
// No heat: everything is solid, no offset
let d = modifier.modify_distance(0.0, 0.0, 0.0, -1.0);
assert!(
(d - (-1.0)).abs() < 0.01,
"No heat should not change SDF, got {d}"
);
assert_eq!(modifier.phase_at(0.0, 0.0, 0.0), Phase::Solid);
}
#[test]
fn test_melting_transition() {
let config = PhaseChangeConfig {
melt_temperature: 100.0,
latent_heat_fusion: 10.0,
ambient_temperature: 20.0,
cooling_rate: 0.0, // No cooling
diffusion_rate: 0.0, // No diffusion (prevent re-solidification)
..Default::default()
};
let mut modifier = PhaseChangeModifier::new(config, 8, (-2.0, -2.0, -2.0), (2.0, 2.0, 2.0));
// Apply heat well above melt temperature (large radius for low-res grid)
modifier.apply_heat_at(0.0, 0.0, 0.0, 2000.0, 1.5);
// Simulate enough steps for transition
for _ in 0..100 {
modifier.update(0.016);
}
let phase = modifier.phase_at(0.0, 0.0, 0.0);
assert!(
phase == Phase::Liquid || phase == Phase::Gas,
"High heat should melt material, got {phase:?}"
);
}
#[test]
fn test_vaporization_transition() {
let config = PhaseChangeConfig {
melt_temperature: 50.0,
boil_temperature: 100.0,
latent_heat_fusion: 1.0,
latent_heat_vaporization: 1.0,
ambient_temperature: 20.0,
cooling_rate: 0.0,
diffusion_rate: 0.0,
..Default::default()
};
let mut modifier = PhaseChangeModifier::new(config, 8, (-2.0, -2.0, -2.0), (2.0, 2.0, 2.0));
// Apply extreme heat (large radius for low-res grid)
modifier.apply_heat_at(0.0, 0.0, 0.0, 5000.0, 1.5);
for _ in 0..200 {
modifier.update(0.016);
}
let phase = modifier.phase_at(0.0, 0.0, 0.0);
assert_eq!(
phase,
Phase::Gas,
"Extreme heat should vaporize, got {phase:?}"
);
// Gas should have increased SDF offset
let offset = modifier.sdf_offset.sample(0.0, 0.0, 0.0);
assert!(offset > 0.0, "Gas should add SDF offset, got {offset}");
}
#[test]
fn test_solidification() {
let config = PhaseChangeConfig {
melt_temperature: 50.0,
latent_heat_fusion: 1.0,
ambient_temperature: 20.0,
cooling_rate: 0.0,
diffusion_rate: 0.0,
..Default::default()
};
let mut modifier = PhaseChangeModifier::new(config, 8, (-2.0, -2.0, -2.0), (2.0, 2.0, 2.0));
// Heat to melt (large radius for low-res grid)
modifier.apply_heat_at(0.0, 0.0, 0.0, 2000.0, 1.5);
for _ in 0..50 {
modifier.update(0.016);
}
// Should be liquid by now
let phase_hot = modifier.phase_at(0.0, 0.0, 0.0);
assert!(
phase_hot == Phase::Liquid || phase_hot == Phase::Gas,
"Should be liquid/gas after heating, got {phase_hot:?}"
);
// Enable fast cooling and cool down
modifier.config.cooling_rate = 5.0;
for _ in 0..500 {
modifier.update(0.016);
}
let phase_cold = modifier.phase_at(0.0, 0.0, 0.0);
assert_eq!(
phase_cold,
Phase::Solid,
"Should solidify after cooling, got {phase_cold:?}"
);
}
fn still_config() -> PhaseChangeConfig {
PhaseChangeConfig {
melt_temperature: 200.0,
boil_temperature: 500.0,
latent_heat_fusion: 50.0,
latent_heat_vaporization: 100.0,
diffusion_rate: 0.0,
ambient_temperature: 20.0,
cooling_rate: 0.0,
liquid_flow_speed: 0.0,
gas_expansion_rate: 0.5,
gas_dissipation_rate: 0.25,
max_offset: 3.0,
}
}
/// oracle: a gas cell holding the full buffer `L_f + L_v = 150` at 400
/// (100 below boiling) condenses: it releases `min(100, 150 − 50) = 100`,
/// so it ends liquid at 500 with buffer `L_f = 50`.
#[test]
fn gas_condenses_to_liquid_closed_form() {
let mut m = PhaseChangeModifier::new(still_config(), 1, (0.0, 0.0, 0.0), (1.0, 1.0, 1.0));
m.phase.data[0] = 2.0;
m.latent_heat.data[0] = 150.0;
m.temperature.data[0] = 400.0;
m.process_transitions();
assert_eq!(m.phase.data[0], 1.0);
assert_eq!(m.temperature.data[0], 500.0);
assert_eq!(m.latent_heat.data[0], 50.0);
}
/// oracle: a gas cell grows its offset by `(expansion + dissipation)·dt =
/// 0.75·(1/2) = 3/8` per call and stops at `max_offset`; a solid cell
/// gets nothing.
#[test]
fn gas_offset_grows_and_is_capped() {
let mut m = PhaseChangeModifier::new(still_config(), 2, (0.0, 0.0, 0.0), (1.0, 1.0, 1.0));
m.phase.data[0] = 2.0;
m.accumulate_offsets(0.5);
assert_eq!(m.sdf_offset.data[0], 0.375);
assert_eq!(m.sdf_offset.data[1], 0.0);
m.accumulate_offsets(100.0);
assert_eq!(m.sdf_offset.data[0], 3.0);
}
/// oracle: with every cell liquid, flow speed 1 and `dt = 1/2`, each cell
/// first gains `0.1·1·(1/2) = 1/20`; then a fraction `1·(1/2)` of the
/// upper cell flows down: lower `1/20 + 1/40 = 3/40`, upper `1/40`. In a
/// column whose lower cell is at `max_offset` there is no room, and an
/// upper cell with a negative offset has nothing to give.
#[test]
fn liquid_flows_down_with_room_and_source() {
let mut m = PhaseChangeModifier::new(
PhaseChangeConfig {
liquid_flow_speed: 1.0,
..still_config()
},
2,
(0.0, 0.0, 0.0),
(1.0, 1.0, 1.0),
);
m.phase.data.fill(1.0);
let full_below = m.sdf_offset.index(1, 0, 0);
let full_above = m.sdf_offset.index(1, 1, 0);
m.sdf_offset.data[full_below] = 3.0;
let neg_below = m.sdf_offset.index(0, 0, 1);
let neg_above = m.sdf_offset.index(0, 1, 1);
m.sdf_offset.data[neg_above] = -1.0;
m.accumulate_offsets(0.5);
let below = m.sdf_offset.index(0, 0, 0);
let above = m.sdf_offset.index(0, 1, 0);
assert!((m.sdf_offset.data[below] - 0.075).abs() < 1e-6);
assert!((m.sdf_offset.data[above] - 0.025).abs() < 1e-6);
assert_eq!(m.sdf_offset.data[full_below], 3.0);
assert!((m.sdf_offset.data[full_above] - 0.05).abs() < 1e-6);
assert!((m.sdf_offset.data[neg_above] + 0.95).abs() < 1e-6);
assert!((m.sdf_offset.data[neg_below] - 0.05).abs() < 1e-6);
}
/// oracle: a disabled modifier leaves the distance and its fields as
/// they are.
#[test]
fn disabled_phase_change_is_inert() {
let mut m = PhaseChangeModifier::new(still_config(), 1, (0.0, 0.0, 0.0), (1.0, 1.0, 1.0));
m.sdf_offset.data[0] = 0.5;
m.temperature.data[0] = 900.0;
m.enabled = false;
assert!(!m.is_active());
assert_eq!(m.modify_distance(0.5, 0.5, 0.5, 1.0), 1.0);
m.update(1.0);
assert_eq!(m.temperature.data[0], 900.0);
assert_eq!(m.phase.data[0], 0.0);
}
fn sample_modifier() -> PhaseChangeModifier {
let mut m = PhaseChangeModifier::new(
PhaseChangeConfig {
melt_temperature: 150.0,
boil_temperature: 450.0,
latent_heat_fusion: 40.0,
latent_heat_vaporization: 80.0,
diffusion_rate: 0.125,
ambient_temperature: 22.5,
cooling_rate: 0.25,
liquid_flow_speed: 0.5,
gas_expansion_rate: 0.375,
gas_dissipation_rate: 0.625,
max_offset: 2.5,
},
2,
(-1.0, -2.0, -3.0),
(1.0, 2.0, 3.0),
);
for (i, v) in m.temperature.data.iter_mut().enumerate() {
*v = 100.0 + i as f32;
}
m.phase.data[1] = 1.0;
m.phase.data[2] = 2.0;
m.latent_heat.data[1] = 40.0;
m.sdf_offset.data[2] = 0.75;
m.enabled = false;
m
}
/// oracle: the participant kind is `"PHAS"` read big endian; a payload
/// written by `write_state` is accepted and read back into a different
/// modifier, which then has the original config, flag and the four
/// fields and writes the same bytes. Observations: channel 0 the hottest
/// cell (107), channel 1 the liquid or gas cells (2), channel 2 the total
/// offset (3/4).
#[test]
fn participant_state_round_trip_and_observations() {
assert_eq!(
PhaseChangeModifier::PARTICIPANT_KIND,
ParticipantKind::new(0x5048_4153)
);
let src = sample_modifier();
assert_eq!(
Participant::kind(&src),
PhaseChangeModifier::PARTICIPANT_KIND
);
let mut bytes = Vec::new();
src.write_state(&mut bytes);
assert_eq!(src.check_state(&bytes), Ok(()));
let mut dst = PhaseChangeModifier::new(
PhaseChangeConfig::default(),
1,
(0.0, 0.0, 0.0),
(1.0, 1.0, 1.0),
);
dst.read_state(&bytes);
assert_eq!(dst.config, src.config);
assert!(!dst.enabled);
assert_eq!(dst.temperature.data, src.temperature.data);
assert_eq!(dst.phase.data, src.phase.data);
assert_eq!(dst.latent_heat.data, src.latent_heat.data);
assert_eq!(dst.sdf_offset.data, src.sdf_offset.data);
let mut again = Vec::new();
dst.write_state(&mut again);
assert_eq!(again, bytes);
let mut sink = ObservationSink::new();
dst.observe(&mut sink);
assert_eq!(
sink.values(),
&[
(0, Fix128::from_int(107)),
(1, Fix128::from_int(2)),
(2, Fix128::from_ratio(3, 4))
]
);
}
/// oracle: the enabled flag sits after the version (4) and eleven `f32`s
/// (44), at offset 48; a value other than 0/1 is refused, as is a
/// payload one byte short or long.
#[test]
fn participant_state_refusals() {
let src = sample_modifier();
let mut bytes = Vec::new();
src.write_state(&mut bytes);
assert_eq!(bytes[48], 0);
let mut bad = bytes.clone();
bad[48] = 5;
assert_eq!(src.check_state(&bad), Err(StateError::InvalidValue));
assert!(matches!(
src.check_state(&bytes[..bytes.len() - 1]),
Err(StateError::Length { .. })
));
let mut long = bytes;
long.push(0);
assert!(matches!(
src.check_state(&long),
Err(StateError::Length { .. })
));
}
#[test]
#[should_panic(expected = "read_state called with a payload check_state refuses")]
fn participant_read_state_panics_on_a_refused_payload() {
let mut m = sample_modifier();
m.read_state(&[1, 0, 0]);
}
/// oracle: in a world of one substep `dt = 1/4`, a one-cell solid at 220
/// (melt 200, fusion buffer 50, no diffusion or cooling) absorbs
/// `min(20, 50) = 20` into its buffer and stays solid at 200: channel 0
/// reads 200, channel 1 (fluid cells) 0, channel 2 (offset) 0.
#[test]
fn participant_substep_in_a_world() {
let mut m = PhaseChangeModifier::new(still_config(), 1, (0.0, 0.0, 0.0), (1.0, 1.0, 1.0));
m.temperature.data[0] = 220.0;
let mut world = crate::solver::PhysicsWorld::new(crate::solver::SolverConfig {
substeps: 1,
..Default::default()
});
world.add_participant(Box::new(m)).expect("register");
world.step(Fix128::from_ratio(1, 4));
let Some(crate::world_participant::Observed::Exact(sink)) = world.observe_participant(0)
else {
panic!("observation");
};
assert_eq!(
sink.values(),
&[
(0, Fix128::from_int(200)),
(1, Fix128::ZERO),
(2, Fix128::ZERO)
]
);
}
}