Read and Write RIFLEX Model
Read and Write RIFLEX Models.
This example demonstrates how to manipulate RIFLEX models programmatically using the SIMAPy API.
- The example includes:
Importing an existing RIFLEX model from a JSON file (exported from SIMA)
Using the first line in the model as a template
Creating multiple new lines with randomized positions
Exporting the modified model back to a JSON file that can be imported into SIMA
>>> python read_and_write_riflex.py
- random() x in the interval [0, 1).
Source Code
read_and_write_riflex.py
1"""Read and Write RIFLEX Models.
2
3This example demonstrates how to manipulate RIFLEX models programmatically using the SIMAPy API.
4
5The example includes:
6 1. Importing an existing RIFLEX model from a JSON file (exported from SIMA)
7 2. Using the first line in the model as a template
8 3. Creating multiple new lines with randomized positions
9 4. Exporting the modified model back to a JSON file that can be imported into SIMA
10
11 >>> python read_and_write_riflex.py
12"""
13
14import os
15from pathlib import Path
16from random import random
17
18from simapy.sima.riflex.riflextask import RIFLEXTask
19from simapy.sima.riflex.supernode import SuperNode
20from simapy.sima_reader import SIMAReader
21from simapy.sima_writer import SIMAWriter
22
23
24def __copy_and_move(sn: SuperNode, offset):
25 """
26 Copy a SuperNode and move the copy with the given offset.
27
28 This helper function creates a copy of a SuperNode and applies a position
29 offset to both the initial and static positions in the x and y coordinates.
30
31 Args:
32 sn (SuperNode): The source SuperNode to copy
33 offset (float): The offset distance to apply to x and y coordinates
34
35 Returns:
36 SuperNode: A new SuperNode with the offset applied
37 """
38 sn_copy = sn.copy()
39 sn_copy.xInitial = sn_copy.xInitial + offset
40 sn_copy.yInitial = sn_copy.yInitial + offset
41 sn_copy.xStatic = sn_copy.xStatic + offset
42 sn_copy.yStatic = sn_copy.yStatic + offset
43 return sn_copy
44
45def main():
46 """Main function to execute the RIFLEX model example."""
47 reader = SIMAReader()
48 root = Path(__file__).parent / ".." / ".."
49 input = root / "input"
50 contents = reader.read(input / "riflex" / "simple_flexible_riser.json")
51 # Using type hints is not necessary but is useful to get automatic help when using an IDE, such as vscode or pycharm
52 task: RIFLEXTask = contents[0]
53 # We will use the first line as a template to generate new ones
54 model = task.model
55 lines = model.slenderSystem.lines
56 nodes = model.slenderSystem.superNodes
57 line = lines[0]
58
59 for name in ["line_1", "line_2", "line_3"]:
60 line_i = line.copy()
61 line_i.name = name
62 # create two new nodes and move to random position
63 end_offset = 300.0 * random()
64
65 sn_end_1: SuperNode = __copy_and_move(line_i.end1, end_offset)
66 sn_end_1.name = name + "_end1"
67 line_i.end1 = sn_end_1
68 nodes.append(sn_end_1)
69
70 sn_end_2: SuperNode = __copy_and_move(line_i.end2, end_offset)
71 sn_end_2.name = name + "_end2"
72 line_i.end2 = sn_end_2
73 nodes.append(sn_end_2)
74
75 lines.append(line_i)
76
77 file = root / "output/riflex/simple_flexible_riser_mod.json"
78 os.makedirs(file.parent, exist_ok=True)
79 writer = SIMAWriter()
80 writer.write([task], file)
81
82
83if __name__ == "__main__":
84 main()