Skip to content
Snippets Groups Projects
Commit 0ecbed3b authored by Laura Christine Kühle's avatar Laura Christine Kühle
Browse files

Added documentation to 'InitialCondition'.

parent 3ae92fc1
No related branches found
No related tags found
No related merge requests found
......@@ -2,33 +2,109 @@
"""
@author: Laura C. Kühle
TODO: Add is_smooth() to DiscontinuousConstant class
"""
import numpy as np
class InitialCondition(object):
"""Class for initial condition function.
Attributes
----------
interval_len : float
Length of the interval between left and right boundary.
Methods
-------
get_name()
Returns string of class name.
is_smooth()
Returns flag whether function is smooth.
induce_adjustment(value)
Adjusts x-value of function.
randomize(config)
Sets all instance variables to random value if not determined otherwise.
calculate(x)
Evaluates function at given x-value.
"""
def __init__(self, left_bound, right_bound, config):
"""Initializes InitialCondition.
Parameters
----------
left_bound : float
Left boundary of interval.
right_bound : float
Right boundary of interval.
config : dict
Additional parameters for initial condition.
"""
self._left_bound = left_bound
self._right_bound = right_bound
self._reset(config)
def _reset(self, config):
"""Resets instance variables.
Parameters
----------
config : dict
Additional parameters for initial condition.
"""
self._interval_len = self._right_bound-self._left_bound
def get_name(self):
"""Returns string of class name."""
return self.__class__.__name__
def is_smooth(self):
"""Returns flag that function is smooth."""
return True
def induce_adjustment(self, value):
"""Adjusts x-value of function.
Parameters
----------
value : float
Value of adjustment on x-axis in right direction.
"""
pass
def randomize(self, config):
"""Sets all instance variables to random value if not determined otherwise.
Parameters
----------
config : dict
Fixed parameters for initial condition.
"""
pass
def calculate(self, x):
"""Evaluates function at given x-value.
Projects x-value into interval of the periodic function and evaluates the function.
Parameters
----------
x : float
Evaluation point of function.
Returns
-------
float
Value of function evaluates at x-value.
"""
while x < self._left_bound:
x = x + self._interval_len
while x > self._right_bound:
......@@ -36,27 +112,103 @@ class InitialCondition(object):
return self._get_point(x)
def _get_point(self, x):
"""Evaluates function at given x-value.
Parameters
----------
x : float
Evaluation point of function.
Returns
-------
float
Value of function evaluates at x-value.
"""
pass
class Sine(InitialCondition):
"""Class for the sine wave.
Attributes
----------
factor : float
Factor by which is the evaluation point is multiplied before applying sine.
Methods
-------
randomize(config)
Sets all instance variables to random value if not determined otherwise.
"""
def _reset(self, config):
"""Resets instance variables.
Parameters
----------
config : dict
Additional parameters for initial condition.
"""
super()._reset(config)
# Unpack necessary configurations
self._factor = config.pop('factor', 2)
def randomize(self, config):
"""Sets all instance variables to random value if not determined otherwise.
Parameters
----------
config : dict
Fixed parameters for initial condition.
"""
factor = config.pop('factor', np.random.uniform(low=-100, high=100))
config = {'factor': factor}
self._reset(config)
def _get_point(self, x):
"""Evaluates function at given x-value.
Parameters
----------
x : float
Evaluation point of function.
Returns
-------
float
Value of function evaluates at x-value.
"""
return np.sin(self._factor * np.pi * x)
class Box(InitialCondition):
"""Class for a continuous function with two jumps (forming a box).
Methods
-------
is_smooth()
Returns flag whether function is smooth.
"""
def _get_point(self, x):
"""Evaluates function at given x-value.
Parameters
----------
x : float
Evaluation point of function.
Returns
-------
float
Value of function evaluates at x-value.
"""
if x < -1:
x = x + 2
if x > 1:
......@@ -67,11 +219,44 @@ class Box(InitialCondition):
return 0
def is_smooth(self):
"""Returns flag that function is not smooth."""
return False
class FourPeakWave(InitialCondition):
"""Class for a function with four peaks.
The function is defined piece-by-piece and consists of a Gaussian function, a box function,
a symmetric absolute function, and an elliptic function.
Attributes
----------
alpha : float
Factor used for the elliptic function..
delta : float
Value used to adjust z for the Gaussian function.
beta : float
Factor used for the Gaussian function.
a : float
Value for x-value adjustment for elliptic function.
z : float
Value for x-value adjustment for Gaussian function.
Methods
-------
is_smooth()
Returns flag whether function is smooth.
"""
def _reset(self, config):
"""Resets instance variables.
Parameters
----------
config : dict
Additional parameters for initial condition.
"""
super()._reset(config)
# Set additional necessary parameter
......@@ -82,9 +267,23 @@ class FourPeakWave(InitialCondition):
self._z = -0.7
def is_smooth(self):
"""Returns flag that function is not smooth."""
return False
def _get_point(self, x):
"""Evaluates function at given x-value.
Parameters
----------
x : float
Evaluation point of function.
Returns
-------
float
Value of function evaluates at x-value.
"""
if (x >= -0.8) & (x <= -0.6):
return 1/6 * (self._gaussian_function(x, self._z-self._delta)
+ self._gaussian_function(x, self._z+self._delta)
......@@ -100,49 +299,200 @@ class FourPeakWave(InitialCondition):
return 0
def _gaussian_function(self, x, z):
"""Evaluates Gaussian function.
Parameters
----------
x : float
Evaluation point of function.
z : float
Value for x-value adjustment.
Returns
-------
float
Value of function evaluates at x-value.
"""
return np.exp(-self._beta * (x-z)**2)
def _elliptic_function(self, x, a):
"""Evaluates elliptic function.
Parameters
----------
x : float
Evaluation point of function.
a : float
Value for x-value adjustment.
Returns
-------
float
Value of function evaluates at x-value.
"""
return np.sqrt(max(1 - self._alpha**2 * (x-a)**2, 0))
class Linear(InitialCondition):
"""Class for the linear function.
Attributes
----------
factor : float
Factor by which is the evaluation point is multiplied.
Methods
-------
randomize(config)
Sets all instance variables to random value if not determined otherwise.
"""
def _reset(self, config):
"""Resets instance variables.
Parameters
----------
config : dict
Additional parameters for initial condition.
"""
def _reset(self, config):
"""Resets instance variables.
Parameters
----------
config : dict
Additional parameters for initial condition.
"""
super()._reset(config)
# Unpack necessary configurations
self._factor = config.pop('factor', 1)
def randomize(self, config):
"""Sets all instance variables to random value if not determined otherwise.
Parameters
----------
config : dict
Fixed parameters for initial condition.
"""
factor = config.pop('factor', np.random.uniform(low=-100, high=100))
config = {'factor': factor}
self._reset(config)
def _get_point(self, x):
"""Evaluates function at given x-value.
Parameters
----------
x : float
Evaluation point of function.
Returns
-------
float
Value of function evaluates at x-value.
"""
return self._factor * x
class LinearAbsolut(InitialCondition):
"""Class for the absolute values of the linear function.
Attributes
----------
factor : float
Factor by which is the evaluation point is multiplied.
Methods
-------
is_smooth()
Returns flag whether function is smooth.
randomize(config)
Sets all instance variables to random value if not determined otherwise.
"""
def _reset(self, config):
"""Resets instance variables.
Parameters
----------
config : dict
Additional parameters for initial condition.
"""
super()._reset(config)
# Unpack necessary configurations
self._factor = config.pop('factor', 1)
def is_smooth(self):
"""Returns flag that function is not smooth."""
return False
def randomize(self, config):
"""Sets all instance variables to random value if not determined otherwise.
Parameters
----------
config : dict
Fixed parameters for initial condition.
"""
factor = config.pop('factor', np.random.uniform(low=-100, high=100))
config = {'factor': factor}
self._reset(config)
def _get_point(self, x):
"""Evaluates function at given x-value.
Parameters
----------
x : float
Evaluation point of function.
Returns
-------
float
Value of function evaluates at x-value.
"""
return self._factor * abs(x)
class DiscontinuousConstant(InitialCondition):
"""Class for the otherwise continuous function with one jump.
Attributes
----------
x0 : float
X-value where jump is induced.
left_factor : float
Factor by which is the evaluation point is multiplied before the jump.
right_factor : float
Factor by which is the evaluation point is multiplied after the jump.
Methods
-------
is_smooth()
Returns flag whether function is smooth.
"""
def _reset(self, config):
"""Resets instance variables.
Parameters
----------
config : dict
Additional parameters for initial condition.
"""
super()._reset(config)
# Unpack necessary configurations
......@@ -151,11 +501,47 @@ class DiscontinuousConstant(InitialCondition):
self._right_factor = config.pop('right_factor', 0.5)
def _get_point(self, x):
"""Evaluates function at given x-value.
Parameters
----------
x : float
Evaluation point of function.
Returns
-------
float
Value of function evaluates at x-value.
"""
return self._left_factor * (x <= self._x0) + self._right_factor * (x > self._x0)
class Polynomial(InitialCondition):
"""Class for the polynomial function.
Attributes
----------
factor : float
Factor by which is the evaluation point is multiplied.
exponential : int
Degree of the polynomial.
Methods
-------
randomize(config)
Sets all instance variables to random value if not determined otherwise.
"""
def _reset(self, config):
"""Resets instance variables.
Parameters
----------
config : dict
Additional parameters for initial condition.
"""
super()._reset(config)
# Unpack necessary configurations
......@@ -163,52 +549,189 @@ class Polynomial(InitialCondition):
self._exponential = config.pop('exponential', 2)
def randomize(self, config):
"""Sets all instance variables to random value if not determined otherwise.
Parameters
----------
config : dict
Fixed parameters for initial condition.
"""
factor = config.pop('factor', np.random.uniform(low=-100, high=100))
exponential = config.pop('exponential', np.random.randint(2, high=6))
config = {'factor': factor, 'exponential': exponential}
self._reset(config)
def _get_point(self, x):
"""Evaluates function at given x-value.
Parameters
----------
x : float
Evaluation point of function.
Returns
-------
float
Value of function evaluates at x-value.
"""
return self._factor * (x ** self._exponential)
class Continuous(InitialCondition):
"""Class for the continuous function.
Attributes
----------
factor : float
Factor by which is the evaluation point is multiplied.
Methods
-------
randomize(config)
Sets all instance variables to random value if not determined otherwise.
"""
def _reset(self, config):
"""Resets instance variables.
Parameters
----------
config : dict
Additional parameters for initial condition.
"""
super()._reset(config)
# Unpack necessary configurations
self._factor = config.pop('factor', 1)
def randomize(self, config):
"""Sets all instance variables to random value if not determined otherwise.
Parameters
----------
config : dict
Fixed parameters for initial condition.
"""
factor = config.pop('factor', np.random.uniform(low=-100, high=100))
config = {'factor': factor}
self._reset(config)
def _get_point(self, x):
"""Evaluates function at given x-value.
Parameters
----------
x : float
Evaluation point of function.
Returns
-------
float
Value of function evaluates at x-value.
"""
return self._factor
class HeavisideOneSided(InitialCondition):
"""Class for the one-sided heaviside function.
Attributes
----------
factor : float
Factor by which is the evaluation point is multiplied.
Methods
-------
is_smooth()
Returns flag whether function is smooth.
randomize(config)
Sets all instance variables to random value if not determined otherwise.
"""
def _reset(self, config):
"""Resets instance variables.
Parameters
----------
config : dict
Additional parameters for initial condition.
"""
super()._reset(config)
# Unpack necessary configurations
self._factor = config.pop('factor', -1)
def is_smooth(self):
"""Returns flag that function is not smooth."""
return False
def randomize(self, config):
"""Sets all instance variables to random value if not determined otherwise.
Parameters
----------
config : dict
Fixed parameters for initial condition.
"""
factor = config.pop('factor', np.random.choice([-1, 1]))
config = {'factor': factor}
self._reset(config)
def _get_point(self, x):
"""Evaluates function at given x-value.
Parameters
----------
x : float
Evaluation point of function.
Returns
-------
float
Value of function evaluates at x-value.
"""
return self._factor - 2 * self._factor * np.heaviside(x, 0)
class HeavisideTwoSided(InitialCondition):
"""Class for the two-sided heaviside function.
Attributes
----------
left_factor : float
Factor by which is the evaluation point is multiplied before the jump.
right_factor : float
Factor by which is the evaluation point is multiplied after the jump.
adjustment : float
Extent of adjustment of evaluation point in x-direction.
Methods
-------
is_smooth()
Returns flag whether function is smooth.
induce_adjustment(value)
Adjusts x-value of function.
randomize(config)
Sets all instance variables to random value if not determined otherwise.
"""
def _reset(self, config):
"""Resets instance variables.
Parameters
----------
config : dict
Additional parameters for initial condition.
"""
super()._reset(config)
# Unpack necessary configurations
......@@ -217,12 +740,29 @@ class HeavisideTwoSided(InitialCondition):
self._adjustment = config.pop('adjustment', 0)
def is_smooth(self):
"""Returns flag that function is not smooth."""
return False
def induce_adjustment(self, value):
"""Adjusts x-value of function.
Parameters
----------
value : float
Value of adjustment on x-axis in right direction.
"""
self._adjustment = value
def randomize(self, config):
"""Sets all instance variables to random value if not determined otherwise.
Parameters
----------
config : dict
Fixed parameters for initial condition.
"""
left_factor = config.pop('left_factor', np.random.choice([-1, 1]))
right_factor = config.pop('right_factor', np.random.choice([-1, 1]))
adjustment = config.pop('adjustment', np.random.uniform(low=-1, high=1))
......@@ -231,6 +771,19 @@ class HeavisideTwoSided(InitialCondition):
self._reset(config)
def _get_point(self, x):
"""Evaluates function at given x-value.
Parameters
----------
x : float
Evaluation point of function.
Returns
-------
float
Value of function evaluates at x-value.
"""
return self._left_factor\
- self._left_factor * np.heaviside(x - self._adjustment, 0)\
- self._right_factor * np.heaviside(x + self._adjustment, 0)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment