Comments (7)
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.
is this issue open?
Im a beginner and I'd like to work on it
from pyelastica.
@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.
@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.
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.
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.
@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:
Please help me with this!
from pyelastica.
Related Issues (20)
- Reproducing snake diffraction results from your Nat Comm Paper HOT 7
- Failure for Helix under Gravity Case HOT 2
- Can PyElastica support GPU acceleration? HOT 3
- Do PyElastica support to simulate a rod containing two segments? HOT 3
- Question about base length HOT 8
- Replicating side-winding results from your Nat-comm snake paper HOT 29
- Instant controler and simulation HOT 6
- Small scale rod simulation HOT 11
- Updating muscle forces mid-simulation - how does it work? HOT 1
- Small-scale simulation problem 2 HOT 1
- OneEndFixedBC didn't work for applying a external torque HOT 4
- Adding force and torque at same time HOT 5
- How to using PyElastica data to visualize in 3D models? HOT 1
- `FileNotFoundError` `python continuum_snake.py` HOT 1
- Bug and fix in 2_Slithering_Snake.ipynb HOT 1
- Task 1: Utilizing Python to clean and convert data from Elastica into a format recognizable by Blender. HOT 1
- Avoid having randomized unittest HOT 17
- [GSOC project 3] Binding PyElastica with Elastica++ HOT 1
- GSoC interest in Optimizing backend kernels in PyElastica HOT 1
- Examples not running due to modules not being found HOT 18
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyelastica.