-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_environment.py
2738 lines (2491 loc) · 120 KB
/
set_environment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
__all__ = ["Environment"]
from elastica._calculus import _isnan_check
from elastica.timestepper import extend_stepper_interface
from elastica import *
from elastica._elastica_numba._rod._muscular_rod import MuscularRod
from elastica._linalg import _batch_norm
from scipy.optimize import minimize_scalar, fsolve
from Cases.post_processing import (
plot_video_with_surface,
plot_video_activation_muscle,
)
# import numba
# from numba import njit
import os
from elastica._rotations import _get_rotation_matrix
# from elastica._calculus import difference_kernel
#
#
#
# from OctopusArmConnections.OctopusArmWithNewPressureModel.connect_straight_rods import (
# get_connection_vector_straight_straight_rod,
# SurfaceJointSideBySide,
# )
#
# from OctopusArmConnections.OctopusArmWithNewPressureModel.connect_ring_straight_rods import (
# OrthogonalRodsSideBySideJoint,
# get_connection_order_and_angle,
# )
# from OctopusArmConnections.OctopusArmWithNewPressureModel.connect_ring_ring_rods import (
# get_ring_ring_connection_reference_index,
# InnerRingRingRodConnectionDifferentLevel,
# OuterRingRingRodConnectionDifferentLevel,
# )
#
# from OctopusArmConnections.OctopusArmWithNewPressureModel.memory_block_connections import (
# MemoryBlockConnections,
# )
#
# from OctopusArmConnections.OctopusArmWithNewPressureModel.pressure_force import (
# PressureForce,
# )
from itertools import groupby
# from OctopusArmConnections.OctopusArmWithNewPressureModel.connect_ring_straight_rods_contact import OrthogonalRodsSideBySideContact
# from OctopusArmConnections.OctopusArmWithNewPressureModel.connect_ring_helical_rods_extra_contact import get_connection_order_and_angle_btw_helical_and_ring_rods, RingHelicalRodJoint, RingHelicalRodContact
from Cases.arm_function import (
no_offset_radius_for_straight_rods,
outer_ring_rod_functions,
helical_rod_radius_function,
)
from Cases.arm_function import ConstrainRingPositionDirectors
from Cases.arm_function import (
SigmoidActivationLongitudinalMuscles,
ActivationRampUpRampDown,
SigmoidActivationTransverseMuscles,
LocalActivation,
)
from Cases.arm_function import DragForceOnStraightRods
from Cases.arm_function import (
StraightRodCallBack,
RingRodCallBack,
RigidCylinderCallBack,
)
from Cases.arm_function import (
DampingFilterBC,
ExponentialDampingBC,
DampingFilterBCRingRod,
)
from Connections import *
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)
# Set base simulator class
class BaseSimulator(
BaseSystemCollection, Constraints, MemoryBlockConnections, Forcing, CallBacks
):
pass
class Environment:
def __init__(
self,
final_time,
k_straight_straight_connection_spring_scale,
k_straight_straight_connection_contact_scale,
k_ring_ring_spring_connection_scale,
k_ring_straight_spring_connection_scale,
k_ring_straight_spring_torque_connection_scale,
k_ring_straight_contact_connection_scale,
k_ring_helical_spring_connection_scale,
k_ring_helical_contact_connection_scale,
COLLECT_DATA_FOR_POSTPROCESSING=False,
):
# Integrator type
self.StatefulStepper = PositionVerlet()
# Simulation parameters
self.final_time = final_time
time_step = 1e-5 # 1.25e-6 * 2 * 2 #* 2 # * 2 * 2 #* 2# (
# 2.0e-5 / 2 / 2 / 4
# ) # / 20 /4 / 2 * 8 * 2 * 5 #/200 # this is a stable timestep
self.learning_step = 1
self.total_steps = int(self.final_time / time_step / self.learning_step) + 1
# self.time_step = np.float64(
# float(self.final_time) / (self.total_steps * self.learning_step)
# )
self.time_step = time_step
# self.time_step = 1.000002000004e-05
# Video speed
self.rendering_fps = 30 # 20 * 1e1
self.step_skip = int(1.0 / (self.rendering_fps * self.time_step))
# Set the connection scales
self.k_straight_straight_connection_spring_scale = (
k_straight_straight_connection_spring_scale
)
self.k_straight_straight_connection_contact_scale = (
k_straight_straight_connection_contact_scale
)
self.k_ring_ring_spring_connection_scale = k_ring_ring_spring_connection_scale
self.k_ring_straight_spring_connection_scale = (
k_ring_straight_spring_connection_scale
)
self.k_ring_straight_spring_torque_connection_scale = (
k_ring_straight_spring_torque_connection_scale
)
self.k_ring_straight_contact_connection_scale = (
k_ring_straight_contact_connection_scale
)
self.k_ring_helical_spring_connection_scale = (
k_ring_helical_spring_connection_scale
)
self.k_ring_helical_contact_connection_scale = (
k_ring_helical_contact_connection_scale
)
# Collect data is a boolean. If it is true callback function collects
# rod parameters defined by user in a list.
self.COLLECT_DATA_FOR_POSTPROCESSING = COLLECT_DATA_FOR_POSTPROCESSING
def save_state(self, 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
----------
directory : str
Directory path name. The path must exist.
"""
os.makedirs(directory, exist_ok=True)
for idx, rod in enumerate(self.rod_list):
path = os.path.join(directory, "rod_{}.npz".format(idx))
np.savez(path, time=time, **rod.__dict__)
if verbose:
print("Save complete: {}".format(directory))
def load_state(self, directory: str = "", verbose: bool = False):
"""
Load the rod-state.
Compatibale with 'save_state' method.
If the save-file does not exist, it returns error.
Parameters
----------
directory : str
Directory path name.
"""
time_list = [] # Simulation time of rods when they are saved.
for idx, rod in enumerate(self.rod_list):
path = os.path.join(directory, "rod_{}.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 value.shape != ():
# Copy data into placeholders
getattr(rod, key)[:] = value
else:
# For single-value data
setattr(rod, key, value)
if all_equal(time_list) == False:
raise ValueError(
"Restart time of loaded rods are different, check your inputs!"
)
# Apply boundary conditions. Ring rods have periodic BC, so we need to update periodic elements in memory block
self.simulator.constrain_values(0.0)
self.simulator.constrain_rates(0.0)
if verbose:
print("Load complete: {}".format(directory))
return time_list[0]
def reset(self):
""""""
self.simulator = BaseSimulator()
# setting up test params
n_elem = 180 # * 2 * 2 # * 2 # 33
n_elem_ring_rod = 40
number_of_straight_rods = 9 # 13#25#7#13 # 25#25#13#25 # 49#9#13#25#9
self.n_connections = number_of_straight_rods - 1
start = np.zeros((3,))
direction = np.array([0.0, 1.0, 0.0]) # rod direction
normal = np.array([0.0, 0.0, 1.0])
binormal = np.cross(direction, normal)
arm_length = 200 # / 2 / 2 # 53.143*4 # mm
mass_club = 1.8 # g
mass_stalk = mass_club / 0.75
# mass_total = mass_stalk + mass_club #mass_club * (1 + 1 / 0.75)
outer_base_radius = 12 # 12#16 # 12 # 2*3.7 # mm
density = 1.050e-3 # *5*2*2 # # g/mm3
nu = 250 # * 5 * 2 * 10 # * 5 # * 2 # dissipation coefficient
E = 1e4 # Young's Modulus Pa # Tramacere et. al. 2013 Interface, octopus dorsal tissue elastic modulus
E *= 1 # convert Pa (kg m.s-2 / m2) to g.mm.s-2 / mm2 This is required for units to be consistent
poisson_ratio = 0.5
taper_ratio = 12 # Kier & Stella 2007
taper_ratio_axial_cord = taper_ratio # 8.42
outer_tip_radius = outer_base_radius / taper_ratio
# Tapered arm is like a cone. Subtracting bigger cone from smaller cone will give us the arm volume.
volume_arm = 1 / 3 * (
((taper_ratio / (taper_ratio - 1)) * arm_length)
* np.pi
* outer_base_radius**2
) - 1 / 3 * (
(1 / (taper_ratio - 1) * arm_length) * np.pi * outer_tip_radius**2
)
mass_total = density * volume_arm
# taper_ratio = 1
# taper_ratio_axial_cord = taper_ratio # 8.42
# Kier & Stella 2007 also Hannsy et. al. 2015
eta_ring_rods = (
0.17 # *5/2/2.5#0.10*4.5 # percentage area of the transverse muscles
)
eta_straight_rods = 0.56 # 0.52
eta_helical_rods = 0.21 # 0.35#0.21
eta_axial_cord = 0.06 # 0.17#0.06 # axial nerve cord
# Compute center rod and other straight rod radius
# center_straight_rod_radius = np.sqrt(outer_base_radius ** 2 * eta_axial_cord)
# outer_straight_rod_radius = np.sqrt(
# outer_base_radius ** 2 * eta_straight_rods / (number_of_straight_rods - 1)
# )
center_straight_rod_radius = np.sqrt(
outer_base_radius**2
* (eta_axial_cord + eta_straight_rods)
/ number_of_straight_rods
)
outer_straight_rod_radius = center_straight_rod_radius
variables = fsolve(
no_offset_radius_for_straight_rods,
x0=(center_straight_rod_radius, outer_straight_rod_radius),
args=(
number_of_straight_rods - 1,
np.pi * outer_base_radius**2 * (eta_axial_cord + eta_straight_rods),
),
)
center_straight_rod_radius, outer_straight_rod_radius = variables
area_ring_rods = np.pi * outer_base_radius**2 * eta_ring_rods
# outer_ring_rod_radius = minimize_scalar(
# outer_ring_rod_functions,
# args=(
# n_elem_ring_rod,
# center_straight_rod_radius,
# outer_straight_rod_radius,
# area_ring_rods,
# ),
# ).x
outer_ring_rod_radius = arm_length / n_elem / 2
# outer_ring_rod_radius = 0.5 * (outer_base_radius - center_straight_rod_radius - 2*outer_straight_rod_radius)
# Compute radius of helical rod. We will place helical rods at the most
# outer layer.
area_helical = (np.pi * outer_base_radius**2) * eta_helical_rods
helical_rod_radius = minimize_scalar(
helical_rod_radius_function,
args=(
center_straight_rod_radius,
outer_straight_rod_radius,
outer_ring_rod_radius,
area_helical,
),
).x
# FIXME: for stability we doubled the helical rod radius, but also with this change helical rod radius density
# is closer to the octopus arm density.
helical_rod_radius *= 1.829478746907853 # 2#1.7197#2#5
center_rod_radius_along_arm = center_straight_rod_radius * np.linspace(
1, 1 / taper_ratio_axial_cord, n_elem
)
radius_ratio_factor_from_base_to_tip = np.linspace(1, 1 / taper_ratio, n_elem)
outer_straight_rod_radius_along_arm = (
outer_straight_rod_radius * radius_ratio_factor_from_base_to_tip
)
outer_ring_rod_radius_along_arm = outer_ring_rod_radius * np.ones(
(n_elem)
) # * radius_ratio_factor_from_base_to_tip
helical_rod_radius_along_arm = (
helical_rod_radius * radius_ratio_factor_from_base_to_tip
)
# Compute the bank angle of the straight rods or longitudinal muscles. These rods are the ones except the one
# at the center and they are not only tapered but banked.
ratio = arm_length / (
(center_rod_radius_along_arm[0] + outer_straight_rod_radius_along_arm[0])
- (
center_rod_radius_along_arm[-1]
+ outer_straight_rod_radius_along_arm[-1]
)
)
bank_angle = np.arctan(ratio)
# Van Leeuwen and Kier 1997 model
# Compute the sacromere, myosin, maximum active stress
l_bz = 0.14 # length of the bare zone (micro meter)
# l_sacro_ref = 2.37 # reference sacromere length (micro meter)
# Sarcomere lengths are changed to fit the Van Leeuwen model to the tentacle transver muscle active force
# measurements given in Kier and Curtin 2002,
l_sacro_base = 1.1 # 1.3399 # sacromere length at the base (micro meter)
l_sacro_tip = 1.1 # 1.3399#0.7276 # sacromere length at the tip (micro meter)
sarcomere_rest_lengths = np.linspace(
l_sacro_base, l_sacro_tip, n_elem
) # np.ones((n_elem))*l_sacro_base#np.linspace(l_sacro_base, l_sacro_tip, n_elem)
l_myo_ref = 1.58 # reference myosin length (micro meter)
# Myosin lengths are measured in Kier and Curtin 2002.
l_myo_base = 0.81 # 0.9707 # 7.41#6.5#0.9707 # myosin length at the base of the tentacle (micro meter)
l_myo_tip = 0.81 # 0.9707#0.4997 # myosin length at the tip of the tentacle (micro meter)
myosin_lengths = np.linspace(
l_myo_base, l_myo_tip, n_elem
) # np.ones((n_elem))*l_myo_base#np.linspace(l_myo_base, l_myo_tip, n_elem)
# Active maximum stress measured in Kier and Curtin 2002 is 130kPa which can be also computed
# using the VanLeeuwen model for a given myosin length (0.81 micro m).
maximum_active_stress_ref = 280e3 # maximum active stress reference value (Pa)
maximum_active_stress = (
maximum_active_stress_ref * (myosin_lengths - l_bz) / (l_myo_ref - l_bz)
)
# minimum_strain_rate_ref = -17 # -17 # 1/s
# minimum_strain_rate = minimum_strain_rate_ref * (
# l_sacro_ref / sarcomere_rest_lengths
# )
# minimum_strain_rate = -1.8 * np.ones(
# (n_elem)
# ) # 1/s # Kier and Curtin 2002 Squid Arm
minimum_strain_rate_longitudinal = (
-1.8
) # -0.913 # 1/s # Zullo 2022 for octopus longitudinal muscle
minimum_strain_rate_transverse = (
-1.8
) # -0.3560 # 1/s # Zullo 2022 for octopus longitudinal muscle
# Force velocity constant is the G term in Hills equation. In Zullo 2022 paper it is not given but we found
# the best fit for G term as 0.80. In our modeling approach we are using K=1/G to be consitend with the
# paper of VanLeewuen and Kier . Since they provide force-velocity relation for both extending and contracting
# muscles.
force_velocity_constant = 0.25 # 1/0.80
# (
# normalized_active_force_slope_transverse_muscles,
# normalized_active_force_y_intercept_transverse_muscles,
# normalized_active_force_break_points_transverse_muscles,
# ) = get_active_force_piecewise_linear_function_parameters_for_VanLeeuwen_muscle_model(
# sarcomere_rest_lengths, myosin_lengths
# )
# # Load longitudinal muscle data for squid tentacle. Kier provided the measurments.
# longitudinal_muscle_active_force_experimental_data = np.load(
# "kier_longitudinal_active_force_measurement.npz"
# )
# normalized_active_force_slope_longitudinal_muscles = (
# longitudinal_muscle_active_force_experimental_data["slope"]
# )
# normalized_active_force_y_intercept_longitudinal_muscles = (
# longitudinal_muscle_active_force_experimental_data["y_intercept"]
# )
# normalized_active_force_break_points_longitudinal_muscles = (
# longitudinal_muscle_active_force_experimental_data["break_points"]
# )
#
# # Load passive force data. For transverse muscles we used the VanLeeuwen model. For longitudinal muscles
# # measurments done by Udit. For passive stress Elastica implementation uses 3rd degree polynomial and
# # requires 4 coefficients. Order of coefficients starts from highest order (cube) to lowest order of poly.
# transverse_muscle_passive_force_coefficients = get_passive_force_cubic_function_coefficients_for_VanLeeuwen_muscle_model(
# strain=np.linspace(0, 0.8, 200)
# )
# # For the longitudinal muscle passive force we are using the coefficients measured by Udit Halder. These
# # measurments are done by taking real octopus arm and performing stretch tests on the actual arm, under
# # different loads. Since the fit provided is 2nd order other cofficients of cubic polynomial are zero.
# longitudinal_muscle_passive_force_coefficients = np.array([0,112.8492e3,8.8245e3 , 0 ])
#
# For both longitudinal muscle active and passive force length curves we fit a polynomial. Lets read
# the coefficients
longitudinal_muscle_coefficient = np.load(
"../octopus_longitudinal_muscles_fit.npz"
)
# Longitudinal muscle active coefficients are computed by fitting 4th order polynomial to the data given in
# Zullo 2022.
longitudinal_muscle_active_force_coefficients = longitudinal_muscle_coefficient[
"longitudinal_active_part_coefficients_with_shift"
]
# Longitudinal muscle passive coefficients are computed by fitting 2nd order polynomial to the data given in
# Zullo 2022.
longitudinal_muscle_passive_force_coefficients = (
longitudinal_muscle_coefficient[
"longitudinal_passive_part_coefficients_with_shift"
]
)
# For both transverse muscle active and passive force length curves we fit a polynomial. Lets read the
# coefficients.
transverse_muscle_coefficient = np.load("../octopus_transverse_muscles_fit.npz")
# Transverse muscle active coefficients are computed by fitting 4th order polynomial to the data given in
# Zullo 2022.
transverse_muscle_active_force_coefficients = transverse_muscle_coefficient[
"transverse_active_part_coefficients_with_shift"
]
# Transverse muscle passive coefficients are computed by fitting 2nd order polynomial to the data given in
# Zullo 2022.
transverse_muscle_passive_force_coefficients = transverse_muscle_coefficient[
"transverse_passive_part_coefficients_with_shift"
]
direction_ring_rod = normal
normal_ring_rod = direction
self.rod_list = []
self.straight_rod_list = []
self.ring_straight_rod_connection_index_list = []
# ring straight rod connection list is containing the list of directions for possible connections.
# Here the idea is to connect nodes in specific position, and make sure there is symmetry in whole arm.
self.ring_straight_rod_connection_direction_list = []
# First straight rod is at the center, remaining ring rods are around the first ring rod.
angle_btw_straight_rods = (
0
if number_of_straight_rods == 1
else 2 * np.pi / (number_of_straight_rods - 1)
)
self.angle_wrt_center_rod = []
self.bank_angle_of_straight_rods_list = []
# First straight rod at the center
start_rod_1 = start
self.ring_straight_rod_connection_direction_list.append([])
for i in range(int((number_of_straight_rods - 1))):
# Compute the direction from center (origin) of the rod towards the other rods straight rods.
rotation_matrix = _get_rotation_matrix(
angle_btw_straight_rods * i, direction.reshape(3, 1)
).reshape(3, 3)
direction_from_center_to_rod = rotation_matrix @ binormal
self.ring_straight_rod_connection_direction_list[0].append(
direction_from_center_to_rod
)
# Center rod has 0 bank angle and 0 angle wrt center rod. THis is center rod.
self.angle_wrt_center_rod.append(0)
self.bank_angle_of_straight_rods_list.append(np.pi / 2)
base_radius_varying_center = (
center_rod_radius_along_arm # center_rod_radius * np.ones((n_elem))
)
axial_cord_length = np.ones((n_elem)) * arm_length / n_elem
volume_axial_cord = axial_cord_length * (
np.pi * center_rod_radius_along_arm**2
)
density_axial_cord = (
density # mass_total * eta_axial_cord / volume_axial_cord.sum()
)
# Arrange sizes of normalized active force piecewise function parameters, before initializing straight rods.
# normalized_active_force_slope_straight_rods = np.ones(
# (n_elem)
# ) * normalized_active_force_slope_longitudinal_muscles.reshape(4, 1)
# normalized_active_force_y_intercept_straight_rods = np.ones(
# (n_elem)
# ) * normalized_active_force_y_intercept_longitudinal_muscles.reshape((4, 1))
# normalized_active_force_break_points_straight_rods = np.ones(
# (n_elem)
# ) * normalized_active_force_break_points_longitudinal_muscles.reshape((4, 1))
muscle_active_force_coefficients_straight_rods = np.ones(
(n_elem)
) * longitudinal_muscle_active_force_coefficients.reshape(9, 1)
straight_rods_force_velocity_constant = (
np.ones((n_elem)) * force_velocity_constant
)
maximum_active_stress_straight_rods = np.ones((n_elem)) * maximum_active_stress
minimum_strain_rate_straight_rods = (
np.ones((n_elem)) * minimum_strain_rate_longitudinal
)
passive_force_coefficients_straight_rods = (
longitudinal_muscle_passive_force_coefficients.reshape(9, 1)
* np.ones((n_elem))
)
nu_rod_1 = (
density_axial_cord
* np.pi
* center_rod_radius_along_arm**2
* nu
/ 8
/ 3
# * 2
# * 2
* 15
/ 4
/ 2
/ 15
# * 2 # *5 * 5#* 10# * 2
) * 0
self.shearable_rod1 = MuscularRod.straight_rod(
n_elem,
start_rod_1,
direction,
normal,
arm_length,
base_radius=center_rod_radius_along_arm,
density=density_axial_cord,
nu=nu_rod_1,
youngs_modulus=E,
poisson_ratio=poisson_ratio,
maximum_active_stress=maximum_active_stress_straight_rods,
minimum_strain_rate=minimum_strain_rate_straight_rods,
force_velocity_constant=straight_rods_force_velocity_constant,
# normalized_active_force_break_points=normalized_active_force_break_points_straight_rods,
# normalized_active_force_y_intercept=normalized_active_force_y_intercept_straight_rods,
# normalized_active_force_slope=normalized_active_force_slope_straight_rods,
E_compression=2.5e4,
compression_strain_limit=-0.025,
active_force_coefficients=muscle_active_force_coefficients_straight_rods,
# tension_passive_force_scale=1/30,#/4,
# Active force starts to decrease around strain 0.55 so we shift passive force to strain of 0.55
# This is also seen in Leech longitudinal muscles (Gerry & Ellebry 2011)
extension_strain_limit=0.025,
passive_force_coefficients=passive_force_coefficients_straight_rods,
)
# self.shearable_rod1.bend_matrix[2,2,:]/=4
self.straight_rod_list.append(self.shearable_rod1)
rod_height = np.hstack((0.0, np.cumsum(self.shearable_rod1.rest_lengths)))
rod_non_dimensional_position = rod_height / np.sin(bank_angle)
for i in range(number_of_straight_rods - 1):
rotation_matrix = _get_rotation_matrix(
angle_btw_straight_rods * i, direction.reshape(3, 1)
).reshape(3, 3)
direction_from_center_to_rod = rotation_matrix @ binormal
self.angle_wrt_center_rod.append(angle_btw_straight_rods * i)
self.ring_straight_rod_connection_direction_list.append(
[direction_from_center_to_rod, -direction_from_center_to_rod]
)
# Compute the rotation matrix, for getting the correct banked angle.
normal_banked_rod = rotation_matrix @ normal
# Rotate direction vector around new normal to get the new direction vector.
# Note that we are doing ccw rotation and direction should be towards the center.
rotation_matrix_banked_rod = _get_rotation_matrix(
-(np.pi / 2 - bank_angle), normal_banked_rod.reshape(3, 1)
).reshape(3, 3)
direction_banked_rod = rotation_matrix_banked_rod @ direction
start_rod = start + (direction_from_center_to_rod) * (
# center rod # this rod
+base_radius_varying_center[0]
+ outer_straight_rod_radius_along_arm[0]
)
position_collection = np.zeros((3, n_elem + 1))
position_collection[:] = np.einsum(
"i,j->ij", direction_banked_rod, rod_non_dimensional_position
) + start_rod.reshape(3, 1)
self.bank_angle_of_straight_rods_list.append(bank_angle)
# Now lets recompute the straight rod radius, since it is banked, elements of center and outer straight rod
# is not touching perfectly. Lets adjust straight_rod_radius so that center and outer ones touch.
element_position_center_rod = 0.5 * (
self.shearable_rod1.position_collection[:, 1:]
+ self.shearable_rod1.position_collection[:, :-1]
)
element_position_straight_rod = 0.5 * (
position_collection[:, 1:] + position_collection[:, :-1]
)
straight_rod_radius = _batch_norm(
element_position_center_rod - element_position_straight_rod
) - (self.shearable_rod1.radius)
longitudinal_muscle_length = np.ones((n_elem)) * arm_length / n_elem
volume_longitudinal_muscle = longitudinal_muscle_length * (
np.pi * straight_rod_radius**2
)
# Adjust density such that its mass is consistent with percentage area of this muscle group
density_longitudinal_muscle = (
mass_total
* eta_straight_rods
/ (number_of_straight_rods - 1)
/ volume_longitudinal_muscle.sum()
)
# density_longitudinal_muscle = density#density_axial_cord
# nu_rod = (
# density_longitudinal_muscle
# * np.pi
# * outer_straight_rod_radius_along_arm ** 2
# * nu
# / 8
# / 3
# )
nu_rod = nu_rod_1 # /10
self.straight_rod_list.append(
MuscularRod.straight_rod(
n_elem,
start_rod,
direction_banked_rod,
normal_banked_rod,
arm_length,
base_radius=straight_rod_radius,
density=density_longitudinal_muscle,
nu=nu_rod,
youngs_modulus=E,
poisson_ratio=poisson_ratio,
position=position_collection,
maximum_active_stress=maximum_active_stress_straight_rods,
minimum_strain_rate=minimum_strain_rate_straight_rods,
force_velocity_constant=straight_rods_force_velocity_constant,
# normalized_active_force_break_points=normalized_active_force_break_points_straight_rods,
# normalized_active_force_y_intercept=normalized_active_force_y_intercept_straight_rods,
# normalized_active_force_slope=normalized_active_force_slope_straight_rods,
E_compression=2.5e4,
compression_strain_limit=-0.025,
active_force_coefficients=muscle_active_force_coefficients_straight_rods,
# tension_passive_force_scale=1/30,#/4,
# Extension shift is to make sure at rest configuration there is no passive tension stress.
extension_strain_limit=0.025,
passive_force_coefficients=passive_force_coefficients_straight_rods,
)
)
# Ring rods
# n_elem_ring_rod = (
# 12#number_of_straight_rods-1 # 48#12#24 # chose multiplies of 8
# )
total_number_of_ring_rods = n_elem # 33
self.ring_rod_list_outer = []
straight_rod_element_position = 0.5 * (
self.shearable_rod1.position_collection[..., 1:]
+ self.shearable_rod1.position_collection[..., :-1]
)
straight_rod_position = np.einsum(
"ij, i->j", straight_rod_element_position, direction
)
ring_rod_position = straight_rod_position
center_position_ring_rod = np.zeros((3, total_number_of_ring_rods))
connection_idx_straight_rod = np.zeros(
(total_number_of_ring_rods), dtype=np.int
)
radius_outer_ring_rod = np.zeros((total_number_of_ring_rods))
base_length_ring_rod_outer = np.zeros((total_number_of_ring_rods))
volume_outer_ring_rod = np.zeros((total_number_of_ring_rods))
for i in range(total_number_of_ring_rods):
center_position_ring_rod[..., i] = start + direction * ring_rod_position[i]
# center_position_ring_rod[..., i] = start + direction * ring_rod_position[1]
# Position of ring rods are defined as percentage of the center rod length. In order to place
# ring rod node positions and center rod node positions we have to do following calculation.
# Placing ring rod and center rod positions on the same plane simplifies the connection routines.
center_position_ring_rod_along_direction = np.dot(
center_position_ring_rod[..., i], direction
)
connection_idx_straight_rod[i] = np.argmin(
np.abs(straight_rod_position - center_position_ring_rod_along_direction)
)
center_position_ring_rod[..., i] = straight_rod_element_position[
..., connection_idx_straight_rod[i]
]
radius_outer_ring_rod[i] = outer_ring_rod_radius_along_arm[
connection_idx_straight_rod[i]
]
base_length_ring_rod_outer[i] = (
2
* np.pi
* (
self.shearable_rod1.radius[connection_idx_straight_rod[i]]
+ 2
* self.straight_rod_list[1].radius[connection_idx_straight_rod[i]]
+ radius_outer_ring_rod[i]
)
)
volume_outer_ring_rod[i] = base_length_ring_rod_outer[i] * (
np.pi * radius_outer_ring_rod[i] ** 2
)
# Adjust density such that its mass is consistent with percentage area of this muscle group
density_ring_rod = mass_total * eta_ring_rods / volume_outer_ring_rod.sum()
# density_ring_rod = density
for i in range(total_number_of_ring_rods):
nu_ring_rod_outer = (
density_ring_rod
* np.pi
* radius_outer_ring_rod[i] ** 2
* nu
/ 4
* 2
* 2
* 2
* 2
* 2
/ 8
/ 3
# * 3
/ 4
/ 2
* 2
# * 2
# * 5 #*1.5
)
# myosin_lengths_ring = (
# np.ones((n_elem_ring_rod))
# * myosin_lengths[connection_idx_straight_rod[i]]
# )
# sarcomere_rest_lengths_ring = (
# np.ones((n_elem_ring_rod))
# * sarcomere_rest_lengths[connection_idx_straight_rod[i]]
# )
# maximum_active_stress_ring = (
# np.ones((n_elem_ring_rod))
# * maximum_active_stress[connection_idx_straight_rod[i]]
# )
# minimum_strain_rate_ring = (
# np.ones((n_elem_ring_rod))
# * minimum_strain_rate[connection_idx_straight_rod[i]]
# )
# myosin_lengths_ring = np.ones((n_elem_ring_rod)) * myosin_lengths[0]
# sarcomere_rest_lengths_ring = (
# np.ones((n_elem_ring_rod)) * sarcomere_rest_lengths[0]
# )
maximum_active_stress_ring = (
np.ones((n_elem_ring_rod)) * maximum_active_stress[0]
)
minimum_strain_rate_ring = (
np.ones((n_elem_ring_rod)) * minimum_strain_rate_transverse
)
force_velocity_constant_ring = (
np.ones((n_elem_ring_rod)) * force_velocity_constant
)
# normalized_active_force_break_points_ring = np.ones(
# (n_elem_ring_rod)
# ) * normalized_active_force_break_points_transverse_muscles[
# :, connection_idx_straight_rod[i]
# ].reshape(
# 4, 1
# )
# normalized_active_force_y_intercept_ring = np.ones(
# (n_elem_ring_rod)
# ) * normalized_active_force_y_intercept_transverse_muscles[
# :, connection_idx_straight_rod[i]
# ].reshape(
# 4, 1
# )
# normalized_active_force_slope_ring = np.ones(
# (n_elem_ring_rod)
# ) * normalized_active_force_slope_transverse_muscles[
# :, connection_idx_straight_rod[i]
# ].reshape(
# 4, 1
# )
muscle_active_force_coefficients_ring_rods = np.ones(
(n_elem_ring_rod)
) * transverse_muscle_active_force_coefficients.reshape(9, 1)
passive_force_coefficients_ring_rods = (
transverse_muscle_passive_force_coefficients.reshape(9, 1)
* np.ones((n_elem_ring_rod))
)
self.ring_rod_list_outer.append(
MuscularRod.ring_rod(
n_elem_ring_rod,
center_position_ring_rod[..., i],
direction_ring_rod,
-normal_ring_rod,
base_length_ring_rod_outer[i],
radius_outer_ring_rod[i],
density=density_ring_rod,
nu=nu_ring_rod_outer,
youngs_modulus=E, # *2,#*4*4, # *4*(2.5)**2,
poisson_ratio=poisson_ratio,
maximum_active_stress=maximum_active_stress_ring, # *4*4,
minimum_strain_rate=minimum_strain_rate_ring,
force_velocity_constant=force_velocity_constant_ring,
E_compression=1e4, # *4*4,
# tension_passive_force_scale = 1 ,
# normalized_active_force_break_points=normalized_active_force_break_points_ring,
# normalized_active_force_y_intercept=normalized_active_force_y_intercept_ring,
# normalized_active_force_slope=normalized_active_force_slope_ring,
compression_strain_limit=-0.025,
active_force_coefficients=muscle_active_force_coefficients_ring_rods,
# Extension shift is to make sure at rest configuration there is no passive tension stress.
extension_strain_limit=0.025,
passive_force_coefficients=passive_force_coefficients_ring_rods,
)
)
# #self.ring_rod_list_outer[i].shear_matrix[0,0,:] *= 5*2#0
# #self.ring_rod_list_outer[i].shear_matrix[1,1,:] *= 5*2#0
self.ring_rod_list = self.ring_rod_list_outer
# Create helix rods or oblique muscles
self.helical_rod_list = []
direction_helical_rod = direction
normal_helical_rod = normal
binormal_helical_rod = np.cross(direction_helical_rod, normal_helical_rod)
total_number_of_helical_rods = 8
# Sum of center rod radius and ring rod diameter and helical rod radius
helix_radius_base = (
self.shearable_rod1.radius[0]
+ 2 * self.straight_rod_list[1].radius[0]
+ 2 * self.ring_rod_list_outer[0].radius[0]
+ helical_rod_radius_along_arm[0]
)
distance_btw_two_ring_rods = ring_rod_position[1] - ring_rod_position[0]
# Helix should cover all ring rods. First term below gives the distance from first ring rod upto last ring rod
# but we want to connect last ring rod and helical rod as well. Thus we add second term.
helix_length_covered = (
ring_rod_position[-1] - ring_rod_position[0]
) + distance_btw_two_ring_rods / 2
# Target helix angle is 72 degrees. If we set number of ring rods covered by one helix turn to 6, helix angle
# becomes 71.0072 degrees, which is the closest to the target angle.
distance_in_one_turn = (20 + 5e-15) * distance_btw_two_ring_rods # 6
self.helix_angle = np.rad2deg(
np.arctan(2 * np.pi * helix_radius_base / distance_in_one_turn)
)
n_helix_turns = helix_length_covered / distance_in_one_turn
pitch_factor = distance_in_one_turn / (2 * np.pi)
# Number of helix turns
n_elem_per_turn = 40 # This should be divided by the 6 at least, # of ring rod that is passed in one turn
n_elem_helical_rod = int(n_helix_turns * n_elem_per_turn) + 1
# Compute the curve angle of helix for each node. Using curve angle later on we will compute the position of
# nodes.
curve_angle = np.linspace(
0.0, 2 * np.pi * n_helix_turns, n_elem_helical_rod + 1
)
# Start helical rod at the same location as the ring rod at the base.
start_position_of_helix = (
np.zeros((3,)) + direction_helical_rod * ring_rod_position[0]
)
# Helix radius is radius of one helix turn, you can make this radius varying and let it decrease as
# it goes along the global direction. For example for tapered arm helix radius decreases along the arm.
# First compute the node positions of the helical rod in `direction` direction.
position_of_helical_rod_in_global_dir = np.einsum(
"ij, i->j",
pitch_factor * np.einsum("i,j->ij", direction_helical_rod, curve_angle),
direction,
)
# We need to find the positions of nodes where ring rod and helical rod are connected. These points are the
# interpolation points for the spline for computing the helix radius, which is used to compute node positions.
# first dimension is height, second is radius of helix
helix_radius_points_for_interp = np.zeros((2, 2 * total_number_of_ring_rods))
helical_rod_radius_points_for_interp = np.zeros((2, total_number_of_ring_rods))
for idx, rod in enumerate(self.ring_rod_list_outer):
# Find the center position of ring rod.
center_position_ring_rod = np.mean(rod.position_collection, axis=1)
center_position_ring_rod_along_direction = np.dot(
center_position_ring_rod - start_position_of_helix, direction
)
# Find the node idx where helical rod node and ring rod are at the same height.
index = np.argmin(
np.abs(
position_of_helical_rod_in_global_dir
- center_position_ring_rod_along_direction
)
)
# We will connect two pairs of nodes (2 from ring and 2 from helical rod). Thus also cache the
# position data for these two pairs.
helix_radius_points_for_interp[
0, 2 * idx
] = position_of_helical_rod_in_global_dir[index]
helix_radius_points_for_interp[1, 2 * idx] = (
self.shearable_rod1.radius[idx]
+ 2 * self.straight_rod_list[1].radius[idx]
+ 2 * self.ring_rod_list_outer[idx].radius[0]
+ helical_rod_radius_along_arm[idx]
)
helix_radius_points_for_interp[
0, 2 * idx + 1
] = position_of_helical_rod_in_global_dir[index + 1]
helix_radius_points_for_interp[1, 2 * idx + 1] = (
self.shearable_rod1.radius[idx]
+ 2 * self.straight_rod_list[1].radius[idx]
+ 2 * self.ring_rod_list_outer[idx].radius[0]
+ helical_rod_radius_along_arm[idx]
)
helical_rod_radius_points_for_interp[0, idx] = 0.5 * (
position_of_helical_rod_in_global_dir[index]
+ position_of_helical_rod_in_global_dir[index + 1]
)
helical_rod_radius_points_for_interp[1, idx] = helical_rod_radius_along_arm[
idx
]
from scipy.interpolate import interp1d
# Generate the interpolation function using the data points computed above.
helix_radius_interp_func = interp1d(
helix_radius_points_for_interp[0, :],
helix_radius_points_for_interp[1, :],
fill_value="extrapolate",
)
# Compute the helix radius, which has a dimension same as number of nodes.
helix_radius = helix_radius_interp_func(position_of_helical_rod_in_global_dir)
# Compute the helical rod radius. This is the radius of the rod and if arm is tapered it decreases from base
# to tip.
helical_rod_radius_interp_func = interp1d(
helical_rod_radius_points_for_interp[0, :],
helical_rod_radius_points_for_interp[1, :],
fill_value="extrapolate",
)
helical_rod_radius = helical_rod_radius_interp_func(
0.5
* (
position_of_helical_rod_in_global_dir[:-1]
+ position_of_helical_rod_in_global_dir[1:]
)
)
# normalized_active_force_break_points_helical_rods = np.ones(
# (n_elem_helical_rod)
# ) * normalized_active_force_break_points_longitudinal_muscles.reshape((4, 1))
# normalized_active_force_y_intercept_helical_rods = np.ones(
# (n_elem_helical_rod)
# ) * normalized_active_force_y_intercept_longitudinal_muscles.reshape((4, 1))
# normalized_active_force_slope_helical_rods = np.ones(
# (n_elem_helical_rod)