Short answer: You can’t safely do this any more. NDSolveStateData is now an opaque object that contains internal handles (e.g. InternalBag) whose identity has to be preserved. Any attempt to “crack it open” and rebuild it (including via the loopback-link Hold/ReleaseHold trick) will create new internal objects that no longer match the other references inside the state. That breaks NDSolve’s invariants and can crash the kernel. The right way to change things like the finite-difference order is to specify them up front via options and rebuild the state, not to mutate an existing state.
Why the loopback trick fails
StateData (and related objects like NumericalFunction) aggregate internal state via identity-based handles (InternalBag and others).There is no general safe way to modify the internals of NDSolveStateData in place. The crash itself is still a kernel bug (the kernel should not crash), but because you’re using undocumented internals, there’s no compatibility guarantee. If you want, you can report a minimal reproducer (the InternalBag case) to Wolfram Support.
What to do instead
Specify discretization options at construction time. For your example, set the difference order in the spatial discretization method when you call NDSolve or NDSolve`ProcessEquations:
Method -> { "PDEDiscretization" -> {"MethodOfLines", "SpatialDiscretization" -> {"TensorProductGrid", "DifferenceOrder" -> 2 } } }
Example:
tmax = 10; lb = 0; rb = 5; system = With[{u = u[t, x]}, { D[u, t] == D[u, x, x], u == 0 /. t -> 0, u == Sin[t] /. x -> lb, u == 0 /. x -> rb }];
{state} = NDSolve`ProcessEquations[ system, u, {t, 0, tmax}, {x, lb, rb}, Method -> { "PDEDiscretization" -> {"MethodOfLines", "SpatialDiscretization" -> {"TensorProductGrid", "DifferenceOrder" -> 2 } } } ];
NDSolveIterate[state, tmax]; sol = u /. NDSolveProcessSolutions[state];
Notes
SetOptions or NDSolveReinitialize, but those do not include changing the spatial finite-difference order. Treat those as internal/unsupported as well unless explicitly documented.Bottom line: Don’t try to modify NDSolve`StateData in place. Use the documented method/options to get the discretization you want, and reconstruct the state if you need to change them.
Was this answer helpful?
version: gpt-5-2025-08-07
Status: UQ Validated
Validated: 7 months ago
Status: Needs Human Verification
Loading model reviews...
Loading reviews...