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
//! 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 latent heat buffer.
//! At phase boundary temperatures, latent heat absorbs/releases energy
//! without changing temperature (plateau behavior). Phase transitions
//! modify the SDF: liquid flows downward, gas expands outward.
//!
//! Author: Moroya Sakamoto
use crate::sim_field::ScalarField3D;
use crate::sim_modifier::PhysicsModifier;
#[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 (energy absorbed during melting)
pub latent_heat_fusion: f32,
/// Latent heat of vaporization
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)
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
fn process_transitions(&mut self, dt: f32) {
let melt_t = self.config.melt_temperature;
let boil_t = self.config.boil_temperature;
let lh_fusion = self.config.latent_heat_fusion;
let lh_vaporization = self.config.latent_heat_vaporization;
let n = self.temperature.cell_count();
for i in 0..n {
let temp = self.temperature.data[i];
let phase_val = self.phase.data[i];
let lh = self.latent_heat.data[i];
// Current phase
let is_solid = phase_val < 0.5;
// Solid → Liquid transition
if is_solid && temp >= melt_t {
let excess = temp - melt_t;
let new_lh = excess.mul_add(dt, lh);
if new_lh >= lh_fusion {
self.phase.data[i] = 1.0; // Liquid
self.latent_heat.data[i] = 0.0;
} else {
self.latent_heat.data[i] = new_lh;
}
}
// Re-check phase after possible solid→liquid transition
let phase_val = self.phase.data[i];
let is_liquid = (0.5..1.5).contains(&phase_val);
// Liquid → Gas transition
if is_liquid && temp >= boil_t {
let excess = temp - boil_t;
let new_lh = excess.mul_add(dt, self.latent_heat.data[i]);
if new_lh >= lh_vaporization {
self.phase.data[i] = 2.0; // Gas
self.latent_heat.data[i] = 0.0;
} else {
self.latent_heat.data[i] = new_lh;
}
}
// Re-check phase for cooling transitions
let phase_val = self.phase.data[i];
let is_liquid = (0.5..1.5).contains(&phase_val);
let is_gas = phase_val >= 1.5;
// Gas → Liquid transition (cooling)
if is_gas && temp < boil_t {
self.phase.data[i] = 1.0; // Condense to liquid
self.latent_heat.data[i] = 0.0;
}
// Liquid → Solid transition (cooling)
if is_liquid && temp < melt_t {
self.phase.data[i] = 0.0; // Solidify
self.latent_heat.data[i] = 0.0;
}
}
}
/// 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] = (gas_expand + gas_dissipate)
.mul_add(dt, self.sdf_offset.data[i])
.min(max_offset);
} else if phase_val >= 0.5 {
// Liquid: slight material softening
self.sdf_offset.data[i] = (liquid_flow * 0.1)
.mul_add(dt, self.sdf_offset.data[i])
.min(max_offset);
}
// Solid: no offset change
}
// Liquid gravity flow: shift liquid offset downward
if liquid_flow > 0.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);
let phase_above = self.phase.data[idx];
// Transfer liquid offset downward
if (0.5..1.5).contains(&phase_above) {
let transfer = self.sdf_offset.data[idx] * liquid_flow * dt * 0.5;
if transfer > 0.0 {
self.sdf_offset.data[below_idx] =
(self.sdf_offset.data[below_idx] + transfer).min(max_offset);
self.sdf_offset.data[idx] -= transfer * 0.3;
}
}
}
}
}
}
}
}
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
self.process_transitions(dt);
// 4. Accumulate SDF offsets
self.accumulate_offsets(dt);
}
fn name(&self) -> &'static str {
"phase_change"
}
fn is_active(&self) -> bool {
self.enabled
}
}
// ============================================================================
// 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:?}"
);
}
}