Import Hydrodynamic Coefficients
Import Hydrodynamic Coefficients.
This example demonstrates how to import hydrodynamic coefficients data into SIMA using the SIMAPy API.
The script shows how to use SIMA along with simapy to import hydrodynamic coefficients from datasets generated by WAMIT. The same approach can be used to import data from HydroD/WADAM or any other file format which is supported by SIMA.
- Features:
- Hydrodynamic Data Import:
Demonstrates importing hydrodynamic coefficients from WAMIT
- SIMA Integration:
Shows the workflow for bringing external hydrodynamic data into SIMA
- Data Export:
Saves the processed data to a JSON file that can be imported into SIMA
- Requirements:
Environment variable SRE_EXE which points to the sre executable of SIMA
SIMA version 4.4.0 or later
Example
Basic usage of this script:
>>> python import_hydrodynamic_coefficients.py
This example is particularly useful for importing hydrodynamic data from external analysis tools and preparing models that require accurate hydrodynamic coefficients for simulations.
- import_file(workspace, filename)[source]
Import a file of a type supported by SIMA as a list of tasks.
This function uses the SIMA Runtime Engine (SRE) to import data from external files into SIMA’s internal format. It supports various file formats including WAMIT output files and HydroD/WADAM G1.SIF files.
- Parameters:
workspace (Path) – Directory where SRE will store temporary files
filename (Path) – Path to the file to import
- Returns:
A list of imported SIMA tasks
- Return type:
list
Notes
The function uses an environmental variable SRE_EXE to locate the SIMA installation, make sure it is set before running.
- main()[source]
Import hydrodynamic coefficients from WAMIT dataset using SRE and export to JSON file.
This function demonstrates the complete workflow for importing hydrodynamic data from WAMIT: 1. Create a workspace for the import/export operation 2. Import the WAMIT .out file using the import_file helper function 3. Export the imported task to a JSON file for later use in SIMA
The generated JSON file can be imported into SIMA to set up simulations using the hydrodynamic coefficients.
- Returns:
The function writes output to disk but doesn’t return any value
- Return type:
None
- run_sima(workspace, commands)[source]
Run SIMA with the given workspace directory and command line arguments.
The function uses an environmental variable SRE_EXE to locate the SIMA installation, make sure it is set before running.
Source Code
1"""Import Hydrodynamic Coefficients.
2
3This example demonstrates how to import hydrodynamic coefficients data into SIMA using the SIMAPy API.
4
5The script shows how to use SIMA along with simapy to import hydrodynamic
6coefficients from datasets generated by WAMIT. The same approach can be used to import
7data from HydroD/WADAM or any other file format which is supported by SIMA.
8
9Features:
10 Hydrodynamic Data Import:
11 Demonstrates importing hydrodynamic coefficients from WAMIT
12 SIMA Integration:
13 Shows the workflow for bringing external hydrodynamic data into SIMA
14 Data Export:
15 Saves the processed data to a JSON file that can be imported into SIMA
16
17Requirements:
18 - Environment variable `SRE_EXE` which points to the sre executable of SIMA
19 - SIMA version 4.4.0 or later
20
21Example:
22 Basic usage of this script:
23
24 >>> python import_hydrodynamic_coefficients.py
25
26 This example is particularly useful for importing hydrodynamic data from external
27 analysis tools and preparing models that require accurate hydrodynamic coefficients
28 for simulations.
29"""
30import os
31import shutil
32from pathlib import Path
33
34from simapy.sre import SIMA
35from simapy.sima_reader import SIMAReader
36from simapy.sima_writer import SIMAWriter
37
38from simapy.sima.command import ImportCommand, ExportCommand
39
40SCRIPT_DIR = Path(os.path.realpath(os.path.dirname(__file__)))
41
42
43def main():
44 """
45 Import hydrodynamic coefficients from WAMIT dataset using SRE and export to JSON file.
46
47 This function demonstrates the complete workflow for importing hydrodynamic data from WAMIT:
48 1. Create a workspace for the import/export operation
49 2. Import the WAMIT .out file using the import_file helper function
50 3. Export the imported task to a JSON file for later use in SIMA
51
52 The generated JSON file can be imported into SIMA to set up simulations using the
53 hydrodynamic coefficients.
54
55 Returns:
56 None: The function writes output to disk but doesn't return any value
57 """
58 # Here we use the import_file function defined below to import data from the WAMIT
59 # dataset by pointing at the .out-file. The same function could be used to for
60 # example to import a G1.SIF file created by HydroD/WADAM as well.
61 # The function returns a list of tasks because certain file formats (e.g. stasks)
62 # might contain multiple tasks. WAMIT result files only create a single task
63 # hence the variable is assigned to the zeroth element of the list.
64
65 workspace = Path("output/simo/simo_wamit_import")
66 if workspace.exists():
67 shutil.rmtree(workspace, ignore_errors=True)
68 os.makedirs(workspace)
69
70 task = import_file(workspace, SCRIPT_DIR / "wamit_data" / "sima.out")[0]
71
72 # After the task has been imported we could do some manipulations to it, add more
73 # data and perhaps make SIMA run a simulation with the task. For this example we
74 # just export it to a JSON file called imported.json. This file can be imported
75 # into SIMA in order to inspect the results.
76 file = workspace / "simo_wamit_import.json"
77 os.makedirs(file.parent, exist_ok=True)
78 SIMAWriter().write([task], file)
79
80
81def import_file(workspace: Path, filename: Path):
82 """Import a file of a type supported by SIMA as a list of tasks.
83
84 This function uses the SIMA Runtime Engine (SRE) to import data from external
85 files into SIMA's internal format. It supports various file formats including WAMIT
86 output files and HydroD/WADAM G1.SIF files.
87
88 Args:
89 workspace (Path): Directory where SRE will store temporary files
90 filename (Path): Path to the file to import
91
92 Returns:
93 list: A list of imported SIMA tasks
94
95 Notes:
96 The function uses an environmental variable SRE_EXE to locate the SIMA installation,
97 make sure it is set before running.
98 """
99 out_file = workspace / "out.json"
100
101 # Run SIMA to import SIF file and export JSON
102 import_command = ImportCommand(file=str(filename.absolute()))
103 export_command = ExportCommand(file=str(out_file.absolute()), delete=True)
104 commands = [import_command, export_command]
105
106 run_sima(workspace, commands)
107
108 # Import JSON file exported by SIMA
109 return SIMAReader().read(out_file)
110
111
112def run_sima(workspace: Path, commands: list):
113 """Run SIMA with the given workspace directory and command line arguments.
114
115 The function uses an environmental variable SRE_EXE to locate the SIMA installation,
116 make sure it is set before running.
117 """
118 sima = SIMA()
119 sima.run(workspace, commands)
120
121
122if __name__ == "__main__":
123 main()