Skip to content

Commit 96478b9

Browse files
authored
Merge pull request #302 from GeoStat-Framework/fourier-gen
Add a Fourier generator for periodic spatial random fields
2 parents 695ed38 + c04c924 commit 96478b9

File tree

10 files changed

+598
-9
lines changed

10 files changed

+598
-9
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ jobs:
4848
run: |
4949
python -m pylint src/gstools/
5050
51-
- name: cython-lint check
52-
run: |
53-
cython-lint src/gstools/
51+
#- name: cython-lint check
52+
#run: |
53+
#cython-lint src/gstools/
5454

5555
build_wheels:
5656
name: wheels for ${{ matrix.os }}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<img align="right" width="450" src="https://raw.githubusercontent.com/GeoStat-Framework/GSTools/main/docs/source/pics/demonstrator.png" alt="">
3535

3636
GeoStatTools provides geostatistical tools for various purposes:
37-
- random field generation
37+
- random field generation, including periodic boundaries
3838
- simple, ordinary, universal and external drift kriging
3939
- conditioned field generation
4040
- incompressible random vector field generation

docs/source/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Purpose
2424

2525
GeoStatTools provides geostatistical tools for various purposes:
2626

27-
- random field generation
27+
- random field generation, including periodic boundaries
2828
- simple, ordinary, universal and external drift kriging
2929
- conditioned field generation
3030
- incompressible random vector field generation
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Generating a Simple Periodic Random Field
3+
-----------------------------------------
4+
5+
In this simple example we are going to learn how to generate periodic spatial
6+
random fields. The Fourier method comes naturally with the property of
7+
periodicity, so we'll use it to create the random field.
8+
"""
9+
10+
import numpy as np
11+
12+
import gstools as gs
13+
14+
# We start off by defining the spatial grid. For the sake of simplicity, we
15+
# use a square domain. We set the optional argument `endpoint` to `False`, to
16+
# not make the domain in each dimension one grid cell larger than the
17+
# periodicity.
18+
L = 500.0
19+
x = np.linspace(0, L, 256, endpoint=False)
20+
y = np.linspace(0, L, 128, endpoint=False)
21+
22+
# Now, we create a Gaussian covariance model with a correlation length which is
23+
# roughly half the size of the grid.
24+
model = gs.Gaussian(dim=2, var=1, len_scale=200)
25+
26+
# Next, we hand the cov. model to the spatial random field class `SRF`
27+
# and set the generator to `"Fourier"`. The argument `period` is set to the
28+
# domain size. If only a single number is given, the same periodicity is
29+
# applied in each dimension, as shown in this example. The `mode_no` argument
30+
# sets the number of Fourier modes. If only an integer is given, that number
31+
# of modes is used for all dimensions.
32+
srf = gs.SRF(
33+
model,
34+
generator="Fourier",
35+
period=L,
36+
mode_no=32,
37+
seed=1681903,
38+
)
39+
40+
# Now, we can calculate the field with the given parameters.
41+
srf((x, y), mesh_type="structured")
42+
43+
# GSTools has a few simple visualization methods built in.
44+
srf.plot()
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
Generating a Transformed Periodic Random Field
3+
----------------------------------------------
4+
5+
Building on the precious example, we are now going to generate periodic
6+
spatial random fields with a transformation applied, resulting in a level set.
7+
"""
8+
9+
import numpy as np
10+
11+
import gstools as gs
12+
13+
# We start off by defining the spatial grid. As in the previous example, we do
14+
# not want to include the endpoints.
15+
L = np.array((500, 400))
16+
x = np.linspace(0, L[0], 300, endpoint=False)
17+
y = np.linspace(0, L[1], 200, endpoint=False)
18+
19+
# Instead of using a Gaussian covariance model, we will use the much rougher
20+
# exponential model and we will introduce an anisotropy by using two different
21+
# length scales in the x- and y-directions
22+
model = gs.Exponential(dim=2, var=2, len_scale=[80, 20])
23+
24+
# Same as before, we set up the spatial random field. But this time, we will
25+
# use a periodicity which is equal to the domain size in x-direction, but
26+
# half the domain size in y-direction. And we will use different `mode_no` for
27+
# the different dimensions.
28+
srf = gs.SRF(
29+
model,
30+
generator="Fourier",
31+
period=[L[0], L[1] / 2],
32+
mode_no=[30, 20],
33+
seed=1681903,
34+
)
35+
# and compute it on our spatial domain
36+
srf((x, y), mesh_type="structured")
37+
38+
# With the field generated, we can now apply transformations starting with a
39+
# discretization of the field into 4 different values
40+
thresholds = np.linspace(np.min(srf.field), np.max(srf.field), 4)
41+
srf.transform("discrete", store="transform_discrete", values=thresholds)
42+
srf.plot("transform_discrete")
43+
44+
# This is already a nice result, but we want to pronounce the peaks of the
45+
# field. We can do this by applying a log-normal transformation on top
46+
srf.transform(
47+
"lognormal", field="transform_discrete", store="transform_lognormal"
48+
)
49+
srf.plot("transform_lognormal")

examples/01_random_field/README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ semi-variogram. This is done by using the so-called randomization method.
1111
The spatial random field is represented by a stochastic Fourier integral
1212
and its discretised modes are evaluated at random frequencies.
1313

14+
In case you want to generate spatial random fields with periodic boundaries,
15+
you can use the so-called Fourier method. See the corresponding examples for
16+
how to do that. The spatial random field is represented by a stochastic
17+
Fourier integral and its discretised modes are evaluated at equidistant
18+
frequencies.
19+
1420
GSTools supports arbitrary and non-isotropic covariance models.
1521

1622
Examples

0 commit comments

Comments
 (0)