← All examples
75

Canonical Grid model

Physics starter - Stiff

Two timescales with exact states, readable solver reports, and deliberate tolerance refinement.

Scale
Small
Source
physics-stiff.grid
Length
53 lines
Collection
Advanced language
Level
Intermediate
Runtime
Portable
Version
1.0.0
physics-stiff.grid
Get Grid
MODEL "Physics starter - Stiff"
DESCRIPTION "Two timescales with exact states, readable solver reports, and deliberate tolerance refinement."
VERSION "1.0.0"
AUTHOR "Grid Team"
TAGS "physics", "starter", "stiff"

# Fast variable follows a slow exponentially decaying variable.
input EndTime = 1
  VALIDATE BETWEEN 0.1 AND 1 MESSAGE "Use an end time from 0.1 to 1."
output FastExact = (1000*EXP(-EndTime)-EXP(-1000*EndTime))/999
output SlowExact = EXP(-EndTime)
output AutoState = ODE_FINAL((time,state) => [-1000*(INDEX(state,1,1)-INDEX(state,1,2)),-INDEX(state,1,2)],[1,1],0,EndTime,{method:"auto",rtol:1e-6,atol:1e-9,max_steps:300})
output StiffState = ODE_FINAL((time,state) => [-1000*(INDEX(state,1,1)-INDEX(state,1,2)),-INDEX(state,1,2)],[1,1],0,EndTime,{method:"stiff",rtol:1e-6,atol:1e-9,max_steps:300})
output WorkReport = ODE_REPORT((time,state) => [-1000*(INDEX(state,1,1)-INDEX(state,1,2)),-INDEX(state,1,2)],[1,1],0,EndTime,{format:"record",method:"auto",rtol:1e-6,atol:1e-9,max_steps:300})

# Scientific checks and interpretation
output TighterReport = ODE_REPORT(
  (time, state) => [-1000 * (INDEX(state, 1, 1) - INDEX(state, 1, 2)), -INDEX(state, 1, 2)],
  [1, 1], 0, EndTime, {format: "record", method: "stiff", rtol: 1e-9, atol: 1e-12, max_steps: 1000})
output GlobalError = MAX(ABS(INDEX(StiffState, 1, 1) - FastExact), ABS(INDEX(StiffState, 1, 2) - SlowExact))
output TighterGlobalError = MAX(ABS(INDEX(TighterReport.state, 1, 1) - FastExact), ABS(INDEX(TighterReport.state, 1, 2) - SlowExact))
output StateComparison = [1, INDEX(StiffState, 1, 1), FastExact; 2, INDEX(StiffState, 1, 2), SlowExact]

output Assumptions = "Two timescales with exact states, readable solver reports, and deliberate tolerance refinement. 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 StateComparison 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 = "Stiff"
  [layout]
  x_axis = "Component"
  y_axis = "State value"
  [[charts]]
  type = "line"
  smooth = false
  bind = "StateComparison"
  x = "0"
  y = "1"
  label = "Stiff state"
  color = "#2563eb"
  [[charts]]
  type = "line"
  smooth = false
  bind = "StateComparison"
  x = "0"
  y = "2"
  label = "Exact state"
  color = "#d97706"
</toml>

END MODEL