Skip to content

Commit 53f0fa1

Browse files
authored
Merge pull request #115 from uit-cosmo/examples
Examples
2 parents 7dc4bbf + c467e47 commit 53f0fa1

14 files changed

+62
-130
lines changed

blobmodel/model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,9 @@ def _compute_start_stop(self, blob: Blob, speed_up: bool, error: float):
339339
# ignores t_drain when calculating stop time
340340
stop = np.minimum(
341341
self._geometry.t.size,
342-
start
343-
+ int(
344-
(
342+
int(
343+
blob.t_init
344+
+ (
345345
-blob.width_prop * np.log(error * np.sqrt(np.pi))
346346
+ self._geometry.Lx
347347
- blob.pos_x

blobmodel/stochasticality.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,4 @@ def is_one_dimensional(self) -> bool:
245245
- Perpendicular width parameters are irrelevant since perp shape should be ignored by the Bolb class.
246246
247247
"""
248-
return self.velocity_y_dist == "zeros"
248+
return self.velocity_y_dist == DistributionEnum.zeros

examples/1d_animation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
BlobShapeEnum,
77
)
88

9+
# Example of a one-dimensional realization and animation.
10+
911
bf = DefaultBlobFactory()
1012

1113
bm = Model(

examples/2_sided_exp_pulse.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
BlobShapeEnum,
77
)
88
import matplotlib.pyplot as plt
9-
import numpy as np
9+
10+
# Example of use of extra blob shape parameters with a double exponential blob shape.
1011

1112
bf = DefaultBlobFactory(
1213
shape_param_x_parameter=0.5,

examples/2d_animation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from blobmodel import Model, show_model, BlobShapeImpl, BlobShapeEnum
1+
from blobmodel import Model, show_model
22

3-
# here you can define your custom parameter distributions
3+
# Example of a simple two-dimensional realization and animation.
44

55
bm = Model(
66
Nx=100,
@@ -15,7 +15,7 @@
1515
t_init=10,
1616
)
1717

18-
# create data
18+
# Make a realization and store it in a DataSet object
1919
ds = bm.make_realization(speed_up=True, error=1e-2)
2020
# show animation and save as gif
2121
show_model(dataset=ds, interval=100, gif_name="2d_animation.gif", fps=10)

examples/analyse_output.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import matplotlib.pyplot as plt
44
import os
55

6+
# Example reading data from a file stored in "example.nc" and plotting the time mean using xarray functions.
7+
68

79
if not os.path.isfile("./example.nc"):
810
bm = Model(

examples/blob_tilting.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
)
99
import numpy as np
1010

11-
# To have something to compare we will first make a simulation with aligned blobs moving with velocities
11+
# Example with tilted blobs, to have something to compare we will first make a simulation with
12+
# aligned blobs moving with velocities
1213

1314
vx = 1
1415
vy = 1

examples/changing_t_drain.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import matplotlib.pyplot as plt
99
import numpy as np
1010

11+
# Example of use of a spatially varying t_drain parameter. The results are compared with the case of constant
12+
# t_drain.
13+
1114
bf = DefaultBlobFactory(A_dist=DistributionEnum.deg, vy_dist=DistributionEnum.zeros)
1215

1316
t_drain = np.linspace(2, 1, 100)
@@ -29,7 +32,7 @@
2932
ds = tmp.make_realization(file_name="profile_comparison.nc", speed_up=True, error=1e-2)
3033

3134

32-
def plot_cahnging_t_drain(ds):
35+
def plot_changing_t_drain(ds):
3336
x = np.linspace(0, 10, 100)
3437
t_p = 1
3538
t_w = 1 / 10
@@ -49,4 +52,4 @@ def plot_cahnging_t_drain(ds):
4952
plt.show()
5053

5154

52-
plot_cahnging_t_drain(ds)
55+
plot_changing_t_drain(ds)

examples/compare_to_analytical_sol.py

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
import matplotlib.pyplot as plt
99
import numpy as np
1010

11+
# The time average of a one-dimensional model realization with constant horizontal velocities is compared
12+
# with analytically derived results. See O. E. Garcia, et al.; Phys. Plasmas 1 May 2016; 23 (5): 052308. https://doi.org/10.1063/1.4951016
13+
1114
# use DefaultBlobFactory to define distribution functions fo random variables
1215
bf = DefaultBlobFactory(A_dist=DistributionEnum.deg, vy_dist=DistributionEnum.zeros)
1316

1417
tmp = Model(
15-
Nx=100,
18+
Nx=20,
1619
Ny=1,
1720
Lx=10,
1821
Ly=0,
@@ -23,29 +26,24 @@
2326
periodic_y=False,
2427
num_blobs=10000,
2528
blob_factory=bf,
29+
one_dimensional=True,
2630
)
2731

28-
ds = tmp.make_realization(file_name="profile_comparison.nc", speed_up=False, error=1e-4)
29-
30-
31-
def plot_convergence_to_analytical_solution(ds):
32-
x = np.linspace(0, 10, 100)
33-
t_p = 1
34-
t_w = 1 / 10
35-
amp = 1
36-
v_p = 1.0
37-
t_loss = 2.0
38-
t_d = t_loss * t_p / (t_loss + t_p)
39-
40-
analytical_profile = (
41-
1 / np.sqrt(np.pi) * t_d / t_w * amp * np.exp(-x / (v_p * t_loss))
42-
)
43-
44-
ds.n.isel(y=0).mean(dim=("t")).plot(label="blob_model")
45-
plt.yscale("log")
46-
plt.plot(x, analytical_profile, label="analytical solution")
47-
plt.legend()
48-
plt.show()
49-
50-
51-
plot_convergence_to_analytical_solution(ds)
32+
ds = tmp.make_realization(speed_up=True, error=1e-10)
33+
x = np.linspace(0, 10, 20)
34+
t_p = 1
35+
t_w = 1 / 10
36+
amp = 1
37+
v_p = 1.0
38+
t_loss = 2.0
39+
t_d = t_loss * t_p / (t_loss + t_p)
40+
41+
analytical_profile = t_d / t_w * amp * np.exp(-x / (v_p * t_loss))
42+
43+
ds.n.isel(y=0).mean(dim=("t")).plot(label="blob_model")
44+
plt.yscale("log")
45+
plt.xlabel("x")
46+
plt.ylabel("Time average")
47+
plt.plot(x, analytical_profile, label="analytical solution")
48+
plt.legend()
49+
plt.show()

examples/custom_blobfactory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
Blob,
55
show_model,
66
AbstractBlobShape,
7-
BlobShapeImpl,
8-
BlobShapeEnum,
97
)
108
import numpy as np
119

10+
# Example of implementing a BlobFactory, which allows to set blob parameters in any desirable way.
11+
1212

1313
# create custom class that inherits from BlobFactory
1414
# here you can define your custom parameter distributions

0 commit comments

Comments
 (0)