def __init__(self, owner: Simulation, input_data: dict): super().__init__(owner, input_data) self.position = np.zeros((1, 3)) self.momentum = np.zeros((1, 3)) self.mass = input_data.get('mass', 1) self.spring_constant = input_data.get('spring_constant', 1) self.push = owner.find_tool_by_name(input_data["pusher"]).push
def simulation_fixture(): """Creates a simulation object used to create a B.O.S. object""" input_data = { "Tools": { "ForwardEuler": {} }, "Clock": { "start_time": 0, "end_time": 1, "dt": 1 }, "PhysicsModules": {}, "Diagnostics": {}, } simulation = Simulation(input_data) simulation.prepare_simulation() return simulation
def test_bos_leapfrog(bos_run, tmp_path): """Tests block_on_spring app with LeapFrog ComputeTool and compares to output files with a "good" output. """ bos_run["PhysicsModules"]["BlockOnSpring"]["pusher"] = "Leapfrog" bos_run["Diagnostics"]["directory"] = tmp_path / "output" sim = Simulation(bos_run) sim.run() for filename in ['block_p', 'block_x', 'time']: ref_data = np.genfromtxt( f'tests/fixtures/block_on_spring/output_leapfrog/{filename}.csv', delimiter=',') tmp_data = np.genfromtxt(tmp_path / f'output/{filename}.csv', delimiter=',') assert np.allclose(ref_data, tmp_data, rtol=1e-05, atol=1e-08)
def test_bos_leapfrog(bos_config): """Tests block_on_spring app with LeapFrog ComputeTool and compares to output files with a "good" output. """ bos_config["PhysicsModules"]["BlockOnSpring"]["pusher"] = "Leapfrog" bos_config["Diagnostics"]["directory"] = ("test_data/test_output/" "output_Leapfrog/") sim = Simulation(bos_config) sim.run() for filename in ['block_p', 'block_x', 'time']: ref_data = np.genfromtxt( 'test_data/reference_output/' f'output_Leapfrog/{filename}.csv', delimiter=',') tmp_data = np.genfromtxt( 'test_data/test_output/' f'output_Leapfrog/{filename}.csv', delimiter=',') assert np.allclose(ref_data, tmp_data)
def __init__(self, owner: Simulation, input_data: dict): super().__init__(owner, input_data) self.E = None self.x = input_data["position"] self.interp_field = owner.grid.create_interpolator(self.x) self.position = np.zeros((1, 3)) self.momentum = np.zeros((1, 3)) self.eoverm = 1.7588e11 self.charge = 1.6022e-19 self.mass = 9.1094e-31 self.push = owner.find_tool_by_name(input_data["pusher"]).push
def bos_run(): # Note: grid isn't used, but "gridless" sims aren't an option yet problem_config = { "Grid": {"N": 2, "x_min": 0, "x_max": 1}, "Clock": {"start_time": 0, "end_time": 10, "num_steps": 10}, "PhysicsModules": { "BlockOnSpring": { "mass": 1, "spring_constant": 1, "pusher": "Leapfrog", "x0": [0, 1, 0], "p0": [0, 0, 0] } }, "Tools": { "Leapfrog": {}, "BlockForwardEuler": {}, "BackwardEuler": {} }, "Diagnostics": { # default values come first "directory": "tmp/block_on_spring/output_leapfrog/", "output_type": "csv", "clock": {"filename": "time.csv"}, "BlockDiagnostic": [ {'component': 'momentum', 'filename': 'block_p.csv'}, {'component': 'position', 'filename': 'block_x.csv'} ] } } sim = Simulation(problem_config) sim.run() problem_config["PhysicsModules"]["BlockOnSpring"]["pusher"] = "BlockForwardEuler" problem_config["Diagnostics"]["directory"] = "tmp/block_on_spring/output_forwardeuler/" sim = Simulation(problem_config) sim.run() problem_config["PhysicsModules"]["BlockOnSpring"]["pusher"] = "BackwardEuler" problem_config["Diagnostics"]["directory"] = "tmp/block_on_spring/output_backwardeuler/" sim = Simulation(problem_config) sim.run()
'units': 'kg m/s', 'coords': ["dim0", "vector component"], 'long_name': 'Block Momentum' }, { 'name': 'Block:position', 'units': 'm', 'coords': ["dim0", "vector component"], 'long_name': 'Block Position' }, ] } } } sim = Simulation(block_config) sim.run() block_config["PhysicsModules"]["BlockOnSpring"]["pusher"] = "ForwardEuler" block_config["Diagnostics"]["directory"] = "output_euler/" sim = Simulation(block_config) sim.run() # Now plot the outputs lf_output = xr.load_dataset('output_leapfrog/output.nc') print(lf_output) lf_output['Block:position'][:, 1].plot(x='time', label='Leapfrog') plt.show() fe_output = xr.load_dataset('output_euler/output.nc')
"Leapfrog": {}, "ForwardEuler": {}, }, "Diagnostics": { # default values come first "directory": "output_leapfrog/", "output_type": "csv", "clock": { "filename": "time.csv" }, "BlockDiagnostic": [{ 'component': 'momentum', 'filename': 'block_p.csv' }, { 'component': 'position', 'filename': 'block_x.csv' }] } } sim = Simulation(problem_config) sim.run() problem_config["PhysicsModules"]["BlockOnSpring"]["pusher"] = "ForwardEuler" problem_config["Diagnostics"]["directory"] = "output_euler/" sim = Simulation(problem_config) sim.run()
def print_diagnose(self, data): print(data) def csv_diagnose(self, data): self.csv.append(data) class ForwardEuler(ComputeTool): def __init__(self, owner: Simulation, input_data: dict): super().__init__(owner, input_data) self.dt = None def initialize(self): self.dt = self.owner.clock.dt def push(self, position, momentum, charge, mass, E, B): p0 = momentum.copy() momentum[:] = momentum + self.dt * E * charge position[:] = position + self.dt * p0 / mass PhysicsModule.register("EMWave", EMWave) PhysicsModule.register("ChargedParticle", ChargedParticle) Diagnostic.register("ParticleDiagnostic", ParticleDiagnostic) ComputeTool.register("ForwardEuler", ForwardEuler) input_file = "particle_in_field.toml" sim = Simulation(input_file) sim.run()