def __init__(self, init=None, val=None): """ Initialization routine Args: init: can either be a number or another fields object val: initial tuple of values for electric and magnetic (default: (None,None)) Raises: DataError: if init is none of the types above """ # if init is another fields object, do a copy (init by copy) if isinstance(init, type(self)): self.elec = fields.electric(init.elec) self.magn = fields.magnetic(init.magn) # if init is a number, create fields object and pick the corresponding initial values elif isinstance(init, int) or isinstance(init, tuple): if isinstance(val, int) or isinstance(val, float) or val is None: self.elec = fields.electric(init, val=val) self.magn = fields.magnetic(init, val=val) elif isinstance(val, tuple) and len(val) == 2: self.elec = fields.electric(init, val=val[0]) self.magn = fields.magnetic(init, val=val[1]) else: raise DataError('wrong type of val, got %s' % val) # something is wrong, if none of the ones above hit else: raise DataError('something went wrong during %s initialization' % type(self))
def __init__(self, init=None, val=None): """ Initialization routine Args: init: can either be a number or another particle object val: initial tuple of values for position and velocity (default: (None,None)) Raises: DataError: if init is none of the types above """ # if init is another particles object, do a copy (init by copy) if isinstance(init, type(self)): self.pos = particles.position(init.pos) self.vel = particles.velocity(init.vel) self.q = init.q.copy() self.m = init.m.copy() # if init is a number, create particles object and pick the corresponding initial values elif isinstance(init, int): if isinstance(val, int) or isinstance(val, float) or val is None: self.pos = particles.position(init, val=val) self.vel = particles.velocity(init, val=val) self.q = np.zeros(init) self.q[:] = val self.m = np.zeros(init) self.m[:] = val elif isinstance(val, tuple) and len(val) == 4: self.pos = particles.position(init, val=val[0]) self.vel = particles.velocity(init, val=val[1]) self.q = np.zeros(init) self.q[:] = val[2] self.m = np.zeros(init) self.m[:] = val[3] else: raise DataError('type of val is wrong, got %s', val) elif isinstance(init, tuple): if isinstance(val, int) or isinstance(val, float) or val is None: self.pos = particles.position(init, val=val) self.vel = particles.velocity(init, val=val) self.q = np.zeros(init[-1]) self.q[:] = val self.m = np.zeros(init[-1]) self.m[:] = val elif isinstance(val, tuple) and len(val) == 4: self.pos = particles.position(init, val=val[0]) self.vel = particles.velocity(init, val=val[1]) self.q = np.zeros(init[-1]) self.q[:] = val[2] self.m = np.zeros(init[-1]) self.m[:] = val[3] else: raise DataError('type of val is wrong, got %s', val) # something is wrong, if none of the ones above hit else: raise DataError('something went wrong during %s initialization' % type(self))
def __init__(self, init=None, vals=(None, None, None, None)): """ Initialization routine Args: init: can either be a number or another particle object vals: initial tuple of values for position and velocity (default: (None,None)) Raises: DataError: if init is none of the types above """ # if init is another particles object, do a deepcopy (init by copy) if isinstance(init, type(self)): self.pos = particles.position(init.pos) self.vel = particles.velocity(init.vel) self.q = cp.deepcopy(init.q) self.m = cp.deepcopy(init.m) # if init is a number, create particles object and pick the corresponding initial values elif isinstance(init, int): self.pos = particles.position(init, val=vals[0]) self.vel = particles.velocity(init, val=vals[1]) self.q = np.zeros(int(np.size(self.pos.values) / 3)) self.q[:] = vals[2] self.m = np.zeros(int(np.size(self.pos.values) / 3)) self.m[:] = vals[3] # something is wrong, if none of the ones above hit else: raise DataError('something went wrong during %s initialization' % type(self))
def apply_mat(self, A): """ Matrix multiplication operator Args: A: a matrix Returns: mesh.rhs_imex_mesh: each component multiplied by the matrix A """ if not A.shape[1] == self.impl.values.shape[0]: raise DataError("ERROR: cannot apply operator %s to %s" % (A, self.impl)) if not A.shape[1] == self.expl.values.shape[0]: raise DataError("ERROR: cannot apply operator %s to %s" % (A, self.expl)) me = rhs_imex_mesh(A.shape[1]) me.impl.values = A.dot(self.impl.values) me.expl.values = A.dot(self.expl.values) return me
def apply_mat(self, A): """ Matrix multiplication operator Args: A: a matrix Returns: mesh.mesh: component multiplied by the matrix A """ if not A.shape[1] == self.values.shape[0]: raise DataError("ERROR: cannot apply operator %s to %s" % (A, self)) me = mesh(A.shape[0]) me.values = A.dot(self.values) return me
def __sub__(self, other): """ Overloading the subtraction operator Args: other: PETSc object to be subtracted Raises: DataError: if other is not a DMDA object Returns: differences between caller and other values (self-other) """ if isinstance(other, type(self)): me = petsc_data(self) me.values -= other.values return me else: raise DataError("Type error: cannot subtract %s from %s" % (type(other), type(self)))
def __add__(self, other): """ Overloading the addition operator Args: other: PETSc object to be added Raises: DataError: if other is not a DMDA object Returns: sum of caller and other values (self+other) """ if isinstance(other, type(self)): me = petsc_data(self) me.values += other.values return me else: raise DataError("Type error: cannot add %s to %s" % (type(other), type(self)))
def __rmul__(self, other): """ Overloading the right multiply by factor operator Args: other (float): factor Raises: DataError: is other is not a float Returns: copy of original values scaled by factor """ if isinstance(other, float) or isinstance(other, complex): me = petsc_data(self) me.values *= other return me else: raise DataError("Type error: cannot multiply %s to %s" % (type(other), type(self)))
def __init__(self, init=None, val=None): """ Initialization routine Args: init: can either be a tuple (one int per dimension) or a number (if only one dimension is requested) or another mesh object val: initial value (default: None) Raises: DataError: if init is none of the types above """ # if init is another mesh, do a deepcopy (init by copy) if isinstance(init, de.Domain): self.values = [init.new_field()] elif isinstance(init, type(self)): self.values = [] for f in init.values: self.values.append(f.domain.new_field()) self.values[-1]['g'] = f['g'] self.values[-1]['c'] = f['c'] elif isinstance(init, tuple): self.values = [] for i in range(init[1]): self.values.append(init[0].new_field()) # elif isinstance(init, type(self)): # if hasattr(init, 'values'): # self.values = init.values.domain.new_field() # self.values['g'] = init.values['g'] # elif hasattr(init, 'list_of_values'): # self.list_of_values = [] # for f in init.list_of_values: # self.list_of_values.append(f.domain.new_field()) # self.list_of_values[-1]['g'] = f['g'] # else: # raise DataError('something went really wrong during %s initialization' % type(self)) # elif isinstance(init, tuple): # self.list_of_values = [] # for i in range(init[0]): # self.list_of_values.append(init[1].new_field()) else: raise DataError('something went wrong during %s initialization' % type(self))
def __rmul__(self, other): """ Overloading the right multiply by factor operator for electric types Args: other (float): factor Raises: DataError: is other is not a float Returns: electric: original values scaled by factor """ if isinstance(other, float): # create new electric, no specific interpretation of float factor E = fields.electric(self.values.shape) E.values = self.values * other return E else: raise DataError("Type error: cannot multiply %s to %s" % (type(other), type(self)))
def __rmul__(self, other): """ Overloading the right multiply by factor operator for velocity types Args: other: float factor Raises: DataError: is other is not a float Returns: position: original values scaled by factor, transformed to position """ if isinstance(other, float): # create new position, interpret float factor as time (time x velocity = position) pos = particles.position(int(np.size(self.values) / 3)) pos.values = self.values * other return pos else: raise DataError("Type error: cannot multiply %s to %s" % (type(other), type(self)))
def __rmul__(self, other): """ Overloading the right multiply by factor operator for mesh types Args: other (float): factor Raises: DataError: is other is not a float Returns: mesh.mesh: copy of original values scaled by factor """ if isinstance(other, float) or isinstance(other, complex): # always create new mesh, since otherwise c = f*a changes a as well! me = mesh(np.shape(self.values)) me.values = self.values * other return me else: raise DataError("Type error: cannot multiply %s to %s" % (type(other), type(self)))
def __add__(self, other): """ Overloading the addition operator for velocity types Args: other: velocity object to be added Raises: DataError: if other is not a velocity object Returns: velocity: sum of caller and other values (self+other) """ if isinstance(other, type(self)): # always create new position, since otherwise c = a + b changes a as well! vel = particles.velocity(self.values.shape) vel.values = self.values + other.values return vel else: raise DataError("Type error: cannot add %s to %s" % (type(other), type(self)))
def __add__(self, other): """ Overloading the addition operator for magnetic types Args: other (magnetic): magnetic object to be added Raises: DataError: if other is not a magnetic object Returns: magnetic: sum of caller and other values (self+other) """ if isinstance(other, type(self)): # always create new magnetic, since otherwise c = a + b changes a as well! M = fields.magnetic(int(np.size(self.values) / 3)) M.values = self.values + other.values return M else: raise DataError("Type error: cannot add %s to %s" % (type(other), type(self)))
def __rmul__(self, other): """ Overloading the right multiply by factor operator for acceleration types Args: other (float): factor Raises: DataError: is other is not a float Returns: velocity: original values scaled by factor, tranformed to velocity """ if isinstance(other, float): # create new velocity, interpret float factor as time (time x acceleration = velocity) vel = particles.velocity(int(np.size(self.values) / 3)) vel.values = self.values * other return vel else: raise DataError("Type error: cannot multiply %s to %s" % (type(other), type(self)))
def __sub__(self, other): """ Overloading the subtraction operator for electric types Args: other (electric): electric object to be subtracted Raises: DataError: if other is not a electric object Returns: electric: difference of caller and other values (self-other) """ if isinstance(other, type(self)): # always create new electric, since otherwise c = a + b changes a as well! E = fields.electric(self.values.shape) E.values = self.values - other.values return E else: raise DataError("Type error: cannot subtract %s from %s" % (type(other), type(self)))
def __add__(self, other): """ Overloading the addition operator for electric types Args: other (electric): electric object to be added Raises: DataError: if other is not a electric object Returns: electric: sum of caller and other values (self+other) """ if isinstance(other, type(self)): # always create new electric, since otherwise c = a + b changes a as well! E = fields.electric(self.values.shape) E.values = self.values + other.values return E else: raise DataError("Type error: cannot add %s to %s" % (type(other), type(self)))
def __add__(self, other): """ Overloading the addition operator for acceleration types Args: other (acceleration): acceleration object to be added Raises: DataError: if other is not a acceleration object Returns: acceleration: sum of caller and other values (self+other) """ if isinstance(other, type(self)): # always create new acceleration, since otherwise c = a + b changes a as well! acc = acceleration(self.values.shape) acc.values = self.values + other.values return acc else: raise DataError("Type error: cannot add %s to %s" % (type(other), type(self)))
def __sub__(self, other): """ Overloading the subtraction operator for velocity types Args: other: velocity object to be subtracted Raises: DataError: if other is not a velocity object Returns: velocity: differences between caller and other values (self-other) """ if isinstance(other, type(self)): # always create new position, since otherwise c = a - b changes a as well! vel = particles.velocity(self.values.shape) vel.values = self.values - other.values return vel else: raise DataError("Type error: cannot subtract %s from %s" % (type(other), type(self)))
def __add__(self, other): """ Overloading the addition operator for position types Args: other (position): position object to be added Raises: DataError: if other is not a position object Returns: position: sum of caller and other values (self+other) """ if isinstance(other, type(self)): # always create new position, since otherwise c = a + b changes a as well! pos = particles.position(int(np.size(self.values) / 3)) pos.values = self.values + other.values return pos else: raise DataError("Type error: cannot add %s to %s" % (type(other), type(self)))
def __rmul__(self, other): """ Overloading the right multiply by factor operator for magnetic types Args: other (float): factor Raises: DataError: is other is not a float Returns: electric: original values scaled by factor, transformed to electric """ if isinstance(other, float): # create new magnetic, no specific interpretation of float factor M = fields.magnetic(int(np.size(self.values) / 3)) M.values = self.values * other return M else: raise DataError("Type error: cannot multiply %s to %s" % (type(other), type(self)))
def __sub__(self, other): """ Overloading the subrtaction operator for magnetic types Args: other (magnetic): magnetic object to be subtracted Raises: DataError: if other is not a magnetic object Returns: magnetic: difference of caller and other values (self-other) """ if isinstance(other, type(self)): # always create new magnetic, since otherwise c = a + b changes a as well! M = fields.magnetic(int(np.size(self.values) / 3)) M.values = self.values - other.values return M else: raise DataError("Type error: cannot subtract %s from %s" % (type(other), type(self)))
def __rmul__(self, other): """ Overloading the right multiply by factor operator for position types Args: other (float): factor Raises: DataError: is other is not a float Returns: position: original values scaled by factor """ if isinstance(other, float): # create new position pos = particles.position(int(np.size(self.values) / 3)) pos.values = self.values * other return pos else: raise DataError("Type error: cannot multiply %s to %s" % (type(other), type(self)))
def __sub__(self, other): """ Overloading the subtraction operator for position types Args: other (position): position object to be subtracted Raises: DataError: if other is not a position object Returns: position: differences between caller and other values (self-other) """ if isinstance(other, type(self)): # always create new position, since otherwise c = a - b changes a as well! pos = particles.position(int(np.size(self.values) / 3)) pos.values = self.values - other.values return pos else: raise DataError("Type error: cannot subtract %s from %s" % (type(other), type(self)))
def __add__(self, other): """ Overloading the addition operator for mesh types Args: other (mesh.mesh): mesh object to be added Raises: DataError: if other is not a mesh object Returns: mesh.mesh: sum of caller and other values (self+other) """ if isinstance(other, mesh): # always create new mesh, since otherwise c = a + b changes a as well! me = mesh(np.shape(self.values)) me.values = self.values + other.values return me else: raise DataError("Type error: cannot add %s to %s" % (type(other), type(self)))
def __rmul__(self, other): """ Overloading the right multiply by factor operator for mesh types Args: other (float): factor Raises: DataError: is other is not a float Returns: fenics_mesh: copy of original values scaled by factor """ if isinstance(other, float): # always create new mesh, since otherwise c = f*a changes a as well! me = fenics_mesh(self) me.values = df.Function(self.V, other * self.values.vector()) return me else: raise DataError("Type error: cannot multiply %s to %s" % (type(other), type(self)))
def __sub__(self, other): """ Overloading the subtraction operator for mesh types Args: other (complex_mesh.mesh): mesh object to be subtracted Raises: DataError: if other is not a mesh object Returns: complex_mesh.mesh: differences between caller and other values (self-other) """ if isinstance(other, mesh): # always create new mesh, since otherwise c = a - b changes a as well! me = mesh(np.shape(self.values)) me.values = self.values - other.values return me else: raise DataError("Type error: cannot subtract %s from %s" % (type(other), type(self)))
def __add__(self, other): """ Overloading the addition operator for mesh types Args: other (fenics_mesh): mesh object to be added Raises: DataError: if other is not a mesh object Returns: fenics_mesh: sum of caller and other values (self+other) """ if isinstance(other, type(self)): # always create new mesh, since otherwise c = a + b changes a as well! me = fenics_mesh(other) me.values = df.Function(self.V, self.values.vector() + other.values.vector()) return me else: raise DataError("Type error: cannot add %s to %s" % (type(other), type(self)))
def __sub__(self, other): """ Overloading the subtraction operator for mesh types Args: other (fenics_mesh): mesh object to be subtracted Raises: DataError: if other is not a mesh object Returns: fenics_mesh: differences between caller and other values (self-other) """ if isinstance(other, type(self)): # always create new mesh, since otherwise c = a - b changes a as well! me = fenics_mesh(other) me.values = df.Function(self.V, self.values.vector() - other.values.vector()) return me else: raise DataError("Type error: cannot subtract %s from %s" % (type(other), type(self)))
def __add__(self, other): """ Overloading the addition operator for rhs types Args: other (rhs_fenics_mesh): rhs object to be added Raises: DataError: if other is not a rhs object Returns: rhs_fenics_mesh: sum of caller and other values (self-other) """ if isinstance(other, rhs_fenics_mesh): # always create new rhs_imex_mesh, since otherwise c = a + b changes a as well! me = rhs_fenics_mesh(self) me.impl = self.impl + other.impl me.expl = self.expl + other.expl return me else: raise DataError("Type error: cannot add %s to %s" % (type(other), type(self)))