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 __sub__(self, other): """ Overloading the subtraction operator for position types Args: other: position object to be subtracted Raises: DataError: if other is not a position object Returns: 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 __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 object, original values scaled by factor """ 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 object to be subtracted Raises: DataError: if other is not a magnetic object Returns: 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 __add__(self, other): """ Overloading the addition operator for magnetic types Args: other: magnetic object to be added Raises: DataError: if other is not a magnetic object Returns: 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 __add__(self, other): """ Overloading the addition operator for position types Args: other: position object to be added Raises: DataError: if other is not a position object Returns: 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 acceleration types Args: other: float factor Raises: DataError: is other is not a float Returns: velocity(!) object, original values scaled by factor """ 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 __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 object, 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 = 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 __sub__(self, other): """ Overloading the subtraction operator for mesh types Args: other: mesh object to be subtracted Raises: DataError: if other is not a mesh object Returns: 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: mesh object to be added Raises: DataError: if other is not a mesh object Returns: 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 __sub__(self, other): """ Overloading the subtraction operator for fields types Args: other: fields object to be subtracted Raises: DataError: if other is not a fields object Returns: differences between caller and other values (self-other) """ if isinstance(other, type(self)): # always create new fields, since otherwise c = a - b changes a as well! p = fields(int(np.size(self.elec.values) / 3)) p.elec = self.elec - other.elec p.magn = self.magn - other.magn return p else: raise DataError("Type error: cannot subtract %s from %s" % (type(other), type(self)))
def __add__(self, other): """ Overloading the addition operator for fields types Args: other: fields object to be added Raises: DataError: if other is not a fields object Returns: sum of caller and other values (self+other) """ if isinstance(other, type(self)): # always create new fields, since otherwise c = a - b changes a as well! p = fields(int(np.size(self.elec.values) / 3)) p.elec = self.elec + other.elec p.magn = self.magn + other.magn return p else: raise DataError("Type error: cannot add %s to %s" % (type(other), type(self)))
def __init__(self, init=None, val=None): """ Initialization routine Args: init: can either be a number or another acceleration object val: initial value (default: 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, acceleration): self.values = cp.deepcopy(init.values) # if init is a number, create acceleration object with val as initial value elif isinstance(init, int): self.values = np.empty(3 * init) self.values[:] = 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)): """ Initialization routine Args: init: can either be a number or another fields object vals: 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 deepcopy (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): self.elec = fields.electric(init, val=vals[0]) self.magn = fields.magnetic(init, val=vals[1]) # 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 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, mesh): self.values = cp.deepcopy(init.values) # if init is a number or a tuple of numbers, create mesh object with val as initial value elif isinstance(init, tuple) or isinstance(init, int): self.values = np.empty(init, dtype=np.complex) self.values[:] = 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): """ Initialization routine Args: init: can either be a tuple (one int per dimension) or a number (if only one dimension is requested) or another rhs_imex_mesh object Raises: DataError: if init is none of the types above """ # if init is another rhs_imex_mesh, do a deepcopy (init by copy) if isinstance(init, type(self)): self.impl = mesh(init.impl) self.expl = mesh(init.expl) # if init is a number or a tuple of numbers, create mesh object with None as initial value elif isinstance(init, tuple) or isinstance(init, int): self.impl = mesh(init) self.expl = mesh(init) # something is wrong, if none of the ones above hit else: raise DataError('something went wrong during %s initialization' % type(self))