-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2d shape #54
2d shape #54
Changes from 14 commits
ff57a76
5825d42
85a9d07
73c376b
8e9ccaf
f5acce1
e00589a
93b6b76
aa5d558
f681ab2
da863a2
2fdb990
e85ae6b
3a68e3b
670bf79
fe1da46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ jobs: | |
|
||
pytest: | ||
|
||
runs-on: ubuntu-latest | ||
runs-on: ubuntu-20.04 | ||
if: always() | ||
strategy: | ||
matrix: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
from .geometry import Geometry | ||
from nptyping import NDArray | ||
import warnings | ||
from .pulse_shape import AbstractBlobShape, BlobShapeImpl | ||
|
||
|
||
class Model: | ||
|
@@ -21,7 +22,7 @@ def __init__( | |
dt: float = 0.1, | ||
T: float = 10, | ||
periodic_y: bool = False, | ||
blob_shape: str = "gauss", | ||
blob_shape: Union[AbstractBlobShape, str] = BlobShapeImpl("gauss"), | ||
gregordecristoforo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
num_blobs: int = 1000, | ||
t_drain: Union[float, NDArray] = 10, | ||
blob_factory: BlobFactory = DefaultBlobFactory(), | ||
|
@@ -81,7 +82,9 @@ def __init__( | |
T=T, | ||
periodic_y=periodic_y, | ||
) | ||
self.blob_shape: str = blob_shape | ||
self.blob_shape = ( | ||
gregordecristoforo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
BlobShapeImpl(blob_shape) if isinstance(blob_shape, str) else blob_shape | ||
) | ||
self.num_blobs: int = num_blobs | ||
self.t_drain: Union[float, NDArray] = t_drain | ||
|
||
|
@@ -98,7 +101,7 @@ def __init__( | |
def __str__(self) -> str: | ||
"""string representation of Model.""" | ||
return ( | ||
f"2d Blob Model with blob shape:{self.blob_shape}," | ||
f"2d Blob Model with" | ||
+ f" num_blobs:{self.num_blobs} and t_drain:{self.t_drain}" | ||
) | ||
|
||
|
@@ -219,28 +222,33 @@ def _sum_up_blobs( | |
|
||
def _compute_start_stop(self, blob: Blob, speed_up: bool, error: float): | ||
if speed_up: | ||
_start = int(blob.t_init / self._geometry.dt) | ||
if blob.v_x == 0: | ||
_stop = self._geometry.t.size | ||
else: | ||
# ignores t_drain when calculating stop time | ||
_stop = np.minimum( | ||
self._geometry.t.size, | ||
_start | ||
+ int( | ||
( | ||
-np.log(error * np.sqrt(np.pi)) | ||
+ self._geometry.Lx | ||
- blob.pos_x | ||
) | ||
/ (blob.v_x * self._geometry.dt) | ||
), | ||
start = 0 | ||
stop = self._geometry.t.size | ||
return start, stop | ||
start = int( | ||
( | ||
blob.t_init * blob.v_x | ||
+ blob.width_prop * np.log(error * np.sqrt(np.pi)) | ||
+ blob.pos_x | ||
) | ||
else: | ||
_start = 0 | ||
_stop = self._geometry.t.size | ||
|
||
return _start, _stop | ||
/ (self._geometry.dt * blob.v_x) | ||
) | ||
# ignores t_drain when calculating stop time | ||
stop = np.minimum( | ||
self._geometry.t.size, | ||
start | ||
+ int( | ||
( | ||
-blob.width_prop * np.log(error * np.sqrt(np.pi)) | ||
+ self._geometry.Lx | ||
- blob.pos_x | ||
) | ||
/ (blob.v_x * self._geometry.dt) | ||
), | ||
) | ||
return start, stop | ||
return 0, self._geometry.t.size | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is equivalent to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, good one! |
||
|
||
def _reset_fields(self): | ||
self._density = np.zeros( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from abc import ABC, abstractmethod | ||
import numpy as np | ||
from nptyping import NDArray | ||
|
||
|
||
class AbstractBlobShape(ABC): | ||
"""Abstract class containing the blob pulse shapes. Two-dimensional blob | ||
pulse shapes are written in the form. | ||
|
||
phi(theta_x, theta_y) = phi_x(theta_x) * phi_y(theta_y). | ||
""" | ||
|
||
@abstractmethod | ||
def get_pulse_shape_prop(self, theta_prop: NDArray, kwargs): | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def get_pulse_shape_perp(self, theta_perp: NDArray, kwargs): | ||
raise NotImplementedError | ||
|
||
|
||
class BlobShapeImpl(AbstractBlobShape): | ||
"""Implementation of the AbstractPulseShape class.""" | ||
|
||
__SHAPE_NAMES__ = {"exp", "gauss", "2-exp", "lorentz", "secant"} | ||
|
||
def __init__(self, pulse_shape_prop="gauss", pulse_shape_perp="gauss"): | ||
self.__GENERATORS__ = { | ||
gregordecristoforo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"exp": BlobShapeImpl._get_exponential_shape, | ||
"gauss": BlobShapeImpl._get_gaussian_shape, | ||
"2-exp": BlobShapeImpl._get_double_exponential_shape, | ||
"lorentz": BlobShapeImpl._get_lorentz_shape, | ||
"secant": BlobShapeImpl._get_secant_shape, | ||
} | ||
self.pulse_shape_prop = pulse_shape_prop | ||
self.pulse_shape_perp = pulse_shape_perp | ||
if ( | ||
pulse_shape_prop not in BlobShapeImpl.__SHAPE_NAMES__ | ||
or pulse_shape_perp not in BlobShapeImpl.__SHAPE_NAMES__ | ||
): | ||
raise NotImplementedError( | ||
f"{self.__class__.__name__}.blob_shape not implemented" | ||
) | ||
|
||
def get_pulse_shape_prop(self, theta: np.ndarray, kwargs) -> np.ndarray: | ||
return self.__GENERATORS__.get(self.pulse_shape_prop)(theta, kwargs) | ||
|
||
def get_pulse_shape_perp(self, theta: np.ndarray, kwargs) -> np.ndarray: | ||
return self.__GENERATORS__.get(self.pulse_shape_perp)(theta, kwargs) | ||
|
||
@staticmethod | ||
def _get_exponential_shape(theta: np.ndarray, kwargs) -> np.ndarray: | ||
return np.exp(theta) * np.heaviside(-1.0 * theta, 1) | ||
|
||
@staticmethod | ||
def _get_lorentz_shape(theta: np.ndarray, kwargs) -> np.ndarray: | ||
return 1 / (np.pi * (1 + theta**2)) | ||
|
||
@staticmethod | ||
def _get_double_exponential_shape(theta: np.ndarray, kwargs) -> np.ndarray: | ||
lam = kwargs["lam"] | ||
gregordecristoforo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert (lam > 0.0) & (lam < 1.0) | ||
kern = np.zeros(len(theta)) | ||
kern[theta < 0] = np.exp(theta[theta < 0] / lam) | ||
kern[theta >= 0] = np.exp(-theta[theta >= 0] / (1 - lam)) | ||
return kern | ||
|
||
@staticmethod | ||
def _get_gaussian_shape(theta: np.ndarray, kwargs) -> np.ndarray: | ||
return 1 / np.sqrt(np.pi) * np.exp(-(theta**2)) | ||
|
||
@staticmethod | ||
def _get_secant_shape(theta: np.ndarray, kwargs) -> np.ndarray: | ||
return 2 / np.pi / (np.exp(theta) + np.exp(-theta)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... serves as a placeholder for
kwargs
right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep