renaming some variables, callback example.
This commit changes `continuum_flagella.py` file by renaming some
variables so that they will be consistent after merging with
`slithering_snake` PR. Also we add a callback example similar to the
`continuum_snake.py`, for the users.
@@ -5,6 +5,8 @@ import sys
sys.path.append("../")
+import os
+
from elastica.wrappers import (
BaseSystemCollection,
Connections,
@@ -16,7 +18,7 @@ from elastica.rod.cosserat_rod import CosseratRod
from elastica.boundary_conditions import FreeRod
from elastica.external_forces import GravityForces, MuscleTorques
from elastica.interaction import SlenderBodyTheory
-from elastica.callback_functions import ContinuumSnakeCallBack
+from elastica.callback_functions import CallBackBaseClass
from elastica.timestepper.symplectic_steppers import PositionVerlet, PEFRL
from elastica.timestepper import integrate
from ContinuumFlagellaCase.continuum_flagella_postprocessing import (
@@ -69,9 +71,14 @@ flagella_sim.append(shearable_rod)
flagella_sim.constrain(shearable_rod).using(FreeRod)
# Add muscle forces on the rod
+if os.path.exists("optimized_coefficients.txt"):
t_coeff_optimized = np.genfromtxt("optimized_coefficients.txt", delimiter=",")
+else:
+ t_coeff_optimized = np.array([17.4, 48.5, 5.4, 14.7])
period = 1.0
-wave_length = 0.3866575573648976 * base_length
+# TODO: wave_length is also part of optimization, when we integrate with CMA-ES
+# remove wave_length from here.
+wave_length = 0.3866575573648976 * base_length # wave number is 16.25
flagella_sim.add_forcing_to(shearable_rod).using(
MuscleTorques,
base_length=base_length,
@@ -79,9 +86,9 @@ flagella_sim.add_forcing_to(shearable_rod).using(
period=period,
wave_number=2.0 * np.pi / (wave_length),
phase_shift=0.0,
- rampupTime=period,
+ ramp_up_time=period,
direction=normal,
- WithSpline=True,
+ with_spline=True,
)
# Add slender body forces
@@ -96,9 +103,34 @@ flagella_sim.add_forcing_to(shearable_rod).using(
# Add call backs
+class ContinuumFlagellaCallBack(CallBackBaseClass):
+ """
+ Call back function for continuum snake
+ """
+
+ def __init__(self, step_skip: int, callback_params):
+ CallBackBaseClass.__init__(self)
+ self.every = step_skip
+ self.callback_params = callback_params
+
+ def make_callback(self, system, time, current_step: int):
+
+ if current_step % self.every == 0:
+
+ self.callback_params["time"].append(time)
+ self.callback_params["step"].append(current_step)
+ self.callback_params["position"].append(system.position_collection.copy())
+ self.callback_params["velocity"].append(system.velocity_collection.copy())
+ self.callback_params["avg_velocity"].append(
+ system.compute_velocity_center_of_mass()
+ )
+
+ return
+
+
pp_list = {"time": [], "step": [], "position": [], "velocity": [], "avg_velocity": []}
flagella_sim.callback_of(shearable_rod).using(
- ContinuumSnakeCallBack, step_skip=200, list=pp_list
+ ContinuumFlagellaCallBack, step_skip=200, callback_params=pp_list
)
flagella_sim.finalize()
