From 6b54f1b8d27af20974cfb82273cc1530e6967703 Mon Sep 17 00:00:00 2001 From: gregordecristoforo Date: Thu, 4 Nov 2021 13:55:46 +0100 Subject: [PATCH 1/7] bugfix for periodic_y = True and vy = 0 --- blobmodel/blobs.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/blobmodel/blobs.py b/blobmodel/blobs.py index df1ecb4..937d02b 100644 --- a/blobmodel/blobs.py +++ b/blobmodel/blobs.py @@ -65,20 +65,21 @@ def discretize_blob( origin=(self.pos_x, self.pos_y), x=x, y=y, angle=-self.theta ) if periodic_y: - __x_border = (Ly - self.pos_y) / np.sin(self.theta) + if np.sin(self.theta) == 0: + __x_border = Ly - self.pos_y + __adjusted_Ly = Ly + else: + __x_border = (Ly - self.pos_y) / np.sin(self.theta) + __adjusted_Ly = Ly / np.sin(self.theta) if type(t) == int or type(t) == float: # t has dimensionality = 0, used for testing __number_of_y_propagations = ( - self.__prop_dir_blob_position(t) - + Ly / np.sin(self.theta) - - __x_border - ) // (Ly / np.sin(self.theta)) + self.__prop_dir_blob_position(t) + __adjusted_Ly - __x_border + ) // __adjusted_Ly else: __number_of_y_propagations = ( - self.__prop_dir_blob_position(t)[0, 0] - + Ly / np.sin(self.theta) - - __x_border - ) // (Ly / np.sin(self.theta)) + self.__prop_dir_blob_position(t)[0, 0] + __adjusted_Ly - __x_border + ) // __adjusted_Ly __blob_values = ( self.__single_blob( x_perp, y_perp, t, Ly, periodic_y, __number_of_y_propagations From ca12ba2f0bf1e5c3bcaca52d570024527c7b1c68 Mon Sep 17 00:00:00 2001 From: gregordecristoforo Date: Thu, 4 Nov 2021 14:43:24 +0100 Subject: [PATCH 2/7] added tests or vx=0 and vy=0 --- blobmodel/model.py | 41 ++++++------- tests/test_vx_or_vy=0.py | 127 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+), 23 deletions(-) create mode 100644 tests/test_vx_or_vy=0.py diff --git a/blobmodel/model.py b/blobmodel/model.py index 1868f36..7afc3af 100644 --- a/blobmodel/model.py +++ b/blobmodel/model.py @@ -49,13 +49,7 @@ def __init__( sets distributions of blob parameters """ self.__geometry: Geometry = Geometry( - Nx=Nx, - Ny=Ny, - Lx=Lx, - Ly=Ly, - dt=dt, - T=T, - periodic_y=periodic_y, + Nx=Nx, Ny=Ny, Lx=Lx, Ly=Ly, dt=dt, T=T, periodic_y=periodic_y, ) self.blob_shape: str = blob_shape self.num_blobs: int = num_blobs @@ -111,15 +105,21 @@ def make_realization( # can also be used for gaussian pulses since they converge faster than exponential pulses if speed_up: start = int(b.t_init / self.__geometry.dt) - try: - # ignores t_drain when calculating stop time - stop = start + int( - (-np.log(error * np.sqrt(np.pi)) + self.__geometry.Lx - b.pos_x) - / (b.v_x * self.__geometry.dt) - ) - except: - print("Warning occurs due to v_x == 0") + if b.v_x == 0: stop = self.__geometry.t.size + else: + try: + # ignores t_drain when calculating stop time + stop = start + int( + ( + -np.log(error * np.sqrt(np.pi)) + + self.__geometry.Lx + - b.pos_x + ) + / (b.v_x * self.__geometry.dt) + ) + except: + stop = self.__geometry.t.size output[:, :, start:stop] += b.discretize_blob( x=self.__geometry.x_matrix[:, :, start:stop], y=self.__geometry.y_matrix[:, :, start:stop], @@ -137,20 +137,15 @@ def make_realization( ) if self.__geometry.Ly == 0: ds = xr.Dataset( - data_vars=dict( - n=(["y", "x", "t"], output), - ), + data_vars=dict(n=(["y", "x", "t"], output),), coords=dict( - x=(["x"], self.__geometry.x), - t=(["t"], self.__geometry.t), + x=(["x"], self.__geometry.x), t=(["t"], self.__geometry.t), ), attrs=dict(description="2D propagating blobs."), ) else: ds = xr.Dataset( - data_vars=dict( - n=(["y", "x", "t"], output), - ), + data_vars=dict(n=(["y", "x", "t"], output),), coords=dict( x=(["x"], self.__geometry.x), y=(["y"], self.__geometry.y), diff --git a/tests/test_vx_or_vy=0.py b/tests/test_vx_or_vy=0.py new file mode 100644 index 0000000..13d6e4f --- /dev/null +++ b/tests/test_vx_or_vy=0.py @@ -0,0 +1,127 @@ +from blobmodel import Model, BlobFactory, Blob +import numpy as np +import warnings +import matplotlib.pyplot as plt + + +class CustomBlobFactoryVy0(BlobFactory): + def __init__(self) -> None: + pass + + def sample_blobs( + self, Ly: float, T: float, num_blobs: int, blob_shape: str, t_drain: float + ) -> list[Blob]: + + # set custom parameter distributions + __amp = np.ones(num_blobs) + __width = np.ones(num_blobs) + __vx = np.ones(num_blobs) + __vy = np.zeros(num_blobs) + + __posx = np.zeros(num_blobs) + __posy = np.ones(num_blobs) * Ly / 2 + __t_init = np.ones(num_blobs) * 0 + + # this block must remain the same + __blobs = [] + for i in range(num_blobs): + __blobs.append( + Blob( + id=i, + blob_shape=blob_shape, + amplitude=__amp[i], + width_prop=__width[i], + width_perp=__width[i], + v_x=__vx[i], + v_y=__vy[i], + pos_x=__posx[i], + pos_y=__posy[i], + t_init=__t_init[i], + t_drain=t_drain, + ) + ) + return __blobs + + +class CustomBlobFactoryVx0(BlobFactory): + def __init__(self) -> None: + pass + + def sample_blobs( + self, Ly: float, T: float, num_blobs: int, blob_shape: str, t_drain: float + ) -> list[Blob]: + + # set custom parameter distributions + __amp = np.ones(num_blobs) + __width = np.ones(num_blobs) + __vx = np.zeros(num_blobs) + __vy = np.ones(num_blobs) + + __posx = np.zeros(num_blobs) + __posy = np.ones(num_blobs) * Ly / 2 + __t_init = np.ones(num_blobs) * 0 + + # this block must remain the same + __blobs = [] + for i in range(num_blobs): + __blobs.append( + Blob( + id=i, + blob_shape=blob_shape, + amplitude=__amp[i], + width_prop=__width[i], + width_perp=__width[i], + v_x=__vx[i], + v_y=__vy[i], + pos_x=__posx[i], + pos_y=__posy[i], + t_init=__t_init[i], + t_drain=t_drain, + ) + ) + return __blobs + + +bf = CustomBlobFactoryVy0() + +bm_vy_0 = Model( + Nx=10, + Ny=10, + Lx=10, + Ly=10, + dt=5, + T=10, + periodic_y=True, + blob_shape="exp", + num_blobs=1, + blob_factory=bf, + t_drain=1e10, +) + +bf = CustomBlobFactoryVx0() + +bm_vx_0 = Model( + Nx=10, + Ny=10, + Lx=10, + Ly=10, + dt=5, + T=10, + periodic_y=True, + blob_shape="exp", + num_blobs=1, + blob_factory=bf, + t_drain=1e10, +) + + +def test_vy_0(): + assert bm_vy_0.make_realization(speed_up=True, error=1e-2) + + +def test_vx_0(): + assert bm_vx_0.make_realization(speed_up=True, error=1e-2) + + +test_vx_0() +test_vy_0() From a9f59018b621bf22603e60d12f337f0cf0b14fe0 Mon Sep 17 00:00:00 2001 From: gregordecristoforo Date: Thu, 4 Nov 2021 14:46:33 +0100 Subject: [PATCH 3/7] added black formating --- blobmodel/model.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/blobmodel/model.py b/blobmodel/model.py index 7afc3af..f545797 100644 --- a/blobmodel/model.py +++ b/blobmodel/model.py @@ -49,7 +49,13 @@ def __init__( sets distributions of blob parameters """ self.__geometry: Geometry = Geometry( - Nx=Nx, Ny=Ny, Lx=Lx, Ly=Ly, dt=dt, T=T, periodic_y=periodic_y, + Nx=Nx, + Ny=Ny, + Lx=Lx, + Ly=Ly, + dt=dt, + T=T, + periodic_y=periodic_y, ) self.blob_shape: str = blob_shape self.num_blobs: int = num_blobs @@ -137,15 +143,20 @@ def make_realization( ) if self.__geometry.Ly == 0: ds = xr.Dataset( - data_vars=dict(n=(["y", "x", "t"], output),), + data_vars=dict( + n=(["y", "x", "t"], output), + ), coords=dict( - x=(["x"], self.__geometry.x), t=(["t"], self.__geometry.t), + x=(["x"], self.__geometry.x), + t=(["t"], self.__geometry.t), ), attrs=dict(description="2D propagating blobs."), ) else: ds = xr.Dataset( - data_vars=dict(n=(["y", "x", "t"], output),), + data_vars=dict( + n=(["y", "x", "t"], output), + ), coords=dict( x=(["x"], self.__geometry.x), y=(["y"], self.__geometry.y), From b03c92c9f5093c512469f809e62f6ad9842921d2 Mon Sep 17 00:00:00 2001 From: gregordecristoforo Date: Thu, 4 Nov 2021 14:50:32 +0100 Subject: [PATCH 4/7] resolve type issues raised by mypy --- tests/test_vx_or_vy=0.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/test_vx_or_vy=0.py b/tests/test_vx_or_vy=0.py index 13d6e4f..6f0aab3 100644 --- a/tests/test_vx_or_vy=0.py +++ b/tests/test_vx_or_vy=0.py @@ -1,7 +1,5 @@ from blobmodel import Model, BlobFactory, Blob import numpy as np -import warnings -import matplotlib.pyplot as plt class CustomBlobFactoryVy0(BlobFactory): @@ -82,7 +80,7 @@ def sample_blobs( return __blobs -bf = CustomBlobFactoryVy0() +bf_vy_0 = CustomBlobFactoryVy0() bm_vy_0 = Model( Nx=10, @@ -94,11 +92,11 @@ def sample_blobs( periodic_y=True, blob_shape="exp", num_blobs=1, - blob_factory=bf, + blob_factory=bf_vy_0, t_drain=1e10, ) -bf = CustomBlobFactoryVx0() +bf_vx_0 = CustomBlobFactoryVx0() bm_vx_0 = Model( Nx=10, @@ -110,7 +108,7 @@ def sample_blobs( periodic_y=True, blob_shape="exp", num_blobs=1, - blob_factory=bf, + blob_factory=bf_vx_0, t_drain=1e10, ) From 36819ff0437a5c275d36f9a21fd46ad6a7d7764a Mon Sep 17 00:00:00 2001 From: gregordecristoforo Date: Thu, 4 Nov 2021 14:56:13 +0100 Subject: [PATCH 5/7] tests covering additional lines --- blobmodel/model.py | 19 ++++--------------- tests/test_vx_or_vy=0.py | 8 ++++---- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/blobmodel/model.py b/blobmodel/model.py index f545797..7afc3af 100644 --- a/blobmodel/model.py +++ b/blobmodel/model.py @@ -49,13 +49,7 @@ def __init__( sets distributions of blob parameters """ self.__geometry: Geometry = Geometry( - Nx=Nx, - Ny=Ny, - Lx=Lx, - Ly=Ly, - dt=dt, - T=T, - periodic_y=periodic_y, + Nx=Nx, Ny=Ny, Lx=Lx, Ly=Ly, dt=dt, T=T, periodic_y=periodic_y, ) self.blob_shape: str = blob_shape self.num_blobs: int = num_blobs @@ -143,20 +137,15 @@ def make_realization( ) if self.__geometry.Ly == 0: ds = xr.Dataset( - data_vars=dict( - n=(["y", "x", "t"], output), - ), + data_vars=dict(n=(["y", "x", "t"], output),), coords=dict( - x=(["x"], self.__geometry.x), - t=(["t"], self.__geometry.t), + x=(["x"], self.__geometry.x), t=(["t"], self.__geometry.t), ), attrs=dict(description="2D propagating blobs."), ) else: ds = xr.Dataset( - data_vars=dict( - n=(["y", "x", "t"], output), - ), + data_vars=dict(n=(["y", "x", "t"], output),), coords=dict( x=(["x"], self.__geometry.x), y=(["y"], self.__geometry.y), diff --git a/tests/test_vx_or_vy=0.py b/tests/test_vx_or_vy=0.py index 6f0aab3..220aa7c 100644 --- a/tests/test_vx_or_vy=0.py +++ b/tests/test_vx_or_vy=0.py @@ -87,8 +87,8 @@ def sample_blobs( Ny=10, Lx=10, Ly=10, - dt=5, - T=10, + dt=1, + T=1, periodic_y=True, blob_shape="exp", num_blobs=1, @@ -103,8 +103,8 @@ def sample_blobs( Ny=10, Lx=10, Ly=10, - dt=5, - T=10, + dt=1, + T=1, periodic_y=True, blob_shape="exp", num_blobs=1, From 41081fc90aa8d007d2ffbf082a9beae3bb120c49 Mon Sep 17 00:00:00 2001 From: gregordecristoforo Date: Thu, 4 Nov 2021 14:57:22 +0100 Subject: [PATCH 6/7] forgot black formatting --- blobmodel/model.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/blobmodel/model.py b/blobmodel/model.py index 7afc3af..f545797 100644 --- a/blobmodel/model.py +++ b/blobmodel/model.py @@ -49,7 +49,13 @@ def __init__( sets distributions of blob parameters """ self.__geometry: Geometry = Geometry( - Nx=Nx, Ny=Ny, Lx=Lx, Ly=Ly, dt=dt, T=T, periodic_y=periodic_y, + Nx=Nx, + Ny=Ny, + Lx=Lx, + Ly=Ly, + dt=dt, + T=T, + periodic_y=periodic_y, ) self.blob_shape: str = blob_shape self.num_blobs: int = num_blobs @@ -137,15 +143,20 @@ def make_realization( ) if self.__geometry.Ly == 0: ds = xr.Dataset( - data_vars=dict(n=(["y", "x", "t"], output),), + data_vars=dict( + n=(["y", "x", "t"], output), + ), coords=dict( - x=(["x"], self.__geometry.x), t=(["t"], self.__geometry.t), + x=(["x"], self.__geometry.x), + t=(["t"], self.__geometry.t), ), attrs=dict(description="2D propagating blobs."), ) else: ds = xr.Dataset( - data_vars=dict(n=(["y", "x", "t"], output),), + data_vars=dict( + n=(["y", "x", "t"], output), + ), coords=dict( x=(["x"], self.__geometry.x), y=(["y"], self.__geometry.y), From 865c96d537f71df50de9348660269b1abe998c7e Mon Sep 17 00:00:00 2001 From: gregordecristoforo Date: Thu, 4 Nov 2021 15:03:34 +0100 Subject: [PATCH 7/7] remove unnecessary try except lines --- blobmodel/model.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/blobmodel/model.py b/blobmodel/model.py index f545797..279c4fb 100644 --- a/blobmodel/model.py +++ b/blobmodel/model.py @@ -114,18 +114,11 @@ def make_realization( if b.v_x == 0: stop = self.__geometry.t.size else: - try: - # ignores t_drain when calculating stop time - stop = start + int( - ( - -np.log(error * np.sqrt(np.pi)) - + self.__geometry.Lx - - b.pos_x - ) - / (b.v_x * self.__geometry.dt) - ) - except: - stop = self.__geometry.t.size + # ignores t_drain when calculating stop time + stop = start + int( + (-np.log(error * np.sqrt(np.pi)) + self.__geometry.Lx - b.pos_x) + / (b.v_x * self.__geometry.dt) + ) output[:, :, start:stop] += b.discretize_blob( x=self.__geometry.x_matrix[:, :, start:stop], y=self.__geometry.y_matrix[:, :, start:stop],