Giter VIP home page Giter VIP logo

Comments (7)

skim0119 avatar skim0119 commented on September 23, 2024

Comment by armantekinalp
Thursday Dec 30, 2021 at 05:29 GMT


In future implementation of restart I think we should add these functionalities:

  • Current implementation allows only loading systems after finalize call. Instead we should call restart functions before finalize.
  • Curren implementation only saves systems i.e. rods, rigid bodies. As @skim0119 mentioned above, restart should save a copy of simulator and user can just load & run. However, some users might need more customization and they might want to only save rods but not the simulation. For those cases we should have an option to save/load systems or simulation.
  • We could also allow users to save simulator/systems every some steps. This might be useful for debugging purposes.

from pyelastica.

Anika-Roy avatar Anika-Roy commented on September 23, 2024

is this issue open?
Im a beginner and I'd like to work on it

from pyelastica.

skim0119 avatar skim0119 commented on September 23, 2024

@Anika-Roy Thank you for your interest. Yes, this issue is still open.
If you are interested in working, let me know, I can assign you.

from pyelastica.

anshusinha26 avatar anshusinha26 commented on September 23, 2024

@skim0119 Hello! I'm a beginner looking to make my first contribution to PyElastica, and I'm interested in working on the Simulation save/restart module issue (#33) listed under the good first issue label. I understand that this may involve some challenges, such as handling non-serialisable data and accounting for customised forces/connections, but I'm eager to learn and contribute to the project.
Could you please provide me with some guidance on how to get started with this issue?
Thank you!

from pyelastica.

anshusinha26 avatar anshusinha26 commented on September 23, 2024

Hi @skim0119 @bhosale2 , I'm interested in contributing to PyElastica and I noticed that issue #33 on simulation save/restart module is still open. Would it be possible to assign this issue to me? I would love to work on it as a beginner.
Thank you!

from pyelastica.

anshusinha26 avatar anshusinha26 commented on September 23, 2024

I have made changes in "restart.py" and here is the updated code:
doc = """Generate or load restart file implementations."""
all = ["save_state", "load_state"]
import numpy as np
import os
from itertools import groupby

def all_equal(iterable):
"""
Checks if all elements of list are equal.
Parameters
----------
iterable : list
Iterable list
Returns
-------
Boolean
References
----------
https://stackoverflow.com/questions/3844801/check-if-all-elements-in-a-list-are-identical
"""
g = groupby(iterable)
return next(g, True) and not next(g, False)

def save_state(simulator, directory: str = "", time=0.0, verbose: bool = False):
"""
Save state parameters of each rod.
TODO : environment list variable is not uniform at the current stage of development.
It would be nice if we have set list (like env.system) that iterates all the rods.
Parameters
----------
simulator : object
Simulator object.
directory : str
Directory path name. The path must exist.
time : float
Simulation time.
verbose : boolean

"""
os.makedirs(directory, exist_ok=True)
for idx, rod in enumerate(simulator):
    path = os.path.join(directory, "system_{}.npz".format(idx))
    np.savez(path, time=time,
             vertices=rod.vertices,
             edges=rod.edges,
             state_variables=rod.state_variables,
             forces=rod.forces,
             connections=rod.connections,
              **rod.__dict__)

    """
    In the modified version of save_state,
    we are saving the following new pieces of data:

        1. vertices: An array of the rod's vertices.
        2. edges: An array of the rod's edges.
        3. state_variables: A dictionary containing the values of any state variables the rod has.
        4. forces: A dictionary containing information about any forces acting on the rod.
        5. connections: A dictionary containing information about any connections the rod has.
    """

if verbose:
    print("Save complete: {}".format(directory))

def load_state(simulator, directory: str = "", verbose: bool = False):
"""
Load the rod-state. Compatibale with 'save_state' method.
If the save-file does not exist, it returns error.
Call this function after finalize method.

Parameters
----------
simulator : object
    Simulator object.
directory : str
    Directory path name.
verbose : boolean

Returns
------
time : float
    Simulation time of systems when they are saved.
"""
time_list = []  # Simulation time of rods when they are saved.
for idx, rod in enumerate(simulator):
    path = os.path.join(directory, "system_{}.npz".format(idx))
    data = np.load(path, allow_pickle=True)
    for key, value in data.items():
        if key == "time":
            time_list.append(value.item())
            continue

        if key == "vertices":
            rod.vertices[:] = value
        elif key == "edges":
            rod.edges[:] = value
        elif key == "state_variables":
            rod.state_variables.update(value.tolist())
        elif key == "forces":
            rod.forces.update(value.tolist())
        elif key == "connections":
            rod.connections.update(value.tolist())
        elif value.shape != ():
            # Copy data into placeholders
            getattr(rod, key)[:] = value
        else:
            # For single-value data
            setattr(rod, key, value)

if not all_equal(time_list):
    raise ValueError(
        "Restart time of loaded rods are different, check your inputs!"
    )

# Apply boundary conditions, after loading the systems.
simulator.constrain_values(0.0)
simulator.constrain_rates(0.0)

if verbose:
    print("Load complete: {}".format(directory))

return time_list[0]

from pyelastica.

anshusinha26 avatar anshusinha26 commented on September 23, 2024

@skim0119 @bhosale2 I'm facing some issues while committing the code:

Added the changes by "git add" and it worked perfectly fine.

Next step was to commit the changes, and I tried "git commit -m "Add a code for saving and loading simulation state",
but it showed and error:

Screenshot 2023-03-26 at 12 35 48 PM

Screenshot 2023-03-26 at 12 38 37 PM

Please help me with this!

from pyelastica.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.