Skip to content

Commit 4cb3489

Browse files
committed
Rename GenerateFluctuationField to FluctuationFieldGenerator and typing
1 parent 2f03693 commit 4cb3489

18 files changed

+465
-415
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
from pathlib import Path
22

33
# from plotly.graph_objs import *
4-
54
import numpy as np
65
import torch
76

87
from drdmannturb.fluctuation_generation import (
9-
plot_velocity_components, # utility function for plotting each velocity component in the field, not used in this example
10-
)
11-
from drdmannturb.fluctuation_generation import (
12-
GenerateFluctuationField,
13-
plot_velocity_magnitude,
8+
FluctuationFieldGenerator,
149
)
1510

1611
path = Path().resolve()
@@ -30,7 +25,7 @@
3025
reference_height = 180.0
3126
roughness_height = 0.75
3227

33-
grid_dimensions = np.array([300.0, 864.0, 576.0]) #* 1/20#* 1/10
28+
grid_dimensions = np.array([300.0, 864.0, 576.0]) # * 1/20#* 1/10
3429
grid_levels = np.array([6, 6, 8])
3530

3631
seed = None # 9000
@@ -47,8 +42,7 @@
4742
)
4843

4944

50-
51-
gen_drd = GenerateFluctuationField(
45+
gen_drd = FluctuationFieldGenerator(
5246
friction_velocity,
5347
reference_height,
5448
grid_dimensions,
@@ -59,49 +53,40 @@
5953
)
6054

6155

62-
for nBlocks in range(1, 15+1):
63-
# gen_drd = GenerateFluctuationField(
64-
# friction_velocity,
65-
# reference_height,
66-
# grid_dimensions,
67-
# grid_levels,
68-
# model=Type_Model,
69-
# path_to_parameters=path_to_parameters,
70-
# seed=seed,
71-
# )
56+
def log_law(z, z_0, u_ast):
57+
return u_ast * np.log(z / z_0 + 1.0) / 0.41
7258

73-
fluctuation_field_drd = gen_drd.generate(1)
59+
60+
for nBlocks in range(1, 15 + 1):
61+
fluctuation_field_drd = gen_drd.generate(1, zref, uref, z0, windprofiletype)
7462

7563
sd = np.sqrt(np.mean(fluctuation_field_drd**2))
76-
fluctuation_field_drd = fluctuation_field_drd / sd
64+
fluctuation_field_drd = fluctuation_field_drd / sd
7765
fluctuation_field_drd *= 4.26
7866

79-
log_law = lambda z, z_0, u_ast: u_ast * np.log(z/z_0+1.0)/0.41
80-
z = np.linspace(0.0,grid_dimensions[2], 2**(grid_levels[2])+1)
67+
z = np.linspace(0.0, grid_dimensions[2], 2 ** (grid_levels[2]) + 1)
8168

8269
mean_profile_z = log_law(z, roughness_height, friction_velocity)
8370

8471
mean_profile = np.zeros_like(fluctuation_field_drd)
85-
mean_profile[...,0] = np.tile(mean_profile_z.T, (mean_profile.shape[0], mean_profile.shape[1], 1))
72+
mean_profile[..., 0] = np.tile(mean_profile_z.T, (mean_profile.shape[0], mean_profile.shape[1], 1))
8673

8774
# wind_field = mean_profile
8875
fluctuation_field_drd += mean_profile
8976

90-
fluctuation_field_drd *= 40/63
77+
fluctuation_field_drd *= 40 / 63
9178

92-
wind_field_vtk = tuple([np.copy(fluctuation_field_drd[...,i], order='C') for i in range(3)])
79+
wind_field_vtk = tuple([np.copy(fluctuation_field_drd[..., i], order="C") for i in range(3)])
9380

94-
cellData = {'grid': np.zeros_like(fluctuation_field_drd[...,0]), 'wind': wind_field_vtk}
81+
cellData = {"grid": np.zeros_like(fluctuation_field_drd[..., 0]), "wind": wind_field_vtk}
9582
# .hl import imageToVTK
9683

9784
from pyevtk.hl import imageToVTK
9885

9986
FileName = f"dat/block_{nBlocks}"
10087

101-
imageToVTK(FileName, cellData = cellData, spacing=spacing)
102-
88+
imageToVTK(FileName, cellData=cellData, spacing=spacing)
10389

10490
print(f"generated blocks for {nBlocks}")
10591

106-
107-
print("saved")
92+
print("saved")

docs/source/uml_fluct_gen.rst

+16-16
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
UML Diagram for Fluctuation Field Generation
44
============================================
55

6-
Here is an UML diagram representing the interoperability between several internal classes of the package that comprise the fluctuation generator :py:class:`GenerateFluctuationField`. Please refer to specific class documentations for details. The following diagram is interactive -- try zooming and panning to resize for your convenience.
6+
Here is an UML diagram representing the interoperability between several internal classes of the package that comprise the fluctuation generator :py:class:`FluctuationFieldGenerator`. Please refer to specific class documentations for details. The following diagram is interactive -- try zooming and panning to resize for your convenience.
77

88
These interactive UML diagrams have an issue with rendering the correct arrow types in dark mode, please consider switching to light mode.
99

10-
.. mermaid::
10+
.. mermaid::
1111
:zoom:
1212

1313
classDiagram
1414
direction LR
15-
GenerateFluctuationField ..> VectorGaussianRandomField
15+
FluctuationFieldGenerator ..> VectorGaussianRandomField
1616
GaussianRandomField ..> Covariance
1717

1818
VectorGaussianRandomField --|> GaussianRandomField
@@ -24,16 +24,16 @@ These interactive UML diagrams have an issue with rendering the correct arrow ty
2424
NNCovariance --|> Covariance
2525
end
2626
27-
GenerateFluctuationField : +np.ndarray total_fluctuation
28-
GenerateFluctuationField : +Callable log_law
29-
GenerateFluctuationField : +int seed
30-
GenerateFluctuationField : +Covariance Covariance
31-
GenerateFluctuationField : +VectorGaussianRandomField RF
27+
FluctuationFieldGenerator : +np.ndarray total_fluctuation
28+
FluctuationFieldGenerator : +Callable log_law
29+
FluctuationFieldGenerator : +int seed
30+
FluctuationFieldGenerator : +Covariance Covariance
31+
FluctuationFieldGenerator : +VectorGaussianRandomField RF
3232

33-
GenerateFluctuationField: -_generate_block()
34-
GenerateFluctuationField: +generate()
35-
GenerateFluctuationField: +normalize()
36-
GenerateFluctuationField: +save_to_vtk()
33+
FluctuationFieldGenerator: -_generate_block()
34+
FluctuationFieldGenerator: +generate()
35+
FluctuationFieldGenerator: +normalize()
36+
FluctuationFieldGenerator: +save_to_vtk()
3737
class VectorGaussianRandomField{
3838
+int vdim
3939
+tuple DomainSlice
@@ -53,21 +53,21 @@ These interactive UML diagrams have an issue with rendering the correct arrow ty
5353
+reseed()
5454
}
5555
class VonKarmanCovariance{
56-
+float length_scale
56+
+float length_scale
5757
+float E0
5858
+precompute_Spectrum()
5959
}
6060
class MannCovariance{
61-
+float length_scale
61+
+float length_scale
6262
+float E0
6363
+float Gamma
6464
+precompute_Spectrum()
6565
}
6666
class NNCovariance{
67-
+float length_scale
67+
+float length_scale
6868
+float E0
6969
+float Gamma
7070
+float h_ref
7171
+OnePointSpectra ops
7272
+precompute_Spectrum()
73-
}
73+
}

docs/source/windgeneration.rst

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
.. py:currentmodule:: drdmannturb
22
3-
Fluctuation Field Generation
3+
Fluctuation Field Generation
44
============================
55

6-
DRDMannTurb provides generic methods for generating fluctuation fields from specified spectra models. Please refer to :doc:`the UML diagram <./uml_fluct_gen>` to see the class hierarchy.
6+
DRDMannTurb provides generic methods for generating fluctuation fields from specified spectra models. Please refer to :doc:`the UML diagram <./uml_fluct_gen>` to see the class hierarchy.
77

8-
.. autoclass:: drdmannturb.GenerateFluctuationField
9-
:members:
8+
.. autoclass:: drdmannturb.FluctuationFieldGenerator
9+
:members:
1010

11-
Plotting Utilities
11+
Plotting Utilities
1212
==================
1313

1414
.. currentmodule:: drdmannturb.fluctuation_generation.wind_plot
1515

1616
.. autofunction:: plot_velocity_magnitude
1717

18-
.. autofunction:: plot_velocity_components
18+
.. autofunction:: plot_velocity_components
1919

20-
.. autofunction:: format_wind_field
20+
.. autofunction:: format_wind_field
2121

2222
.. autofunction:: create_grid

drdmannturb/__init__.py

+61-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
"""
2+
DRDMannTurb (short for Deep Rapid Distortion theory Mann Turbulence model) is a data-driven
3+
framework for syntetic turbulence generation in Python. The code is based on the original work
4+
of Jacob Mann in 1994 and 1998 as well as in the deep-learning enhancement developed by Keith
5+
et al. in 2021.
6+
"""
7+
18
from .common import (
29
CPU_Unpickler,
310
Mann_linear_exponential_approx,
@@ -8,8 +15,8 @@
815
from .enums import DataType, EddyLifetimeType, PowerSpectraType
916
from .fluctuation_generation import (
1017
Covariance,
18+
FluctuationFieldGenerator,
1119
GaussianRandomField,
12-
GenerateFluctuationField,
1320
MannCovariance,
1421
NNCovariance,
1522
Sampling_DCT,
@@ -41,3 +48,56 @@
4148
OnePointSpectraDataGenerator,
4249
PowerSpectraRDT,
4350
)
51+
52+
__all__ = [
53+
# Common
54+
"CPU_Unpickler",
55+
"Mann_linear_exponential_approx",
56+
"MannEddyLifetime",
57+
"VKEnergySpectrum",
58+
"plot_loss_logs",
59+
# Enums
60+
"DataType",
61+
"EddyLifetimeType",
62+
"PowerSpectraType",
63+
# Fluctuation Generation
64+
"Covariance",
65+
"FluctuationFieldGenerator",
66+
"GaussianRandomField",
67+
"MannCovariance",
68+
"NNCovariance",
69+
"Sampling_DCT",
70+
"Sampling_DST",
71+
"Sampling_FFT",
72+
"Sampling_FFTW",
73+
"Sampling_method_base",
74+
"Sampling_method_freq",
75+
"Sampling_VF_FFTW",
76+
"VectorGaussianRandomField",
77+
"VonKarmanCovariance",
78+
"create_grid",
79+
"format_wind_field",
80+
"plot_velocity_components",
81+
"plot_velocity_magnitude",
82+
# Interpolation
83+
"extract_x_spectra",
84+
"interp_spectra",
85+
"interpolate",
86+
# NN Modules
87+
"CustomMLP",
88+
"CustomNet",
89+
"Rational",
90+
"SimpleNN",
91+
"TauNet",
92+
# Parameters
93+
"LossParameters",
94+
"NNParameters",
95+
"PhysicalParameters",
96+
"ProblemParameters",
97+
# Spectra Fitting
98+
"CalibrationProblem",
99+
"LossAggregator",
100+
"OnePointSpectra",
101+
"OnePointSpectraDataGenerator",
102+
"PowerSpectraRDT",
103+
]

drdmannturb/fluctuation_generation/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"MannCovariance",
55
"GaussianRandomField",
66
"VectorGaussianRandomField",
7-
"GenerateFluctuationField",
7+
"FluctuationFieldGenerator",
88
"NNCovariance",
99
"Sampling_method_base",
1010
"Sampling_method_freq",
@@ -20,7 +20,7 @@
2020
]
2121

2222
from .covariance_kernels import Covariance, MannCovariance, VonKarmanCovariance
23-
from .fluctuation_field_generator import GenerateFluctuationField
23+
from .fluctuation_field_generator import FluctuationFieldGenerator
2424
from .gaussian_random_fields import GaussianRandomField, VectorGaussianRandomField
2525
from .nn_covariance import NNCovariance
2626
from .sampling_methods import (

drdmannturb/fluctuation_generation/fluctuation_field_generator.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from math import ceil
77
from os import PathLike
88
from pathlib import Path
9-
from typing import Optional, Union
9+
from typing import Callable, Optional, Union
1010

1111
import numpy as np
1212
import torch
@@ -19,7 +19,7 @@
1919
from .nn_covariance import NNCovariance
2020

2121

22-
class GenerateFluctuationField:
22+
class FluctuationFieldGenerator:
2323
r"""
2424
.. _generate-fluctuation-field-reference:
2525
Class for generating a fluctuation field either from a Mann model or a pre-fit DRD model that generates
@@ -82,7 +82,7 @@ def __init__(
8282
energy_spectrum_scale: Optional[float] = None,
8383
lowfreq_params: Optional[LowFreqParameters] = None,
8484
path_to_parameters: Optional[Union[str, PathLike]] = None,
85-
seed: int = None,
85+
seed: Optional[int] = None,
8686
blend_num=10,
8787
):
8888
r"""
@@ -154,9 +154,10 @@ def __init__(
154154
L *= reference_height
155155
Gamma = T
156156

157-
else: # VK or Mann model
158-
if any(p is None for p in [length_scale, time_scale, energy_spectrum_scale]):
159-
raise ValueError("VK/Mann models require length, time and energy spectrum scales")
157+
else: # VK or Mann model case
158+
assert length_scale is not None, "VK/Mann models require length scale" # for type checker
159+
assert time_scale is not None, "VK/Mann models require time scale"
160+
assert energy_spectrum_scale is not None, "VK/Mann models require energy spectrum scale"
160161

161162
E0 = energy_spectrum_scale * friction_velocity**2 * reference_height ** (-2 / 3)
162163
L = length_scale
@@ -235,11 +236,14 @@ def make_slice(start=None, end=None):
235236
self.noise = None
236237
self.total_fluctuation = np.zeros(wind_shape)
237238

238-
self.log_law = lambda z, z0, zref, uref: uref * np.log(z / z0 + 1.0) / np.log(zref / z0)
239-
self.power_law = lambda z, zref, Uref, a: Uref * (z / zref) ** a
239+
CovarianceType = Union[
240+
type[VonKarmanCovariance],
241+
type[MannCovariance],
242+
Callable[..., NNCovariance],
243+
]
240244

241245
### Random field object
242-
covariance_map = {
246+
covariance_map: dict[str, CovarianceType] = { # type: ignore
243247
"VK": VonKarmanCovariance,
244248
"Mann": MannCovariance,
245249
"DRD": lambda **kwargs: NNCovariance(**kwargs, ops=pb.OPS, h_ref=reference_height),
@@ -366,10 +370,12 @@ def _normalize_block(
366370
if windprofiletype == "LOG":
367371
mean_profile_z = self.log_law(z_space, z0, zref, uref)
368372
else:
373+
assert plexp is not None, "Power law exponent (plexp) is required when using power law mean profile."
369374
mean_profile_z = self.power_law(z_space, zref, uref, plexp)
370375

371376
mean_profile = np.zeros_like(curr_block)
372-
mean_profile[..., 0] = np.tile(mean_profile_z.T, (mean_profile.shape[0], mean_profile.shape[1], 1))
377+
# NOTE: (Leaving for now) this *was* mean_profile_z.T, but that's a float? so there's no transpose.
378+
mean_profile[..., 0] = np.tile(mean_profile_z, (mean_profile.shape[0], mean_profile.shape[1], 1))
373379

374380
return curr_block + mean_profile
375381

0 commit comments

Comments
 (0)