def test_vector(self): assert type(vector(0)) == np.ndarray assert type(vector([1, 2, 3])) == np.ndarray assert_array_equal(vector(0), [0, 0, 0]) assert_array_equal(vector(3.14), [3.14, 3.14, 3.14]) v = [1, 2, 3] assert_array_equal(vector(v), v)
def __init__(self, start=(0, 0, 0), end=(1, 0, 0), radius=1): super().__init__() self.start = vector(start) self.end = vector(end) self._start = self.xp.asarray(self.start) self._end = self.xp.asarray(self.end) self.radius = float(radius) self._rotation = rotation_from_z(self.end - self.start)
def __init__(self, start=(0, 0, 0), end=(1, 0, 0), inner_radius=1, outer_radius=2): super().__init__() self.start = vector(start) self.end = vector(end) self._start = self.xp.asarray(self.start) self._end = self.xp.asarray(self.end) self.inner_radius = float(inner_radius) self.outer_radius = float(outer_radius) self._rotation = rotation_from_z(self.end - self.start)
def from_step(cls, size, step, origin=(0, 0, 0)): size = vector(size) if np.any(size <= 0): raise ValueError('Grid size must be positive', size) step = vector(step) if np.any(step <= 0): raise ValueError('Grid step must be positive', step) n_nodes = np.ceil(size / step).astype(int) + 1 mesh = cls(size, n_nodes, origin) for i in np.nonzero(mesh.cell != step)[0]: logging.warning( f"{('X', 'Y', 'Z')[i]} step on spatial grid was reduced to " f"{mesh.cell[i]:.3f} from {step[i]:.3f} " f"to fit in a round number of cells.") return mesh
def test_wrong_shape(self): with raises(ValueError): vector((1, 2)) with raises(ValueError): vector([]) with raises(ValueError): vector((1, 2, 3, 4)) with raises(ValueError): vector([[1], [2], [3]])
def __init__(self, name="particle_source", shape=None, initial_number_of_particles: int = 0, particles_to_generate_each_step: int = 0, mean_momentum: VectorInput = 0, temperature: float = 0., charge: float = 0., mass: float = 1.): self.name = name self.shape = shape self.initial_number_of_particles = initial_number_of_particles self.particles_to_generate_each_step = particles_to_generate_each_step self.mean_momentum = vector(mean_momentum) self.temperature = temperature self.charge = charge self.mass = mass self._generator = RandomState()
def __init__(self, size, n_nodes, origin=(0, 0, 0)): self.size = vector(size) self.n_nodes = vector(n_nodes, np.int) self.origin = vector(origin)
def __init__(self, name, electric_or_magnetic, uniform_field_vector): super().__init__(name, electric_or_magnetic) self.uniform_field_vector = vector(uniform_field_vector)
def __init__(self, origin=(0, 0, 0), radius=1): super().__init__() self.origin = vector(origin) self._origin = self.xp.asarray(self.origin) self.radius = float(radius)
def __init__(self, origin=(0, 0, 0), size=(1, 1, 1)): super().__init__() self.origin = vector(origin) self.size = vector(size) self._origin = self.xp.asarray(self.origin) self._size = self.xp.asarray(self.size)
def test_dtype(self): assert vector(0).dtype == np.int assert vector(0, np.float).dtype == np.float assert vector(0, np.int).dtype == np.int assert vector(3.14).dtype == np.float assert vector(3.14, np.float).dtype == np.float with raises(TypeError): vector(3.14, np.int) assert vector([1, 2, 3.14]).dtype == np.float with raises(TypeError): vector([1, 2, 3.14], np.int) assert_array_equal(vector({ 1: 2, 3: 4 }), [{ 1: 2, 3: 4 }] * 3) # maybe unexpected, but works with raises(TypeError): vector({1: 2, 3: 4}, np.int)
def test_copy(self): a = np.array([1., 2., 3.]) assert a is a assert vector(a) is not a