There is a problem that I was not able to solve for months, if not years, and I can't postpone it any longer. It is this: I know how to output the result of a computation from NDSolve into a file after NDSolve has finished completely, that is, the integration is completed from tinitial to tfinal. But I need to output at some control points of my choosing between tinitial and tfinal, because computation may run for hours and if smth. happens I will loose everything. If I saved a few intermediate results though, then I can re-start from the last one I saved. Here is the simplified code, which I trimmed to run for < 20 sec. I want to insert the output statements similar to the last Export statement in this code into WhenEvent[..] statement that I already have inside NDSolve (presently, it just outputs the t value reached by the integrator) , to dump the results at t=tinitial+deltat, t=tinitial+2deltat, etc. into the files with names like test1.csv, test2.csv, etc. In this example there would be 2 such files, corresponding to t=10000 and 20000. Please help. I am convinced that the solution will help many people. There is nothing remotely close in the posted questions and answers.
RHSH = D[H[x, t] D[H[x, t], x, x], x, x] +
1/2 D[D[H[x, t], x]^2, x, x] +
Subscript[Z, 4] D[H[x, t]^3 D[H[x, t], x, x, x, x], x, x] +
Subscript[Z, 13]
D[H[x, t]^2 D[H[x, t], x] D[H[x, t], x, x, x], x, x];
Nkmax = 0.0947; N\[Lambda]max = 2 Pi/Nkmax; domainlength =
20 N\[Lambda]max; NH0 = 11.47;
klsmall = 10; kllarge = 100; kf = 12; u = 5/2; \[Phi] = 1/2;
IniShapeH = NH0 + \[Phi] Cos[30 (2 Pi/domainlength) x] + u*\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(k = 1\), \(kf/
2\)]\((\((1/
RandomInteger[{klsmall,
kllarge}]\ )\) Cos[\((2\ Pi/
domainlength)\) RandomInteger[{2, 10}] x] + \((1/
RandomInteger[{klsmall,
kllarge}]\ )\) Sin[\((2\ Pi/
domainlength)\) RandomInteger[{2, 10}] x])\)\);
\[CapitalDelta]t = 10000; endtime = 30000;
Timing[solution =
NDSolve[{D[H[x, t], t] == RHSH /. {Subscript[Z, 4] -> 0.6322,
Subscript[Z, 13] -> 3.7932}, H[0, t] == H[domainlength, t],
H[x, 0] == IniShapeH,
WhenEvent[Mod[t, \[CapitalDelta]t] == 0, Print[t]]},
H, {x, 0, domainlength}, {t, 0, endtime},
AccuracyGoal -> MachinePrecision/2,
PrecisionGoal -> MachinePrecision/2, InterpolationOrder -> All,
Method -> {"MethodOfLines",
"SpatialDiscretization" -> {"TensorProductGrid",
"DifferenceOrder" -> 4, MinPoints -> 2500, MaxPoints -> 2500,
AccuracyGoal -> MachinePrecision/2,
PrecisionGoal -> MachinePrecision/2}, Method -> "BDF"}]]
Export["c:\\Test\\test.csv",
Table[Flatten[{x, H[x, 30000]} /. solution], {x, 0, domainlength,
domainlength/1000}]]

NDSolve`Iterate. See http://reference.wolfram.com/language/tutorial/NDSolveStateData.html – Michael E2 Dec 07 '17 at 21:29Print[i], you could save your data.) – Michael E2 Dec 08 '17 at 13:12