def test_create(self): working_dir = tempfile.mkdtemp( ) # make temp working dir for converter output files logger.info("Creating directory {:s}".format(working_dir)) generated_file_path = os.path.join(working_dir, "generated.inp") generated_file_object = gen.generate_fluka_file(generated_file_path) logger.info("Expecting file {:s} to be generated by pymchelper".format( generated_file_path)) self.assertTrue(os.path.exists(generated_file_path)) self.assertIsNotNone(generated_file_object) parsed_file_object = Input.Input() parsed_file_object.read(generated_file_path) parsed_file_path = os.path.join(working_dir, "parsed.inp") parsed_file_object.write(parsed_file_path) logger.info("Checking number of regions") self.assertEqual(len(generated_file_object.regionProperties()), len(parsed_file_object.regionProperties())) logger.info("Checking number of USRBINs") self.assertEqual(len(generated_file_object["USRBIN"]), len(parsed_file_object["USRBIN"])) logger.info("Checking number of cards") self.assertEqual(len(generated_file_object.allcards()), len(parsed_file_object.allcards())) logger.info("Removing directory {:s}".format(working_dir)) shutil.rmtree(working_dir)
def check_directory(self, dir_path): for filename in os.listdir(self.main_dir): rel_path = os.path.join(self.main_dir, filename) if rel_path.endswith(".inp"): logger.info("opening " + rel_path) input = Input.Input() input.read(rel_path) logger.info("checking if START setting is correct ") self.assertGreater(int(input.cards["START"][0].whats()[1]), 100.0) logger.info("checking if BEAM setting is correct ") self.assertEqual(input.cards["BEAM"][0].sdum(), 'PROTON') logger.info("checking if more than one USRBIN present") self.assertGreater(len(input.cards["USRBIN"]), 1)
def test_fluka(self): generate_fluka_input.main() expected_filename = "fl_sim.inp" logger.info("checking presence of {:s} file".format(expected_filename)) self.assertTrue(os.path.isfile(expected_filename)) input = Input.Input() input.read(expected_filename) logger.info("checking presence of RANDOMIZ card") self.assertIn("RANDOMIZ", input.cards) logger.info("checking if there is only one RANDOMIZ card ") self.assertEqual(len(input.cards["RANDOMIZ"]), 1) logger.info("checking if RNG setting is correct ") self.assertEqual(input.cards["RANDOMIZ"][0].whats()[2], 137) logger.info("checking presence of USRBIN cards") self.assertIn("USRBIN", input.cards) logger.info("checking if there are 8 USRBIN cards") self.assertEqual(len(input.cards["USRBIN"]), 2 * 4)
def generate_fluka_file(output_filename): """ Compose programatically fl_sim.inp Fluka input file with many combinations of estimator and mesh types. Fluka input file is saved in the default format (as Flair saves it), namely: - fixed format is used everywhere, except: - free format for geometry, forced by COMBNAME paramater in GEOBEGIN card - names instead of numbers are used as identifies of particles, materials and regions :param output_filename: output filename :return: generated Fluka object """ # create empty input file object input_configuration = Input.Input() # add title input_configuration.addCard(Card("TITLE", extra="proton beam simulation")) # add default physics settings input_configuration.addCard( Card("DEFAULTS", what=["HADROTHE"], comment="default physics for hadron therapy")) # add beam characteristics, see http://www.fluka.org/fluka.php?id=man_onl&sub=12 beam = Card("BEAM", comment="beam characteristics") beam.setSdum("PROTON") # SDUM = beam particle name. beam.setWhat(1, -0.06) # WHAT(1) < 0.0 : average beam kinetic energy in GeV beam.setWhat( 4, 2.0 ) # WHAT(4) > 0.0 : If WHAT(6) > 0.0, beam width in x-direction in cm, # The beam profile is assumed to be rectangular beam.setWhat( 5, 2.0 ) # WHAT(5) > 0.0 : If WHAT(6) > 0.0, beam width in y-direction in cm. # The beam profile is assumed to be rectangular. input_configuration.addCard( Card("BEAM", what=["PROTON", -0.06, 0.0, 0.0, -2.0, -2.0], comment="beam source")) # add beam source position, see http://www.fluka.org/fluka.php?id=man_onl&sub=14 beam_pos = Card("BEAMPOS", comment="beam source position") beam_pos.setWhat(1, 0.0) # WHAT(1) = x-coordinate of the spot centre. beam_pos.setWhat(2, 0.0) # WHAT(2) = y-coordinate of the spot centre. beam_pos.setWhat(3, -100.0) # WHAT(3) = z-coordinate of the spot centre. input_configuration.addCard(beam_pos) # set geometry, in separate function set_geometry(input_configuration) # add scoring, in separate function add_scoring(input_configuration) # random number generator settings, see http://www.fluka.org/fluka.php?id=man_onl&sub=66 rng_card = Card("RANDOMIZ") rng_card.setComment("random number generator settings") rng_card.setWhat( 2, 137 ) # Different numbers input as WHAT(2) will initialise different and independent # random number sequences, allowing the user to run several jobs in parallel for the same problem input_configuration.addCard(rng_card) # number of particles to simulate, see http://www.fluka.org/fluka.php?id=man_onl&sub=73 start_card = Card("START") start_card.setComment("number of particles to simulate") start_card.setWhat( 1, 1000 ) # WHAT(1) - maximum number of primary histories simulated in the run input_configuration.addCard(start_card) # end of input configuration http://www.fluka.org/fluka.php?id=man_onl&sub=76 input_configuration.addCard(Card("STOP")) # write file to the disk input_configuration.write(output_filename) return input_configuration