def _test_export( outputdir, expected_number_materials, expected_number_modules, dry_run ): if dry_run: return # Test ini infilepath = outputdir.joinpath(PenepmaExporter.DEFAULT_IN_FILENAME) assert infilepath.exists() input = PenepmaInput() with open(infilepath, "r") as fp: input.read(fp) assert input.SKPAR.get()[0] == KPAR.ELECTRON # Test materials assert len(list(outputdir.glob("mat*.mat"))) == expected_number_materials # Test geometry geofilepath = outputdir.joinpath(PenepmaExporter.DEFAULT_GEO_FILENAME) assert geofilepath.exists() geometry = Geometry() material_lookup = dict( (index, None) for index in range(1, expected_number_materials + 1) ) with open(geofilepath, "r") as fp: geometry.read(fp, material_lookup) assert len(geometry.get_modules()) == expected_number_modules
def _write_read_input(self, input): fileobj = io.StringIO() try: input.write(fileobj) fileobj.seek(0) outinput = PenepmaInput() outinput.read(fileobj) finally: fileobj.close() return outinput
def _create_input(self, options): input = PenepmaInput() input.TITLE.set("Options") input.GEOMFN.set(self.DEFAULT_GEO_FILENAME) input.DUMPTO.set("dump1.dat") input.DUMPP.set(self.dump_interval_s) input.RSEED.set(*self.random_seeds) return input
def create_epma2(): # Create geometry surface_top = zplane(0.0) surface_bottom = zplane(-0.1) surface_cylinder = cylinder(1.0) surface_divider = xplane(0.0) module_right = Module(MATERIAL_CU, 'Right half of the sample') module_right.add_surface(surface_top, SIDEPOINTER_NEGATIVE) module_right.add_surface(surface_bottom, SIDEPOINTER_POSITIVE) module_right.add_surface(surface_cylinder, SIDEPOINTER_NEGATIVE) module_right.add_surface(surface_divider, SIDEPOINTER_POSITIVE) module_left = Module(MATERIAL_FE, 'Left half of the sample') module_left.add_surface(surface_top, SIDEPOINTER_NEGATIVE) module_left.add_surface(surface_bottom, SIDEPOINTER_POSITIVE) module_left.add_surface(surface_cylinder, SIDEPOINTER_NEGATIVE) module_left.add_module(module_right) geometry = Geometry('Cylindrical homogeneous foil') geometry.add_module(module_right) geometry.add_module(module_left) index_lookup = geometry.indexify() index_lookup[MATERIAL_CU] = 1 index_lookup[MATERIAL_FE] = 2 # Create input input = PenepmaInput() input.TITLE.set('A CU-Fe couple') input.SENERG.set(15e3) input.SPOSIT.set(2e-5, 0.0, 1.0) input.SDIREC.set(180, 0.0) input.SAPERT.set(0.0) input.materials.add(index_lookup[MATERIAL_FE], MATERIAL_FE.filename, 1e3, 1e3, 1e3, 0.2, 0.2, 1e3, 1e3) # Note inversion input.materials.add(index_lookup[MATERIAL_CU], MATERIAL_CU.filename, 1e3, 1e3, 1e3, 0.2, 0.2, 1e3, 1e3) input.GEOMFN.set('epma2.geo') input.DSMAX.add(index_lookup[module_right], 1e-4) input.DSMAX.add(index_lookup[module_left], 1e-4) input.IFORCE.add(index_lookup[module_right], 1, 4, -5, 0.9, 1.0) input.IFORCE.add(index_lookup[module_right], 1, 5, -250, 0.9, 1.0) input.IFORCE.add(index_lookup[module_right], 2, 2, 10, 1e-3, 1.0) input.IFORCE.add(index_lookup[module_right], 2, 3, 10, 1e-3, 1.0) input.IFORCE.add(index_lookup[module_left], 1, 4, -5, 0.9, 1.0) input.IFORCE.add(index_lookup[module_left], 1, 5, -7, 0.9, 1.0) input.IFORCE.add(index_lookup[module_left], 2, 2, 10, 1e-3, 1.0) input.IFORCE.add(index_lookup[module_left], 2, 3, 10, 1e-3, 1.0) input.IBRSPL.add(index_lookup[module_right], 2) input.IBRSPL.add(index_lookup[module_left], 2) input.IXRSPL.add(index_lookup[module_right], 2) input.IXRSPL.add(index_lookup[module_left], 2) input.NBE.set(0, 0, 300) input.NBANGL.set(45, 30) input.photon_detectors.add(0, 90, 0, 360, 0, 0.0, 0.0, 1000) input.photon_detectors.add(5, 15, 0, 360, 0, 0.0, 0.0, 1000) input.photon_detectors.add(15, 25, 0, 360, 0, 0.0, 0.0, 1000) input.photon_detectors.add(25, 35, 0, 360, 0, 0.0, 0.0, 1000) input.photon_detectors.add(35, 45, 0, 360, 0, 0.0, 0.0, 1000) input.photon_detectors.add(45, 55, 0, 360, 0, 0.0, 0.0, 1000) input.photon_detectors.add(55, 65, 0, 360, 0, 0.0, 0.0, 1000) input.photon_detectors.add(65, 75, 0, 360, 0, 0.0, 0.0, 1000) input.photon_detectors.add(75, 85, 0, 360, 0, 0.0, 0.0, 1000) input.GRIDX.set(-1e-5, 5e-5, 60) input.GRIDY.set(-3e-5, 3e-5, 60) input.GRIDZ.set(-6e-5, 0.0, 60) input.XRLINE.add(26010300) input.XRLINE.add(29010300) input.RESUME.set('dump2.dat') input.DUMPTO.set('dump2.dat') input.DUMPP.set(60) input.RSEED.set(-10, 1) input.REFLIN.set(26010300, 1, 1.5e-3) input.NSIMSH.set(2e9) input.TIME.set(2e9) return input
def test_epma2_read(self): filepath = os.path.join(self.testdatadir, 'epma2.in') input = PenepmaInput() with open(filepath, 'r') as fp: input.read(fp) self._test_epma2(input)
def create_epma1(): # Create geometry module = Module(MATERIAL_CU, 'Sample') module.add_surface(zplane(0.0), SIDEPOINTER_NEGATIVE) module.add_surface(zplane(-0.1), SIDEPOINTER_POSITIVE) module.add_surface(cylinder(1.0), SIDEPOINTER_NEGATIVE) geometry = Geometry('Cylindrical homogeneous foil') geometry.add_module(module) index_lookup = geometry.indexify() # Create input input = PenepmaInput() input.TITLE.set('A Cu slab') input.SENERG.set(15e3) input.SPOSIT.set(0.0, 0.0, 1.0) input.SDIREC.set(180, 0.0) input.SAPERT.set(0.0) input.materials.add(index_lookup[MATERIAL_CU], MATERIAL_CU.filename, 1e3, 1e3, 1e3, 0.2, 0.2, 1e3, 1e3) input.GEOMFN.set('epma1.geo') input.DSMAX.add(index_lookup[module], 1e-4) input.IFORCE.add(index_lookup[module], 1, 4, -5, 0.9, 1.0) input.IFORCE.add(index_lookup[module], 1, 5, -250, 0.9, 1.0) input.IFORCE.add(index_lookup[module], 2, 2, 10, 1e-3, 1.0) input.IFORCE.add(index_lookup[module], 2, 3, 10, 1e-3, 1.0) input.IBRSPL.add(index_lookup[module], 2) input.IXRSPL.add(index_lookup[module], 2) input.NBE.set(0, 0, 300) input.NBANGL.set(45, 30) input.photon_detectors.add(0, 90, 0, 360, 0, 0.0, 0.0, 1000) input.photon_detectors.add(5, 15, 0, 360, 0, 0.0, 0.0, 1000) input.photon_detectors.add(15, 25, 0, 360, 0, 0.0, 0.0, 1000) input.photon_detectors.add(25, 35, 0, 360, 0, 0.0, 0.0, 1000) input.photon_detectors.add(35, 45, 0, 360, 0, 0.0, 0.0, 1000) input.photon_detectors.add(45, 55, 0, 360, 0, 0.0, 0.0, 1000) input.photon_detectors.add(55, 65, 0, 360, 0, 0.0, 0.0, 1000) input.photon_detectors.add(65, 75, 0, 360, 0, 0.0, 0.0, 1000) input.photon_detectors.add(75, 85, 0, 360, 0, 0.0, 0.0, 1000) input.GRIDX.set(-4e-5, 4e-5, 60) input.GRIDY.set(-4e-5, 4e-5, 60) input.GRIDZ.set(-6e-5, 0.0, 60) input.XRLINE.set(29010300) input.RESUME.set('dump1.dat') input.DUMPTO.set('dump1.dat') input.DUMPP.set(60) input.RSEED.set(-10, 1) input.REFLIN.set(29010300, 1, 1.25e-3) input.NSIMSH.set(2e9) input.TIME.set(2e9) return input
async def _run(self, token, simulation, outputdir): options = simulation.options program = options.program # Export token.update(0.1, "Exporting simulation") await program.exporter.export(options, outputdir) # Run token.update(0.2, "Run simulation") infilepath = os.path.join(outputdir, program.exporter.DEFAULT_IN_FILENAME) with open(infilepath, "r") as fileobj: # Read targets penepma_input = PenepmaInput() penepma_input.read(fileobj) (target_time_s, ) = penepma_input.TIME.get() (target_trajectories, ) = penepma_input.NSIMSH.get() _izs1s200, _idet, target_uncertainty = penepma_input.REFLIN.get() if target_uncertainty is None: target_uncertainty = 1.0 fileobj.seek(0) # Setup result reader result = PenepmaResult() # Launch simulation args = [program.executable] logger.debug("Launching %s", " ".join(args)) kwargs = {} kwargs["stdin"] = fileobj kwargs["stdout"] = asyncio.subprocess.PIPE kwargs["stderr"] = asyncio.subprocess.DEVNULL kwargs["cwd"] = outputdir kwargs["startupinfo"] = create_startupinfo() process = await asyncio.create_subprocess_exec(*args, **kwargs) try: # Update token while True: stdout = await process.stdout.readline() stdout = stdout.decode("ascii").strip() if not stdout: break logger.debug("Stdout: {}".format(stdout)) if stdout.startswith("Number of simulated showers ="): try: result.read_directory(outputdir) except: logger.exception("Error while reading results") continue current_time_s = result.simulation_time_s.n progress_time = current_time_s / target_time_s current_trajectories = result.simulated_primary_showers.n progress_trajectories = (current_trajectories / target_trajectories) current_uncertainty = result.reference_line_uncertainty.n progress_uncertainty = ( target_uncertainty - current_uncertainty) / target_uncertainty progress = max( 0.001, progress_time, progress_trajectories, progress_uncertainty, ) progress = 0.7 * progress + 0.2 token.update(progress, "Running...") else: token.update(0.2, stdout) returncode = await process.wait() if returncode != 0: raise WorkerError("Error running PENEPMA") except asyncio.CancelledError: # Make sure the process is killed before raising CancelledError kill_process(process.pid) raise # Import token.update(0.9, "Importing results") simulation.results += await program.importer.import_( options, outputdir) return simulation