forked from SAP-archive/fedem-solvers
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodeler.py
More file actions
1805 lines (1512 loc) · 60.9 KB
/
Copy pathmodeler.py
File metadata and controls
1805 lines (1512 loc) · 60.9 KB
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
# SPDX-FileCopyrightText: 2023 SAP SE
#
# SPDX-License-Identifier: Apache-2.0
#
# This file is part of FEDEM - https://openfedem.org
"""
Python wrapper for the native fedem modeler.
This module provides functionality for creating new Fedem models,
and for modifying existing ones.
"""
from ctypes import POINTER, byref, c_bool, c_char_p, c_double, c_int
from os import environ, path
from fedempy.enums import FmDof, FmDofStat, FmType, FmVar
from fedempy.fmm import FedemModel, _convert_char, _convert_int, _convert_real
def _convert_bool(arg):
"""
Converts a boolean from Python to C type.
"""
if arg is None:
return c_bool(False)
if isinstance(arg, bool):
return c_bool(arg)
if isinstance(arg, c_bool):
return arg
raise TypeError(f"Expected {bool} or {c_bool}, got {type(arg)}.")
def _convert_int_array(arg):
"""
Converts an array of integers from Python to C type.
"""
if arg is None:
return c_int(0), None
if isinstance(arg, (list, tuple)):
argc = len(arg)
if argc > 0:
argv = (c_int * argc)()
argv[:] = arg
else:
argv = None
return c_int(argc), argv
if hasattr(arg, "_length_") and getattr(arg, "_type_", None) is c_int:
return c_int(len(arg)), arg
raise TypeError(f"Expected {list} or {c_int}, got {type(arg)}.")
def _convert_float_array(arg):
"""
Converts an array of floats from Python to C type.
"""
if arg is None:
return c_int(0), None
if isinstance(arg, (list, tuple)):
argc = len(arg)
if argc > 0:
argv = (c_double * argc)()
argv[:] = arg
else:
argv = None
return c_int(argc), argv
if hasattr(arg, "_length_") and getattr(arg, "_type_", None) is c_double:
return c_int(len(arg)), arg
raise TypeError(f"Expected {list} or {c_double}, got {type(arg)}.")
def _extract_dofs(arg):
"""
Extracts 6 DOF values into a c_double array from a dictionary.
"""
if not isinstance(arg, dict):
return None
has_dofs = False
dof_tags = ["Tx", "Ty", "Tz", "Rx", "Ry", "Rz"]
dof_vals = (c_double * 6)(*([0.0] * 6))
for key, value in arg.items():
if key in dof_tags:
dof_vals[dof_tags.index(key)] = _convert_real(value)
has_dofs = True
if has_dofs:
return dof_vals
return None # Return nothing if no DOF tags were found
def _extract_polyline(arg):
"""
Extracts data for a polyline function from a dictionary.
"""
coord = arg["xy"]
n_ = c_int(len(coord))
x_ = (c_double * len(coord))()
y_ = (c_double * len(coord))()
x_[:] = [c_double(c[0]) for c in coord]
y_[:] = [c_double(c[1]) for c in coord]
extrap = ["NONE", "FLAT", "LINEAR"]
e_type = arg.get("extrapol_type", extrap[0])
if e_type in extrap:
e_ = c_int(extrap.index(e_type))
else:
e_ = c_int(0)
return n_, x_, y_, e_
class FedemModeler(FedemModel):
"""
This subclass of :class:`fmm.FedemModel` adds some basic modeling methods.
Parameters
----------
model_file : str, default=None
Absolute path to the fedem model file to open
force_new : bool, default=False
If True, any existing model will be overwritten on save
plugins : str or list of str, default=None
Plugin libraries with user-defined elements and functions
name : str, default=None
Description of the new model, not used if opening existing model
Methods
-------
open:
Opens the specified model file
save:
Saves current model into the specified model file
close:
Closes the currently open model
make_triad:
Creates a triad at specified location or node
make_beam:
Creates a string of beam elements
make_beam_section:
Creates a beam cross section property object
make_beam_material:
Creates a material property object
make_spring:
Creates a spring element
make_damper:
Creates a damper element
make_joint:
Creates a joint object
make_load:
Creates an external load object
make_function:
Creates a general function object
make_sensor:
Creates a sensor object
make_fe_part:
Creates an FE part
make_generic_part:
Creates a generic part
make_strain_rosette:
Creates a strain rosette on an FE part
make_udelm:
Creates a string of user-defined elements
edit_triad:
Modifies an existing triad
edit_part:
Modifies an existing FE part
edit_joint:
Modifies an existing joint
edit_function:
Modifies an existing function
solver_setup:
Setting dynamics solver parameters
"""
def __init__(self, model_file=None, force_new=False, plugins=None, name=None):
"""
Constructor.
Optionally opens a Fedem model, if a model file name is specified.
"""
fedem_lib = environ.get("FEDEM_MDB")
if not fedem_lib:
print("\n *** Environment variable FEDEM_MDB is not set!", flush=True)
return # This will probably cause crash later, but not here
if isinstance(plugins, str):
super().__init__(fedem_lib, plugins)
elif isinstance(plugins, list) and len(plugins) > 0:
if len(plugins) > 1:
super().__init__(fedem_lib, plugins[0], plugins[1])
else:
super().__init__(fedem_lib, plugins[0])
else:
super().__init__(fedem_lib)
self._fmlib.FmCreateTriad.restype = c_int
self._fmlib.FmCreateTriad.argtypes = [
c_char_p,
c_double,
c_double,
c_double,
c_double,
c_double,
c_double,
]
self._fmlib.FmTriadOnNode.restype = c_int
self._fmlib.FmTriadOnNode.argtypes = [c_char_p, c_int, c_int]
self._fmlib.FmCreateBeam.restype = c_int
self._fmlib.FmCreateBeam.argtypes = [c_char_p, c_int, c_int]
self._fmlib.FmCreateBeamProperty.restype = c_int
self._fmlib.FmCreateBeamProperty.argtypes = [
c_char_p,
c_int,
c_int,
POINTER(c_double),
]
self._fmlib.FmCreateMaterialProperty.restype = c_int
self._fmlib.FmCreateMaterialProperty.argtypes = [
c_char_p,
c_int,
POINTER(c_double),
]
self._fmlib.FmCreateSpring.restype = c_int
self._fmlib.FmCreateSpring.argtypes = [
c_char_p,
c_int,
c_int,
c_double,
c_bool,
c_double,
POINTER(c_int),
c_int,
POINTER(c_double),
POINTER(c_double),
c_int,
c_int,
]
self._fmlib.FmCreateDamper.restype = c_int
self._fmlib.FmCreateDamper.argtypes = [
c_char_p,
c_int,
c_int,
c_bool,
c_double,
POINTER(c_int),
c_int,
POINTER(c_double),
POINTER(c_double),
c_int,
]
self._fmlib.FmCreateJoint.restype = c_int
self._fmlib.FmCreateJoint.argtypes = [
c_char_p,
c_int,
c_int,
POINTER(c_int),
c_int,
]
self._fmlib.FmCreateLoad.restype = c_int
self._fmlib.FmCreateLoad.argtypes = [
c_char_p,
c_int,
c_int,
c_double,
c_double,
c_double,
c_char_p,
c_int,
]
self._fmlib.FmCreateMathExprFunc.restype = c_int
self._fmlib.FmCreateMathExprFunc.argtypes = [
c_char_p,
c_char_p,
c_char_p,
c_bool,
]
self._fmlib.FmCreateExternalFunc.restype = c_int
self._fmlib.FmCreateExternalFunc.argtypes = [c_char_p, c_char_p, c_bool]
self._fmlib.FmCreateSineFunc.restype = c_int
self._fmlib.FmCreateSineFunc.argtypes = [
c_char_p,
c_char_p,
POINTER(c_double),
c_bool,
]
self._fmlib.FmCreateLinearFunc.restype = c_int
self._fmlib.FmCreateLinearFunc.argstypes = [
c_char_p,
c_char_p,
POINTER(c_double),
c_bool,
]
self._fmlib.FmCreatePolyFunc.restype = c_int
self._fmlib.FmCreatePolyFunc.argtypes = [
c_char_p,
c_char_p,
c_int,
POINTER(c_double),
POINTER(c_double),
c_int,
c_bool,
]
self._fmlib.FmCreateDeviceFunc.restype = c_int
self._fmlib.FmCreateDeviceFunc.argtypes = [
c_char_p,
c_char_p,
c_char_p,
c_char_p,
c_double,
c_bool,
c_double,
c_bool,
]
self._fmlib.FmCreateSensor.restype = c_int
self._fmlib.FmCreateSensor.argtypes = [
c_char_p,
c_char_p,
c_int,
c_int,
c_int,
c_int,
c_int,
c_int,
]
self._fmlib.FmSetFunctionArg.restype = c_bool
self._fmlib.FmSetFunctionArg.argtypes = [
c_int,
c_int,
c_int,
c_int,
c_int,
c_int,
c_int,
]
self._fmlib.FmLoadPart.restype = c_int
self._fmlib.FmLoadPart.argtypes = [c_char_p, c_char_p]
self._fmlib.FmCreatePart.restype = c_int
self._fmlib.FmCreatePart.argtypes = [c_char_p, c_int, POINTER(c_int)]
self._fmlib.FmCreateStrainRosette.restype = c_int
self._fmlib.FmCreateStrainRosette.argtypes = [
c_char_p,
c_int,
c_int,
POINTER(c_int),
POINTER(c_double),
c_double,
c_bool,
]
self._fmlib.FmCreateUDE2.restype = c_int
self._fmlib.FmCreateUDE2.argtypes = [c_char_p, c_int, c_int]
self._fmlib.FmCreateAssembly.restype = c_int
self._fmlib.FmCreateAssembly.argtypes = [c_char_p, c_int, POINTER(c_int)]
self._fmlib.FmMoveObject.restype = c_bool
self._fmlib.FmMoveObject.argtypes = [c_int, POINTER(c_double), c_int, c_int]
self._fmlib.FmConstrainObject.restype = c_bool
self._fmlib.FmConstrainObject.argtypes = [c_int, c_int, c_int]
self._fmlib.FmAddMass.restype = c_bool
self._fmlib.FmAddMass.argtypes = [c_int, c_int, POINTER(c_double), c_int]
self._fmlib.FmDofProperty.restype = c_bool
self._fmlib.FmDofProperty.argtypes = [c_int, c_int, c_int, c_double, c_int]
self._fmlib.FmStructDamp.restype = c_bool
self._fmlib.FmStructDamp.argtypes = [c_int, c_double, c_double]
self._fmlib.FmReduceOpts.restype = c_bool
self._fmlib.FmReduceOpts.argtypes = [c_int, c_int, c_bool]
self._fmlib.FmRecoverOpts.restype = c_bool
self._fmlib.FmRecoverOpts.argtypes = [c_int, c_int, c_bool]
if model_file is None or not path.isfile(model_file) or force_new:
self.fm_new(model_file, name)
elif not self.fm_open(model_file):
print(" *** Failed to open model file", model_file)
def _convert_ids(self, obj_ids, obj_type):
"""
Convenience method, to reduce cognitive complexity.
"""
if isinstance(obj_ids, list):
return obj_ids
return self._convert_id(obj_ids, obj_type, True)
def open(self, model_file):
"""
Opens the specified model file and prints out some key model parameters.
Parameters
----------
model_file : str
Absolute path of the Fedem model file to open
Returns
-------
bool
True on success, otherwise False
"""
if not self.fm_open(model_file):
print(" *** Failed to open model file", model_file)
return False
print(" * Model file", model_file, "successfully opened")
print(" Number of Triads:", self.fm_count(FmType.TRIAD))
print(" Number of Beams:", self.fm_count(FmType.BEAM))
print(" Number of Parts:", self.fm_count(FmType.FEPART))
print(" Total number of mechanism objects:", self.fm_count())
return True
def save(self, model_file=None):
"""
Saves current model into the specified model file.
If no model_file is given, that last opened model file is overwritten.
Parameters
----------
model_file : str, default=None
Absolute path of the Fedem model file to save to
Returns
-------
bool
True on success, otherwise False
"""
status = self.fm_save(model_file)
if not status:
print(" *** Failed to save model file", model_file)
return status
def close(self, save=False, remove_singletons=False):
"""
Closes the currently open model.
Parameters
----------
save : bool, default=False
If True, the model file is updated with the current model
remove_singletons : bool, default=False
If True, heap-allocated singelton objects are also released
Returns
-------
bool
True on success, otherwise False
"""
status = self.fm_save() if save else True
if not status:
print(" *** Failed to save current fedem model")
self.fm_close(True)
else:
self.fm_close(remove_singletons)
return status
def make_triad(self, name, pos=None, rot=None, node=0, on_part=0, tag=None):
"""
Creates a new triad at specified location or nodal point.
Parameters
----------
name : str
Description of the new triad
pos : (float, float, float)
Global XYZ-coordinates of new triad
rot : (float, float, float), default=None
Global Euler angles giving the orientation of new triad
node : int, default=0
FE node number to associate the triad with.
Used only if `on_part` is specified.
on_part : int or str, default=0
Base Id or tag of the part that this triad should be attached to.
You can also specify the Reference plane here,
to create a new triad that is attached to ground.
tag : str, default=None
Tag to associate the created triad with
Returns
-------
int
Base Id of new triad, zero or negative on error
"""
part_ = self._convert_id(on_part)
if node > 0 and part_.value > 2:
triad = self._fmlib.FmTriadOnNode(
_convert_char(name), _convert_int(node), part_
)
elif len(pos) < 3:
print(" *** Invalid Triad position", pos)
triad = -1
elif rot is None:
if len(pos) > 5:
triad = self._fmlib.FmCreateTriad(
_convert_char(name),
_convert_real(pos[0]),
_convert_real(pos[1]),
_convert_real(pos[2]),
_convert_real(pos[3]),
_convert_real(pos[4]),
_convert_real(pos[5]),
part_,
)
else:
triad = self._fmlib.FmCreateTriad(
_convert_char(name),
_convert_real(pos[0]),
_convert_real(pos[1]),
_convert_real(pos[2]),
c_double(0),
c_double(0),
c_double(0),
part_,
)
elif len(rot) < 3:
print(" *** Invalid Triad rotation", rot)
triad = -1
else:
triad = self._fmlib.FmCreateTriad(
_convert_char(name),
_convert_real(pos[0]),
_convert_real(pos[1]),
_convert_real(pos[2]),
_convert_real(rot[0]),
_convert_real(rot[1]),
_convert_real(rot[2]),
part_,
)
if tag is not None and triad > 0:
self.fm_tag_object(triad, tag)
return triad
def make_beam(self, name, triads, bprop=None, tag=None):
"""
Creates a string of beam elements.
Parameters
----------
name : str
Description of the new beam(s)
triads : list of int or list of str
List of base Ids or tags of the connected triads
bprop : int or str, default=None
Base Id or tag of beam property to use
tag : str, default=None
Tag to associate the created beam(s) with
Returns
-------
list of int
Base Ids of the created beams, None if error
"""
if len(triads) < 2:
print(" *** make_beam: At least two triads must be specified")
return None
base_ids = []
for i in range(len(triads) - 1):
base_id = self._fmlib.FmCreateBeam(
_convert_char(name),
self._convert_id(triads[i], FmType.TRIAD),
self._convert_id(triads[i + 1], FmType.TRIAD),
self._convert_id(bprop, FmType.BEAM_PROP),
)
if base_id < 1:
return None
base_ids.append(base_id)
if tag is not None:
self.fm_tag_object(base_ids, tag)
return base_ids
def make_beam_section(self, name, mat, bprops, tag=None):
"""
Creates a beam cross section property object.
Parameters
----------
name : str
Description of the new beam property
mat : int or str
Cross section type flag.
If zero, a Generic cross section is defined.
If str or a non-zero int, a Pipe cross section is defined,
and the value gives the tag or base Id of the material to use.
bprop : list of float
List of property data
tag : str, default=None
Tag to associate the created beam property with
Returns
-------
int
Base Id of beam property object, zero or negative on error
"""
nprop_, prop_ = _convert_float_array(bprops)
bprop = self._fmlib.FmCreateBeamProperty(
_convert_char(name),
self._convert_id(mat, FmType.MAT_PROP),
nprop_,
prop_,
)
if tag is not None and bprop > 0:
self.fm_tag_object(bprop, tag)
return bprop
def make_beam_material(self, name, mprops, tag=None):
"""
Creates a material property object.
Parameters
----------
name : str
Description of the new material property
mprop : list of float
List of property data
tag : str, default=None
Tag to associate the created material property with
Returns
-------
int
Base Id of material property object, zero or negative on error
"""
nprop_, prop_ = _convert_float_array(mprops)
mprop = self._fmlib.FmCreateMaterialProperty(_convert_char(name), nprop_, prop_)
if tag is not None and mprop > 0:
self.fm_tag_object(mprop, tag)
return mprop
def make_spring(self, name, triads, **kwargs):
"""
Creates axial spring elements.
Parameters
----------
name : str
Description of the new spring(s)
triads : (str, str) or (int, int) or list of (int, int)
Tags or base Ids of the connected triads.
If a list of tuples is specified,
one spring is created for each tuple.
tag : str, default=None
Tag to associate the created beam(s) with
kwargs : dict
Keyword arguments defining the spring properties.
The following keywords are currently supported:
* `tag` : Tag to associate the created spring(s) with
* `constDefl`: Constant stress free deflection
* `constLength`: Constant stress free length
* `length`: User Id of general function defining stress free length
* `init_Stiff_Coeff`: Constant spring stiffness coefficient
* `fn`: Base Id of spring stiffness function
* `xy`: List of XY-pairs giving a piece-wise linear spring stiffness
* `extrapol_type`: String, either "NONE" (default), "FLAT" or "LINEAR"
* `spring_characteristics`: String, either "SPR_TRA_STIFF" (default)
or "SPR_TRA_FORCE"
Returns
-------
int or list of int
Base Id(s) of the created spring(s), None if an error occurs
"""
n_ = c_int(0)
x_ = None
y_ = None
e_ = c_int(0)
use_constant_defl_ = c_bool(True) # default is constant deflection
if "constDefl" in kwargs:
const_length_defl_ = _convert_real(kwargs["constDefl"])
elif "constLength" in kwargs:
const_length_defl_ = _convert_real(kwargs["constLength"])
use_constant_defl_ = c_bool(False)
else:
const_length_defl_ = c_double(0)
if "fn" in kwargs: # Base Id of existing spring function
sp_ = _convert_int(kwargs["fn"])
else:
sp_ = c_int(0)
if "xy" in kwargs:
# We have data of a new spring stiffness function
n_, x_, y_, e_ = _extract_polyline(kwargs)
charac = [
"SPR_TRA_STIFF", # stiffness - translational deflection
"SPR_TRA_FORCE", # force - translational deflection
]
sp_charac = kwargs.get("spring_characteristics", charac[0])
if sp_charac in charac:
# Notice negative value to indicate spring function type
sp_ = c_int(-charac.index(sp_charac))
one_spring = isinstance(triads, tuple)
if one_spring:
triads = [triads] # Only one spring is created
base_ids = []
for triad in triads:
s_id = self._fmlib.FmCreateSpring(
_convert_char(name),
self._convert_id(triad[0], FmType.TRIAD),
self._convert_id(triad[1], FmType.TRIAD),
const_length_defl_,
use_constant_defl_,
_convert_real(kwargs.get("init_Stiff_Coeff", 0.0)),
byref(sp_),
n_,
x_,
y_,
e_,
_convert_int(kwargs.get("length", 0)),
)
if s_id > 0:
base_ids.append(s_id)
if len(base_ids) < len(triads):
return None # Failure creating at least one spring
if "tag" in kwargs:
self.fm_tag_object(base_ids, kwargs["tag"])
if one_spring:
return base_ids[0]
return base_ids
def make_damper(self, name, triads, **kwargs):
"""
Creates axial damper elements.
Parameters
----------
name : str
Description of the new damper(s)
triads : (str, str) or (int, int) or list of (int, int)
Tags or base Ids of the connected triads.
If a list of tuples is specified,
one damper is created for each tuple.
kwargs : dict
Keyword arguments defining the damper properties.
The following keywords are currently supported:
* `tag` : Tag to associate the created damper(s) with
* `def_vel_damper`: If True, use deformational velocity
* `init_Damp_Coeff`: Constant damping coefficient
* `fn`: Base Id of damping coefficient function
* `xy`: List of XY-pairs giving a piece-wise linear damping coefficient
* `extrapol_type`: String, either "NONE" (default), "FLAT" or "LINEAR"
* `damp_characteristics`: String, either "DA_TRA_COEFF" (default)
or "DA_TRA_FORCE"
Returns
-------
int or list of int
Base Id(s) of the created damper(s), None if an error occurs
"""
n_ = c_int(0)
x_ = None
y_ = None
e_ = c_int(0)
if "fn" in kwargs: # Base Id of existing damper function
da_ = _convert_int(kwargs["fn"])
else:
da_ = c_int(0)
if "xy" in kwargs:
# We have data of a new damping coefficient function
n_, x_, y_, e_ = _extract_polyline(kwargs)
charac = [
"DA_TRA_COEFF", # damping coefficient - translational velocity
"DA_TRA_FORCE", # force - translational velocity
]
da_charac = kwargs.get("damp_characteristics", charac[0])
if da_charac in charac:
# Notice negative value to indicate damper function type
da_ = c_int(-charac.index(da_charac))
one_damper = isinstance(triads, tuple)
if one_damper:
triads = [triads] # Only one damper is created
base_ids = []
for triad in triads:
d_id = self._fmlib.FmCreateDamper(
_convert_char(name),
self._convert_id(triad[0], FmType.TRIAD),
self._convert_id(triad[1], FmType.TRIAD),
_convert_bool(kwargs.get("def_vel_damper", False)),
_convert_real(kwargs.get("init_Damp_Coeff", 0.0)),
byref(da_),
n_,
x_,
y_,
e_,
)
if d_id > 0:
base_ids.append(d_id)
if len(base_ids) < len(triads):
return None # Failure creating at least one damper
if "tag" in kwargs:
self.fm_tag_object(base_ids, kwargs["tag"])
if one_damper:
return base_ids[0]
return base_ids
def make_joint(self, name, joint_type, follower, followed=None, tag=None):
"""
Creates a joint object.
Parameters
----------
name : str
Description of the new joint
joint_type : FmType
Type of joint
follower : int or str
Base Id or tag of the dependent joint triad
followed : int or str or list of int, default=None
Base Id or tag of the independent joint triad(s).
If None, the joint is connected to ground and the independent
triad is created at the same location as the dependent triad.
For point-to-path joints, the first two triads specified
are taken as the end points of the glider.
tag : str, default=None
Tag to associate the created joint with
Returns
-------
int
Base Id of joint object, zero or negative on error
"""
id_ = self._convert_id(follower, FmType.TRIAD)
if isinstance(followed, list):
nids_, ids_ = _convert_int_array(followed)
else:
nids_ = c_int(1)
ids_ = (c_int * 1)(self._convert_id(followed, FmType.TRIAD))
joint = self._fmlib.FmCreateJoint(
_convert_char(name), _convert_int(joint_type), id_, ids_, nids_
)
if tag is not None and joint > 0:
self.fm_tag_object(joint, tag)
return joint
def make_load(
self, name, load_type, triad, load_dir, magnitude=None, fn=0, tag=None
):
"""
Creates an external load object.
Parameters
----------
name : str
Description of the new load
load_type : FmLoadType
Type of load
triad : int or str
Base Id or tag of triad where the load attacks
load_dir : (float, float, float)
Load direction vector
magnitude : str, default=None
Load magnitude expression
fn : int, default=0
User Id of load magnitude function
tag : str, default=None
Tag to associate the created load with
Returns
-------
int
Base Id of load object, zero or negative on error
"""
if len(load_dir) < 3:
print(" *** Invalid load direction vector", load_dir)
return -1
load = self._fmlib.FmCreateLoad(
_convert_char(name),
_convert_int(load_type),
self._convert_id(triad, FmType.TRIAD),
_convert_real(load_dir[0]),
_convert_real(load_dir[1]),
_convert_real(load_dir[2]),
_convert_char(magnitude),
_convert_int(fn),
)
if tag is not None and load > 0:
self.fm_tag_object(load, tag)
return load
def make_function(self, name, **kwargs):
"""
Creates a general function of time.
The type of function to be created is determined
by which keyword arguments are provided.
The keyword determining the function type is below marked
by an asterix (:sup:`*`) in each case.
Parameters
----------
name : str
Description of the new function
kwargs : dict
Keyword arguments depending on function type.
The following **function types** and `keywords`
are currently supported:
1. **Polyline**
| `xy`:sup:`*`: List of `XY`-pairs giving a
piece-wise linear curve
| `extrapol_type`: String, either "NONE" (default),
"FLAT" or "LINEAR"
2. **Polyline-from-file**
| `filename`:sup:`*`: Name of file containing `XY`-pairs
| `ch_name`: String identifying the column to use
for multi-column files
| `sc_factor`: Scaling factor, default=1.0
| `z_adjust`: If True, the `y`-values are shifted
such that the first value is zero, default=False
| `v_shift`: Additional shift of the `y`-values, default=0.0
3. **Sine**
| `frequency`:sup:`*`: Angular frequency
| `amplitude`: Scaling factor, default=1.0
| `delay`: Phase shift, default=0.0
| `mean_value`: Constant shift, default=0.0
| `end`: Default=0.0, if greater than zero, the function
value is constant for `x` greater than `end`
The Sine function therefore evaluates to::
f(x) = amplitude*sin(frequency*x-delay) + mean_value
for `x`-values less than `end`, whereas ``f(x) = f(end)``