Import hydrodynamic coefficients

Import hydrodynamic coefficients computed by WAMIT through SRE, and save the result as a JSON file that SIMA imports.

The same approach imports HydroD/WADAM results or any other file format SIMA reads.

1. Run it

Set SRE_EXE to the SIMA Runtime Engine executable, sre.exe in the SIMA installation folder. From the examples/ folder:

python simo/import_hydrodynamic_coefficients.py

2. Source code

import_hydrodynamic_coefficients.py
"""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 os
import shutil
from pathlib import Path

from simapy.sre import SIMA
from simapy.sima_reader import SIMAReader
from simapy.sima_writer import SIMAWriter

from simapy.sima.command import ImportCommand, ExportCommand

SCRIPT_DIR = Path(os.path.realpath(os.path.dirname(__file__)))


def main():
    """
    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:
        None: The function writes output to disk but doesn't return any value
    """
    # Here we use the import_file function defined below to import data from the WAMIT
    # dataset by pointing at the .out-file. The same function could be used to for
    # example to import a G1.SIF file created by HydroD/WADAM as well.
    # The function returns a list of tasks because certain file formats (e.g. stasks)
    # might contain multiple tasks. WAMIT result files only create a single task
    # hence the variable is assigned to the zeroth element of the list.

    workspace = Path("output/simo/simo_wamit_import")
    if workspace.exists():
        shutil.rmtree(workspace, ignore_errors=True)
    os.makedirs(workspace)

    task = import_file(workspace, SCRIPT_DIR / "wamit_data" / "sima.out")[0]

    # After the task has been imported we could do some manipulations to it, add more
    # data and perhaps make SIMA run a simulation with the task. For this example we
    # just export it to a JSON file called imported.json. This file can be imported
    # into SIMA in order to inspect the results.
    file = workspace / "simo_wamit_import.json"
    os.makedirs(file.parent, exist_ok=True)
    SIMAWriter().write([task], file)


def import_file(workspace: Path, filename: Path):
    """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.
    
    Args:
        workspace (Path): Directory where SRE will store temporary files
        filename (Path): Path to the file to import
        
    Returns:
        list: A list of imported SIMA tasks
        
    Notes:
        The function uses an environmental variable SRE_EXE to locate the SIMA installation,
        make sure it is set before running.
    """
    out_file = workspace / "out.json"

    # Run SIMA to import SIF file and export JSON
    import_command = ImportCommand(file=str(filename.absolute()))
    export_command = ExportCommand(file=str(out_file.absolute()), delete=True)
    commands = [import_command, export_command]

    run_sima(workspace, commands)

    # Import JSON file exported by SIMA
    return SIMAReader().read(out_file)


def run_sima(workspace: Path, commands: list):
    """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.
    """
    sima = SIMA()
    sima.run(workspace, commands)


if __name__ == "__main__":
    main()