Example #1
0
    def wave_force_from_capytaine(self, filename, mode):
        """
        Reads hydrodynamic data from a netCFD file created with capytaine and copies the
        data for the requested mode into the object.

        Args:
            filename: .nc file to read from
            mode: Name of the mode to read ('Heave' 'Pitch' ... 'Sway' 'Yaw')

        Returns:
            None

        Examples:
            test = Rao()
            test.wave_force_from_capytaine(r"capytaine.nc", "Heave")

        """

        from capytaine.io.xarray import merge_complex_values
        dataset = merge_complex_values(xr.open_dataset(filename))

        dataset['wave_direction'] *= 180 / np.pi  # convert rad/s to deg

        if 'excitation_force' not in dataset:
            dataset['excitation_force'] = dataset[
                'Froude_Krylov_force'] + dataset['diffraction_force']

        da = dataset['excitation_force'].sel(influenced_dof=mode)

        self._data['amplitude'] = abs(da)
        self._data['complex_unit'] = xr.DataArray(da / abs(da),
                                                  dims=('omega',
                                                        'wave_direction'))

        self._data.coords['mode'] = mode
    def load_data(self):
        from capytaine.io.xarray import merge_complex_values

        results_data = merge_complex_values(
            xarray.open_dataset(self.hydrodynamics_file_name))

        return results_data
Example #3
0
 def __read_bem__(self, fpath=None) -> xr.Dataset:
     if fpath is None: #TODO - should be able to do this as "from_dir" and build entire object
         fpath = os.path.join(self.params['wrk_dir'], self.params['name'] + '.nc')
     assert isinstance(fpath, str), 'fpath must be type(str), received {:}'.format(type(fpath))
     assert os.path.exists(fpath)
     hydro = merge_complex_values(xr.open_dataset(fpath)) # TODO - this could result in a mismatch between mesh and hydro
     self.hydro = hydro
     self.__post_proc_bem__()
     return hydro
Example #4
0
    def load_from_capytaine(self, filename):

        from capytaine.io.xarray import merge_complex_values
        dataset = merge_complex_values(xr.open_dataset(filename))
        dataset['wave_direction'] *= 180 / np.pi  # convert rad/s to deg

        self._force.clear()
        for mode in self._modes:
            r = Rao()
            r.wave_force_from_capytaine(filename, mode)
            r.scale(self._N_to_kN)
            self._force.append(r)

        self._damping = dataset['radiation_damping'] * self._N_to_kN
        self._mass = dataset['added_mass'] * self._kg_to_mt
Example #5
0
def test_remove_complex_values():
    original_dataset = xr.Dataset(
        data_vars={
            'variable1': (['x', 'y'], np.random.rand(4, 5)),
            'variable2': (['x', 'y', 'z'], np.random.rand(4, 5, 3) + 1j * np.random.rand(4, 5, 3)),
            'variable3': (['y', 'z'], np.random.rand(5, 3) + 1j * np.random.rand(5, 3)),
        },
        coords={
            'x': (['x'], np.linspace(0, 10, 4))
        })

    real_dataset = separate_complex_values(original_dataset)
    assert np.allclose(real_dataset['variable3'].sel(complex='re').data,
                       np.real(original_dataset['variable3'].data),
                       atol=0.1)
    assert set(original_dataset.dims) == set(real_dataset.dims) - {'complex'}

    complex_dataset = merge_complex_values(real_dataset)
    assert set(original_dataset.dims) == set(complex_dataset.dims)