Skip to content

Commit cac0229

Browse files
committed
docs: clean up tutorial for gallery
1 parent 046df44 commit cac0229

File tree

5 files changed

+111
-93
lines changed

5 files changed

+111
-93
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,12 @@ pytestcache-remove:
112112
build-remove:
113113
rm -rf build/ dist/
114114

115+
.PHONY: doc-remove
116+
doc-remove:
117+
rm -rf docs/_build docs/gen_modules/ docs/sg_execution_times.rst docs/_gallery/
118+
115119
.PHONY: cleanup
116-
cleanup: pycache-remove dsstore-remove ipynbcheckpoints-remove pytestcache-remove mypycache-remove build-remove
120+
cleanup: pycache-remove dsstore-remove ipynbcheckpoints-remove pytestcache-remove mypycache-remove build-remove doc-remove
117121

118122
all: format-codestyle cleanup test
119123

examples/AxialStretchingCase/run_axial_stretching.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
Axial Stretching
33
================
44
5-
This case tests the axial stretching of a rod. A rod is fixed at one end and
6-
a force is applied at the other end. The rod stretches and the displacement
7-
of the tip is compared with the analytical solution.
5+
This case tests the axial stretching of a rod.
6+
The expected behavior is supposed to be like a spring-gravity motion, but
7+
with a rod. A rod is fixed at one end and a force is applied at the other
8+
end. The rod stretches and the displacement of the tip is compared with
9+
the analytical solution.
810
"""
911

1012
# isort:skip_file
@@ -15,8 +17,10 @@
1517
import elastica as ea
1618

1719
# %%
18-
# First, we define a simulator class that inherits from the necessary mixins.
19-
# This makes it easy to add constraints, forces, and damping to the system.
20+
# Simulation Setup
21+
# ----------------
22+
# We define a simulator class that inherits from the necessary mixins.
23+
# This makes constraints, forces, and damping evailable to the system.
2024

2125

2226
class StretchingBeamSimulator(
@@ -29,9 +33,13 @@ class StretchingBeamSimulator(
2933
final_time = 200.0
3034

3135
# %%
32-
# Next, we set up the test parameters for the simulation. This includes the
36+
# Rod Setup
37+
# ---------
38+
# Next, we set up the test parameters for the simulating rods. This includes the
3339
# number of elements, the start position, direction, normal, length, radius,
3440
# density, and Young's modulus of the rod.
41+
# For this case, we have fixed boundary condition at one end, and we apply external
42+
# force at the other end.
3543

3644
# setting up test params
3745
n_elem = 19
@@ -47,10 +55,6 @@ class StretchingBeamSimulator(
4755
poisson_ratio = 0.5
4856
shear_modulus = youngs_modulus / (poisson_ratio + 1.0)
4957

50-
# %%
51-
# Now we can create the `CosseratRod` object. We use the `straight_rod` method
52-
# to create a straight rod with the specified parameters.
53-
5458
stretchable_rod = ea.CosseratRod.straight_rod(
5559
n_elem,
5660
start,
@@ -65,18 +69,10 @@ class StretchingBeamSimulator(
6569

6670
stretch_sim.append(stretchable_rod)
6771

68-
# %%
69-
# We then apply a boundary condition to fix one end of the rod. We use the
70-
# `OneEndFixedBC` constraint to fix the position and director of the first node.
71-
7272
stretch_sim.constrain(stretchable_rod).using(
7373
ea.OneEndFixedBC, constrained_position_idx=(0,), constrained_director_idx=(0,)
7474
)
7575

76-
# %%
77-
# A force is applied to the other end of the rod. We use the `EndpointForces`
78-
# forcing to apply a force in the x-direction.
79-
8076
end_force_x = 1.0
8177
end_force = np.array([end_force_x, 0.0, 0.0])
8278
stretch_sim.add_forcing_to(stretchable_rod).using(
@@ -99,7 +95,9 @@ class StretchingBeamSimulator(
9995

10096

10197
# %%
102-
# We define a callback class to record the position and velocity of the rod
98+
# Callbacks
99+
# ---------
100+
# A callback object is passed to the simulator to record states of the rod
103101
# during the simulation. This is useful for post-processing the results.
104102

105103

@@ -137,16 +135,15 @@ def make_callback(
137135
)
138136

139137
# %%
138+
# Finalize and Run
139+
# ----------------
140140
# We finalize the simulator and create the time-stepper. The `PositionVerlet`
141141
# time-stepper is used to integrate the system.
142142

143143
stretch_sim.finalize()
144144
timestepper: ea.typing.StepperProtocol = ea.PositionVerlet()
145145
# timestepper = PEFRL()
146146

147-
# %%
148-
# The simulation is run for the specified `final_time`.
149-
150147
total_steps = int(final_time / dt)
151148
print("Total steps", total_steps)
152149
dt = final_time / total_steps
@@ -155,6 +152,8 @@ def make_callback(
155152
time = timestepper.step(stretch_sim, time, dt)
156153

157154
# %%
155+
# Post-Processing
156+
# ---------------
158157
# Finally, we plot the results and compare them with the analytical solution.
159158
# The analytical solution is calculated using the first-order theory with
160159
# both the base length and the modified length.

examples/ButterflyCase/run_butterfly.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
=========
44
55
This case simulates the motion of a rod that is initially shaped like a
6-
butterfly. The rod is released from rest and allowed to fall under gravity.
6+
butterfly. The rod is released from rest and allowed to deform freely.
7+
The goal of the simulation is for sanity check: how does the timestepper
8+
reliably preserve total energy of the system, when the system is simple Hamiltonian.
79
The simulation tracks the position and energy of the rod over time.
10+
11+
This example case also demonstrate how to setup rod with customized
12+
positions and directors.
813
"""
914

1015
import numpy as np
@@ -16,7 +21,9 @@
1621
from elastica.utils import MaxDimension
1722

1823
# %%
19-
# First, we define a simulator class that inherits from the necessary mixins.
24+
# Simulation Setup
25+
# ----------------
26+
# We define a simulator class that inherits from the necessary mixins.
2027

2128

2229
class ButterflySimulator(ea.BaseSystemCollection, ea.CallBacks):
@@ -27,9 +34,9 @@ class ButterflySimulator(ea.BaseSystemCollection, ea.CallBacks):
2734
final_time = 40.0
2835

2936
# %%
30-
# Next, we set up the test parameters for the simulation. This includes the
31-
# number of elements, the origin, the angle of inclination, the length,
32-
# radius, density, and Young's modulus of the rod.
37+
# Rod Setup
38+
# ---------
39+
# Next, we set up the test parameters for the simulation.
3340

3441
# setting up test params
3542
# FIXME : Doesn't work with elements > 10 (the inverse rotate kernel fails)
@@ -95,7 +102,9 @@ class ButterflySimulator(ea.BaseSystemCollection, ea.CallBacks):
95102

96103

97104
# %%
98-
# We define a callback class to record the position and energy of the rod
105+
# Callback Setup
106+
# --------------
107+
# A callback object is defined to record the position and energy of the rod
99108
# during the simulation.
100109

101110

@@ -127,7 +136,9 @@ def make_callback(
127136
return
128137

129138

139+
# database
130140
recorded_history: dict[str, list] = ea.defaultdict(list)
141+
131142
# initially record history
132143
recorded_history["time"].append(0.0)
133144
recorded_history["position"].append(butterfly_rod.position_collection.copy())
@@ -141,16 +152,15 @@ def make_callback(
141152
)
142153

143154
# %%
155+
# Finalize and Run
156+
# ----------------
144157
# We finalize the simulator and create the time-stepper.
145158

146159
butterfly_sim.finalize()
147160
timestepper: ea.typing.StepperProtocol
148161
timestepper = ea.PositionVerlet()
149162
# timestepper = PEFRL()
150163

151-
# %%
152-
# The simulation is run for the specified `final_time`.
153-
154164
dt = 0.01 * dl
155165
total_steps = int(final_time / dt)
156166
print("Total steps", total_steps)
@@ -160,8 +170,10 @@ def make_callback(
160170
time = timestepper.step(butterfly_sim, time, dt)
161171

162172
# %%
163-
# Finally, we plot the results. The position of the rod is plotted at
164-
# different time steps, and the energies are plotted as a function of time.
173+
# Post-Processing
174+
# ---------------
175+
# The position of the rod is plotted at different time steps,
176+
# and the energies are plotted as a function of time.
165177

166178
# Plot the histories
167179
fig = plt.figure(figsize=(5, 4), frameon=True, dpi=150)
@@ -180,6 +192,8 @@ def make_callback(
180192
# don't block
181193
fig.show()
182194

195+
# %%
196+
183197
# Plot the energies
184198
energy_fig = plt.figure(figsize=(5, 4), frameon=True, dpi=150)
185199
energy_ax = energy_fig.add_subplot(111)

examples/CatenaryCase/run_catenary.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
)
1919

2020
# %%
21-
# First, we define a simulator class that inherits from the necessary mixins.
21+
# Simulation Setup
22+
# ----------------
23+
# We define a simulator class that inherits from the necessary mixins.
2224

2325

2426
class CatenarySimulator(
@@ -28,24 +30,19 @@ class CatenarySimulator(
2830

2931

3032
catenary_sim = CatenarySimulator()
33+
final_time = 30
3134

3235
# %%
33-
# Next, we set up the simulation parameters. This includes the final time,
34-
# damping constant, time step, and rendering frame rate.
36+
# Rod Setup
37+
# ---------
38+
# We set up the rod parameters. This rod is affected by a gravity force.
3539

36-
final_time = 10
37-
damping_constant = 0.3
40+
n_elem = 500
3841
time_step = 1e-4
3942
total_steps = int(final_time / time_step)
4043
rendering_fps = 20
4144
step_skip = int(1.0 / (rendering_fps * time_step))
4245

43-
# %%
44-
# We then define the properties of the rod, such as the number of elements,
45-
# start position, direction, normal, length, radius, and material properties.
46-
47-
n_elem = 500
48-
4946
start = np.zeros((3,))
5047
direction = np.array([1.0, 0.0, 0.0])
5148
normal = np.array([0.0, 0.0, 1.0])
@@ -62,9 +59,6 @@ class CatenarySimulator(
6259
poisson_ratio = 0.5
6360
shear_modulus = E / (poisson_ratio + 1.0)
6461

65-
# %%
66-
# Now we can create the `CosseratRod` object and add it to the simulator.
67-
6862
base_rod = ea.CosseratRod.straight_rod(
6963
n_elem,
7064
start,
@@ -79,25 +73,25 @@ class CatenarySimulator(
7973

8074
catenary_sim.append(base_rod)
8175

76+
# Add gravity
77+
catenary_sim.add_forcing_to(base_rod).using(
78+
ea.GravityForces, acc_gravity=-9.80665 * normal
79+
)
80+
8281
# %%
8382
# Damping is added to the system to help it reach a steady state.
8483

8584
# add damping
85+
damping_constant = 0.3
8686
catenary_sim.dampen(base_rod).using(
8787
ea.AnalyticalLinearDamper,
8888
damping_constant=damping_constant,
8989
time_step=time_step,
9090
)
9191

9292
# %%
93-
# We add gravity to the system to simulate the weight of the rod.
94-
95-
# Add gravity
96-
catenary_sim.add_forcing_to(base_rod).using(
97-
ea.GravityForces, acc_gravity=-9.80665 * normal
98-
)
99-
100-
# %%
93+
# Boundary Conditions
94+
# -------------------
10195
# We fix both ends of the rod using the `FixedConstraint`.
10296

10397
# fix catenary ends
@@ -107,10 +101,10 @@ class CatenarySimulator(
107101
constrained_director_idx=(0, -1),
108102
)
109103

110-
111104
# %%
112-
# We define a callback class to record the position, radius, and internal
113-
# forces of the rod during the simulation.
105+
# Callback
106+
# --------
107+
# We define a callback class to record the rod state during the simulation.
114108

115109

116110
# Add call backs
@@ -145,14 +139,13 @@ def make_callback(
145139
)
146140

147141
# %%
148-
# We finalize the simulator and create the time-stepper.
142+
# Finalize and Run
143+
# ----------------
144+
# We finalize the simulator, create the time-stepper, and run.
149145

150146
catenary_sim.finalize()
151147
timestepper: ea.typing.StepperProtocol = ea.PositionVerlet()
152148

153-
# %%
154-
# The simulation is run for the specified number of steps.
155-
156149
dt = final_time / total_steps
157150
time = 0.0
158151
for i in range(total_steps):
@@ -161,6 +154,8 @@ def make_callback(
161154
b = np.min(position[-1][2])
162155

163156
# %%
157+
# Post-processing
158+
# ---------------
164159
# Finally, we can save a video of the simulation and plot the final
165160
# shape of the catenary.
166161

0 commit comments

Comments
 (0)