Skip to content
Snippets Groups Projects
Commit 13d9b8df authored by Mads M. Pedersen's avatar Mads M. Pedersen
Browse files

add save function in mann_turbulence.py

parent 0e457785
No related branches found
No related tags found
1 merge request!159add save function in mann_turbulence.py
Pipeline #16535 passed
image: dtuwindenergy/wetb
test-3.4:
stage:
test
# except:
# - test_pypi
before_script:
- git submodule sync --recursive
- git submodule update --init --recursive
script:
- apt-get update
- pip3 install pytest
......
......@@ -8,8 +8,8 @@ import wetb
import inspect
from urllib.request import urlretrieve
wetb_rep_path = os.path.abspath(os.path.dirname(wetb.__file__) + "/../") + "/"
local_TestFiles_path = wetb_rep_path + "TestFiles/"
wetb_rep_path = os.path.abspath(os.path.dirname(wetb.__file__) + "/../").replace("\\", "/") + "/"
local_TestFiles_path = wetb_rep_path + "TestFiles/"
remote_TestFiles_url = "https://gitlab.windenergy.dtu.dk/toolbox/TestFiles/raw/master/"
......@@ -19,19 +19,17 @@ def _absolute_filename(filename):
caller_module_path = os.path.dirname(inspect.stack()[index][1])
tfp = caller_module_path + "/test_files/"
filename = tfp + filename
return filename
return os.path.abspath(filename).replace("\\", "/")
def get_test_file(filename):
filename = _absolute_filename(filename)
filename = _absolute_filename(filename)
if not os.path.exists(filename):
rel_path = os.path.relpath(filename, wetb_rep_path).replace("\\","/")
rel_path = os.path.relpath(filename, wetb_rep_path).replace("\\", "/")
filename = local_TestFiles_path + rel_path
if not os.path.exists(filename):
urlretrieve(remote_TestFiles_url + rel_path, filename)
return filename
def move2test_files(filename):
......@@ -42,8 +40,3 @@ def move2test_files(filename):
if not os.path.exists(folder):
os.makedirs(folder)
os.rename(filename, dst_filename)
\ No newline at end of file
......@@ -8,38 +8,42 @@ import numpy as np
from wetb.wind.turbulence.spectra import spectra, spectra_from_time_series
name_format = "mann_l%.1f_ae%.4f_g%.1f_h%d_%dx%dx%d_%.3fx%.2fx%.2f_s%04d%c.turb"
def load(filename, N=(32,32)):
def load(filename, N=(32, 32)):
"""Load mann turbulence box
Parameters
----------
filename : str
Filename of turbulence box
N : tuple, (ny,nz) or (nx,ny,nz)
Number of grid points
Returns
-------
turbulence_box : nd_array
Examples
--------
>>> u = load('turb_u.dat')
>>> u = load('turb_u.dat')
"""
data = np.fromfile(filename, np.dtype('<f'), -1)
if len(N)==2:
ny,nz = N
nx = len(data)/(ny*nz)
assert nx==int(nx), "Size of turbulence box (%d) does not match ny x nz (%d), nx=%.2f" % (len(data),ny*nz, nx)
nx=int(nx)
if len(N) == 2:
ny, nz = N
nx = len(data) / (ny * nz)
assert nx == int(nx), "Size of turbulence box (%d) does not match ny x nz (%d), nx=%.2f" % (
len(data), ny * nz, nx)
nx = int(nx)
else:
nx,ny,nz = N
assert len(data) == nx*ny*nz, "Size of turbulence box (%d) does not match nx x ny x nz (%d)" % (len(data),nx*ny*nz)
return data.reshape(nx , ny * nz)
nx, ny, nz = N
assert len(data) == nx * ny * \
nz, "Size of turbulence box (%d) does not match nx x ny x nz (%d)" % (len(data), nx * ny * nz)
return data.reshape(nx, ny * nz)
def load_uvw(filenames, N=(1024,32,32)):
def load_uvw(filenames, N=(1024, 32, 32)):
"""Load u, v and w turbulence boxes
Parameters
----------
filenames : list or str
......@@ -47,34 +51,40 @@ def load_uvw(filenames, N=(1024,32,32)):
if str: filename pattern where u,v,w are replaced with '%s'
N : tuple
Number of grid point in the x, y and z direction
Returns
-------
u,v,w : list of np_array
Examples
--------
>>> u,v,w =load_uvw('turb_%s.dat')
>>> u,v,w =load_uvw('turb_%s.dat')
"""
if isinstance(filenames,str):
return [load(filenames%uvw, N) for uvw in 'uvw']
if isinstance(filenames, str):
return [load(filenames % uvw, N) for uvw in 'uvw']
else:
return [load(f, N) for f in filenames]
def parameters2name(no_grid_points,box_dimension,ae23,L, Gamma,high_frq_compensation, seed, folder="./turb/"):
def save(turb, filename):
np.array(turb).astype('<f').tofile(filename)
def parameters2name(no_grid_points, box_dimension, ae23, L, Gamma, high_frq_compensation, seed, folder="./turb/"):
dxyz = tuple(np.array(box_dimension) / no_grid_points)
return ["./turb/" + name_format % ((L, ae23, Gamma, high_frq_compensation) + no_grid_points + dxyz + (seed, uvw)) for uvw in ['u', 'v', 'w']]
return ["./turb/" + name_format % ((L, ae23, Gamma, high_frq_compensation) +
no_grid_points + dxyz + (seed, uvw)) for uvw in ['u', 'v', 'w']]
def fit_mann_parameters(spatial_resolution, u, v, w=None, plt=None):
"""Fit mann parameters, ae, L, G
Parameters
----------
spatial_resolution : inf, float or array_like
Distance between samples in meters
- For turbulence boxes: 1/dx = Lx/Nx where dx is distance between points,
- For turbulence boxes: 1/dx = Lx/Nx where dx is distance between points,
Nx is number of points and Lx is box length in meters
- For time series: Sample frequency / U
u : array_like
......@@ -89,7 +99,7 @@ def fit_mann_parameters(spatial_resolution, u, v, w=None, plt=None):
The w-wind component
- if shape is (r,): One time series with *r* observations\n
- if shape is (r,c): *c* different time series with *r* observations\n
Returns
-------
ae : int or float
......@@ -105,4 +115,4 @@ def fit_mann_parameters(spatial_resolution, u, v, w=None, plt=None):
def fit_mann_parameters_from_time_series(sample_frq, Uvw_lst, plt=None):
from wetb.wind.turbulence.mann_parameters import fit_mann_model_spectra
return fit_mann_model_spectra(*spectra_from_time_series(sample_frq, Uvw_lst), plt=plt)
\ No newline at end of file
return fit_mann_model_spectra(*spectra_from_time_series(sample_frq, Uvw_lst), plt=plt)
'''
Created on 20. jul. 2017
@author: mmpe
'''
import unittest
from wetb.utils.test_files import get_test_file
from wetb.wind.turbulence import mann_turbulence
from wetb.wind.turbulence.mann_turbulence import parameters2name
from tests import npt
class TestMannTurbulence(unittest.TestCase):
def testLoad(self):
fn = get_test_file('h2a8192_8_8_16384_32_32_0.15_10_3.3u.dat')
self.assertRaises(AssertionError, mann_turbulence.load, fn, (31, 31))
self.assertRaises(AssertionError, mann_turbulence.load, fn, (8192, 32, 32))
u = mann_turbulence.load(fn, (8192, 8, 8))
self.assertEqual(u.shape, (8192, 8 * 8))
def test_loaduvw(self):
fn = 'h2a8192_8_8_16384_32_32_0.15_10_3.3%s.dat'
fn_lst = [get_test_file(fn % uvw) for uvw in 'uvw']
u, v, w = mann_turbulence.load_uvw(fn_lst, (8192, 8, 8))
self.assertEqual(u.shape, (8192, 8 * 8))
self.assertTrue(u.shape == v.shape == w.shape == (8192, 8 * 8))
u, v, w = mann_turbulence.load_uvw(fn_lst[0].replace("u.dat", "%s.dat"), (8192, 8, 8))
self.assertEqual(u.shape, (8192, 8 * 8))
self.assertTrue(u.shape == v.shape == w.shape == (8192, 8 * 8))
def test_parameters2name(self):
self.assertEqual(parameters2name((8192, 8, 8), (16384, 32, 32), 0.15, 10, 3.3, 1, 1)[
0], './turb/mann_l10.0_ae0.1500_g3.3_h1_8192x8x8_2.000x4.00x4.00_s0001u.turb')
def test_save(self):
fn = get_test_file('h2a8192_8_8_16384_32_32_0.15_10_3.3u.dat')
u_ref = mann_turbulence.load(fn, (8192, 8, 8))
mann_turbulence.save(u_ref, 'tmp_u.turb')
u = mann_turbulence.load('tmp_u.turb', (8192, 8, 8))
npt.assert_array_equal(u, u_ref)
# def test_name2parameters(self):
# self.assertEqual(name2parameters('./turb/mann_l10.0_ae0.15_g3.3_h1_8192x8x8_2.000x4.00x4.00_s0001u.turb'),
# ((8192, 8, 8), (16384., 32., 32.), 0.15, 10, 3.3, 1, 1))
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment