Growing Crystal
A small crystal grows as its surroundings cool. Warming them back up can melt it. A simplified solidification model.
Published
No video preview yet
Source
use "@hyle/maths";
// Thermal phase-field solidification of an undercooled pure melt in lattice units.
// Solid fraction phi follows an Allen-Cahn double well; heat diffusion and latent
// heat oppose growth. Cubic kinetic anisotropy favors the six crystal axes.
// Both equations advance by dt=0.15: 25% more model time per step than dt=0.12.
// Keep the bath rate scaled with dt; latent heat uses the actual solid change.
// This is qualitative solidification, not a calibrated mineral/solution chemistry model.
property CrystalPhase {
solid: Float [0.0, 1.0] = 0.0;
temperature: Float [-2.0, 2.0] = -0.55;
change: Float [-1.0, 1.0] = 0.0;
}
model CrystalMelt : CrystalPhase;
// Bath coupling is zero in the closed energy-conservation experiment.
input cooling: Float [0.0, 1.0] = 0.15;
input bathTemperature: Float [-0.8, 0.3] = -0.55;
world { dimensions = 3; neighborhood = VonNeumann; }
behavior PlanSolidification for CrystalPhase {
update {
let lap = sum n in neighbors[CrystalPhase using VonNeumann] { n.solid-self.solid };
let dx=(neighbors[CrystalPhase using VonNeumann,+X].solid else self.solid)-(neighbors[CrystalPhase using VonNeumann,-X].solid else self.solid);
let dy=(neighbors[CrystalPhase using VonNeumann,+Y].solid else self.solid)-(neighbors[CrystalPhase using VonNeumann,-Y].solid else self.solid);
let dz=(neighbors[CrystalPhase using VonNeumann,+Z].solid else self.solid)-(neighbors[CrystalPhase using VonNeumann,-Z].solid else self.solid);
let g2=dx*dx+dy*dy+dz*dz;
let cubic=(dx*dx*dx*dx+dy*dy*dy*dy+dz*dz*dz*dz)/maths::maximum(g2*g2,0.00000001);
let mobility=0.02+0.98*cubic*cubic;
let driving=maths::clip(-self.temperature, -0.45,0.45);
let reaction=self.solid*(1.0-self.solid)*(self.solid-0.5+driving);
let next=maths::clip(self.solid+0.15*mobility*(lap+reaction),0.0,1.0);
set self.change=next-self.solid;
}
}
// H = temperature - solid is conserved with cooling=0 and insulating boundaries.
// Solidification releases latent heat; melting consumes exactly the same amount.
behavior SolidifyAndDiffuseHeat for CrystalPhase
needs inputs(cooling: Float [0.0, 1.0], bathTemperature: Float [-0.8, 0.3]) {
update {
let lap=sum n in neighbors[CrystalPhase using VonNeumann] { n.temperature-self.temperature };
set self.solid=maths::clip(self.solid+self.change,0.0,1.0);
set self.temperature=maths::clip(self.temperature+0.15*lap+self.change+0.01*cooling*(bathTemperature-self.temperature),-2.0,2.0);
}
}
phase interface_motion { run PlanSolidification on CrystalMelt; }
phase thermal_balance { run SolidifyAndDiffuseHeat on CrystalMelt; }
visual CrystalSurface for CrystalPhase {
color {
let warm=maths::clip((self.temperature+0.55)*1.8,0.0,1.0);
rgba(0.18+0.65*warm,0.8-0.35*warm,1.0-0.18*warm,maths::clip((self.solid-0.08)*1.4,0.0,1.0))
}
}
visualize CrystalMelt with CrystalSurface;