Writing Chrono::System to file

30 views
Skip to first unread message

June Knauth

unread,
Jun 20, 2024, 2:00:49 PM (12 days ago) Jun 20
to ProjectChrono
Is there any way to serialize Chrono::Systems so that the system state is recoverable from disk? I'd like to be able to save the state of the system for debugging at a later date.

Dario Mangoni

unread,
Jun 20, 2024, 5:18:46 PM (12 days ago) Jun 20
to ProjectChrono
Hi,
yes there is.

You may find more references by searching for the "archive" keyword instead of serialization.
demo_CH_archive is a starting point, but it's probably showing many more features that just simply a Chrono system serialization.
utest_CH_archive is another source of information, while it's meant for testing.

In general you can import|export to JSON, XML and binary format.

In general, for a whole ChSystem, you can simply do something like.

{
    ChSystemNSC system;
    ADD THINGS TO YOUR SYSTEM
    std::ofstream mfileo("ChArchiveJSON_Pendulum.json");
    ChArchiveOutJSON archive_out(mfileo);
    archive_out << CHNVP(system);
}

std::ifstream mfilei("ChArchiveJSON_Pendulum.json");
ChArchiveInJSON archive_in(mfilei);

ChSystemNSC system;
archive_in >> CHNVP(system);


Please mind that not all the classes are enabled to serialization (e.g. ChVisualShapeFEA are not).
You may be able to check which are enabled by checking the relative ArchiveIn|ArchiveOut methods for each class.

Dario Mangoni

unread,
Jun 20, 2024, 5:24:31 PM (12 days ago) Jun 20
to ProjectChrono
BTW, if you just want to load/save the state of the system, without the model itself, you can come up with some easier yet not complete, solution of using ChSystem::StateGather and ChSystem::StateScatter.

Something like:

        auto state = chrono_types::make_shared<ChState>(system.GetNumCoordsPosLevel(), &system);
        auto state_der = chrono_types::make_shared<ChStateDelta>(system.GetNumCoordsVelLevel(), &system);
        double time;
        system.StateGather(*state   , *state_der, time_dummy);


and similarly for the Scattering (i.e. loading back into ChSystem)

Please notice however that you have to make sure that the model is exactly the same and also that can be additional relevant variables (e.g. some variable in the timesteppers) that is not retrieved in this way thus making a slight difference when you load back the state.

chao peng

unread,
Jun 21, 2024, 3:11:22 PM (11 days ago) Jun 21
to ProjectChrono
Hi,

Just complemented another point. You might also need to Gather and Scatter the acceleration and Lagrange multipliers if you want to recover the system state exactly as the snapshot investigated.

Something like:

        auto state_der2 = chrono_types::make_shared<ChStateDelta>(system.GetNumCoordsVelLevel(), &system);
        system.StateGatherAcceleration(*state_der2);

        auto state_L = chrono_types::make_shared<ChVectorDynamic<>>(system.GetNumConstraints());
        system.StateGatherReactions(*state_L);

and similarly for the Scattering (i.e. loading back into ChSystem).

The complete state of a system includes: X (position and orientation), V (velocity), A (acceleration), T (time), L( Lagrange multipliers).

Good luck.
Chao PENG.

June Knauth

unread,
Jun 24, 2024, 11:55:22 AM (8 days ago) Jun 24
to chao peng, ProjectChrono
Hey everyone, this is really helpful. Thanks for the info!

June

--
You received this message because you are subscribed to the Google Groups "ProjectChrono" group.
To unsubscribe from this group and stop receiving emails from it, send an email to projectchron...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/projectchrono/ee47e377-6e3b-4f00-9f74-23acb85fff90n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages