I'm writing some code at the moment that schematically looks like:
(set initial conditions for some differential equation for given parameters (M,R,...))
M=1;
R=1:
(NDSolve differential equation taking ~10mins or more to give solution dependent on some more paramaters (w,l,m); defining a memoized function)
Phi[w_?NumericQ,l_?IntegerQ,m_?IntegerQ]:=Phi[w,l,m]= "Interpolated Func result of NDSolve taking 10mins to compute"
(For a given parameter set (w,l,m) compute a few things, such as coefficients and the (w,l,m) term in a sum, for which numerical differentiation ND[..] is needed, hence the necessity of memoizing this Phi to speed this step up)
A[w,l,m]:= "some calc dependent on the Phi";
B[w,l,m]:= "some calc dependent on the Phi"
termSum[w,l,m]:= "some calc dependent on the Phi and A,B"
(write termSum to file)
PutAppend[...]
(clear cache of the memoized Phi[w,l,m] interp func and the other variables like A[w,l,m])
Phi[1,2,3]:=. (*unset*)
. . . (after the rinse now repeat for a different (w,l,m))
Implemented by some 'do' or 'for' loop.
Some issues I'm considering:
1) Is there a way to use Block/Module say to allow me to better handle my caching of the variables (especially the huge memory hogs like the Phi that is a very large Interpolating Function)?
2) Also if I change my initial parameters M, R etc. I have to start all over again with setting initial conditions and so forth and functions that depend. Could I use Block/Module to incorporate this?
3) Is there a way to run some kind of initialization script in Mathematica that will set my constants, run my standard definitions etc. each time I load the notebook?
4) In a later calculation I will probably use the Phi,A,B for a given (w,l,m) all over again but for different values of 'r' along the range of the interpolating function. I've thought of saving to hard memory the actual interpolating functions of a given (w,l,m) but normal Save leads to 200MB files, and even DumpSave >20MB. Anything I could do about this or do I just have to recompute?
Block, so that your definitions are only valid in it.Internal`InheritedBlockcan let you add local memoization without you losing the function definition. Also, what's the problem of a >20MB DumpSave? I would definately go for that: memoization inside anInheritedBlock(orBlock), dump saving the result for later use. – Rojo Aug 01 '12 at 18:18DumpSavecan save all your definitions in a single file (question 3) ). Also think of having an external script (a .m file to call with Get), initialization cell. You could even store your already calculated data hidden in your notebook. It all depends on your taste and use case – Rojo Aug 01 '12 at 18:20