← All examples
67

Canonical Grid model

Physics starter - Diffusion

Heat equation on the unit interval with zero boundaries and a sine initial mode; separate spatial and temporal error.

Scale
Small
Source
physics-diffusion.grid
Length
68 lines
Collection
Advanced language
Level
Intermediate
Runtime
Portable
Version
1.0.0
physics-diffusion.grid
Get Grid
MODEL "Physics starter - Diffusion"
DESCRIPTION "Heat equation on the unit interval with zero boundaries and a sine initial mode; separate spatial and temporal error."
VERSION "1.0.0"
AUTHOR "Grid Team"
TAGS "physics", "starter", "diffusion"

# Heat equation u_t=u_xx on [0,1], zero fixed boundaries, sine initial mode.
input EndTime = 0.1
  VALIDATE BETWEEN 0.01 AND 0.2 MESSAGE "Use time from 0.01 to 0.2; zero time has no spatial error from which to estimate an order."
output ContinuumCenter = EXP(-((PI()^2)*EndTime))
Field_21 = PDE_SOLVE_1D((time,position,value,gradient,curvature) => curvature,(position) => SIN(PI()*position),0,0,0,1,21,EndTime,{samples:2,rtol:1e-9,atol:1e-12})
output Center_21 = INDEX(Field_21,2,11)
Discrete_21 = EXP(-((4*20^2)*SIN(PI()/(2*20))^2*EndTime))
output Error_21 = ABS(Center_21-ContinuumCenter)
output TemporalError_21 = ABS(Center_21-Discrete_21)
Field_41 = PDE_SOLVE_1D((time,position,value,gradient,curvature) => curvature,(position) => SIN(PI()*position),0,0,0,1,41,EndTime,{samples:2,rtol:1e-9,atol:1e-12})
output Center_41 = INDEX(Field_41,2,21)
Discrete_41 = EXP(-((4*40^2)*SIN(PI()/(2*40))^2*EndTime))
output Error_41 = ABS(Center_41-ContinuumCenter)
output TemporalError_41 = ABS(Center_41-Discrete_41)
output Field_81 = PDE_SOLVE_1D((time,position,value,gradient,curvature) => curvature,(position) => SIN(PI()*position),0,0,0,1,81,EndTime,{samples:2,rtol:1e-9,atol:1e-12})
output Center_81 = INDEX(Field_81,2,41)
Discrete_81 = EXP(-((4*80^2)*SIN(PI()/(2*80))^2*EndTime))
output Error_81 = ABS(Center_81-ContinuumCenter)
output TemporalError_81 = ABS(Center_81-Discrete_81)
output ObservedOrder = LN(Error_21/Error_41)/LN(2)
output ObservedOrderFine = LN(Error_41/Error_81)/LN(2)

# Scientific checks and interpretation
TightField = PDE_SOLVE_1D(
  (time, position, value, gradient, curvature) => curvature,
  (position) => SIN(PI() * position), 0, 0, 0, 1, 81, EndTime,
  {samples: 2, rtol: 1e-11, atol: 1e-14})
output TightTemporalError = ABS(INDEX(TightField, 2, 41) - Discrete_81)
output Profile = MAKEARRAY(81, 3, (row, col) => col = 1 THEN (row - 1) / 80
  ELSE col = 2 THEN INDEX(TightField, 2, row)
  ELSE EXP(-(PI()^2) * EndTime) * SIN(PI() * (row - 1) / 80))

output Assumptions = "Heat equation on the unit interval with zero boundaries and a sine initial mode; separate spatial and temporal error. Solver completion alone does not establish global accuracy or physical validity."
output ScenarioGuide = "Save a Baseline input scenario, change an admitted input, inspect numerical checks and the unsmoothed plot, then restore Baseline. Save and reopen a .gridoc archive. Plot selection also accepts the Profile numeric array; choose its X and Y columns in the chart dialog."

Charts_1!type = "charts"
Charts_1!config = <toml>
  version = 1
  kind = "charts"
  title = "Diffusion"
  [layout]
  x_axis = "Position"
  y_axis = "Temperature"
  [[charts]]
  type = "line"
  smooth = false
  bind = "Profile"
  x = "0"
  y = "1"
  label = "81-node solution"
  color = "#2563eb"
  [[charts]]
  type = "line"
  smooth = false
  bind = "Profile"
  x = "0"
  y = "2"
  label = "Continuum solution"
  color = "#d97706"
</toml>

END MODEL