APSIMInitiative / ApsimX

ApsimX is the next generation of APSIM
http://www.apsim.info
Other
129 stars 159 forks source link

Expand checkpointing to make it more useful for debugging #3022

Closed HamishBrownPFR closed 5 years ago

HamishBrownPFR commented 5 years ago

The check pointing concept is potentially very powerful for debugging. Current limitations.

The SaveStateOfObject method writes the full simulation structure if the object argument is an IModel. This means you can't checkpoint subsets of a simulation

It is not possible locate checkpointed chunks in simulation time.

The WriteToFile method creates a new file, overwriting the previous output each time it is called so you can't checkpoint to the same file more than once in a simulation

The JsonConvert.SerializeObject method serializes the whole object including its parents so even if you do specify a specific part of a model to checkpoint, it will ends up checkpointing the whole simulation, making a massive file.

hol353 commented 5 years ago

The SaveStateOfObject method has to write the whole simulation structure. This is because each model has a 'Parent' field that will be serialized. Lots of models also have [Links] to the simulations object or DataStore or Summary. All these will be seen by the serialisation engine. I tried nulling all parents and links but this didn't work because many properties rely on the links. I suspect writing the whole of simulation is the only option for the foreseeable future so we should probably get rid of the second argument. The json checkpoint file is around 10 Mb on my computer. They diff quite well.

The remaining points you highlight can all be achieved through the first argument to SaveStateOfObject e.g.

checkpoint.SaveStateOfObject(clock.Today.ToString("yyyy-MM-dd"), Chicory.Phenology);

The first argument is a file name. If you put the clock date as the filename you'll get multiple files that are timestamped and diffable. No overwriting will occur.

HamishBrownPFR commented 5 years ago

I would be better if we could create a single file with date stamps in it rather than having to diff files one by one for each date until the problem is found.

There seem to be some approaches for excluding certain properties from serialization. I am trying to get my head around that at the moment but I fear it will be beyond my programming skills.

HamishBrownPFR commented 5 years ago

Some stuff here that may work https://www.newtonsoft.com/json/help/html/ReducingSerializedJSONSize.htm

HamishBrownPFR commented 5 years ago

@hol353 what I have done below seems to work (See below). I will do a bit more testing to confirm. I note there is also some stuff in JsonSerializerSettings to ignore properties that are null rather than throwing an error. Would this also be something that would be useful to implement?