def run_calculations(project): ''' Run all calculations ''' ty_solverdir = os.environ.get('TYMPAN_SOLVERDIR') if not ty_solverdir: raise RuntimeError( 'Please set the TYMPAN_SOLVERDIR environment variable') for calc in project.computations: print('Select calculation:', calc.name) project.set_current_computation(calc) print('Update altimetry') altim = AltimetryMesh.from_site(project.site) project.update_site_altimetry(altim) print('Get Solver object from Tympan project located at %s' % ty_solverdir) solver = Solver.from_project(project, ty_solverdir) print('Build model') model = Model.from_project(project, set_sources=True, set_receptors=True) print('Launch solver') result = solver.solve(model) # print('Save results') # Import results to project project.import_result(model, result) # result must be destroyed otherwise it crashes del result
def run_calculations(input_xml, run_first=True): """ Run all calculations """ # load project project = Project.from_xml(input_xml) for i, calc in enumerate(project.computations): if run_first or i > 0: print('Select calculation '+str(i)+' :', calc.name) project.select_computation(calc) print('Update altimetry') altim = AltimetryMesh.from_site(project.site) project.update_site_altimetry(altim) print('Get Solver object from Tympan project located at %s' % ty_solverdir) solver = Solver.from_project(project, ty_solverdir) print('Build model') model = Model.from_project(project, set_sources=True, set_receptors=True) print('Launch solver') result = solver.solve(model) # Import results to project print('Import results') project.import_result(model, result) # result must be destroyed otherwise it crashes del result return project
def _compute_project(self, test_file): project = self.load_project(test_file) model = Model.from_project(project) solver = Solver.from_project(project, TEST_SOLVERS_DIR) # avoid segfaults due to multithreading solver.nb_threads = 1 solver_result = solver.solve(model) project.import_result(model, solver_result) return project
def test_with_file(self): # Load and solve the project project = self.load_project(osp.join(TEST_PROBLEM_DIR, test_file)) model = Model.from_project(project) solver = Solver.from_project(project, TEST_SOLVERS_DIR) # avoid segfaults due to multithreading solver.nthreads = 1 solver_result = solver.solve(model) project.import_result(model, solver_result) # Load the expected result result_file = osp.join(TEST_RESULT_DIR, test_file).replace('_NO_RESU', '') expected_result_project = Project.from_xml(result_file) # Compare results current_result = project.current_computation.result expected_result = expected_result_project.current_computation.result # Check we have the same number of receptors self.assertEqual(current_result.nreceptors, expected_result.nreceptors) # Check if there is a control point (TYPointControl) among the receptors # in the project. # If not, we are not interested in checking the sources since the # control points are the only receptors able to take into account the # individual contributions of the sources. # The sources here can be user sources, the machines and the buildings # (TYUserSourcePonctuelle, TYMachine, TYBatiment) check_nsources = False for i in xrange(current_result.nreceptors): if current_result.receptor(i).is_control_point(): self.assertTrue(expected_result.receptor(i).is_control_point()) check_nsources = True if check_nsources: self.assertEqual(current_result.nsources, expected_result.nsources) current_spectra = np.array( list( current_result.spectrum(current_result.receptors[i], current_result.sources[j]).values for i in xrange(current_result.nreceptors) for j in xrange(current_result.nsources))) expected_spectra = np.array( list( expected_result.spectrum(expected_result.receptors[i], expected_result.sources[j]).values for i in xrange(current_result.nreceptors) for j in xrange(current_result.nsources))) if current_result.nsources + current_result.nreceptors > 1: # Order the two spectra lists because spectra are not always kept in the same order current_spectra = sorted(current_spectra, cmp=compare_floats) expected_spectra = sorted(expected_spectra, cmp=compare_floats) for i in xrange(len(current_spectra)): # All spectra must have the same number of elements self.assertEqual(current_spectra[i].size, expected_spectra[i].size) np.testing.assert_almost_equal(current_spectra[i], expected_spectra[i], decimal=1)
def test_computation_with_manually_added_source(self): power_lvl = np.array([10.0] * 31, dtype=float) ref_proj = self.load_project('site_receptor_source.xml') ref_model = Model.from_project(ref_proj) assert ref_model.nsources == 1 assert ref_model.nreceptors == 1 ref_src = ref_model.source(0) assert (ref_src.position.x, ref_src.position.y, ref_src.position.z) == (3., 3., 2.) assert_allclose(ref_src.spectrum.to_dB().values, power_lvl) solver = Solver.from_project(ref_proj, TEST_SOLVERS_DIR) ref_result = solver.solve(ref_model).spectrum(0, 0).values # do the same with a manually added source (the xml project is the same as # 'site_receptor_source.xml' except the source has been removed) proj = self.load_project('site_receptor.xml') model = Model.from_project(proj) assert model.nsources == 0 assert model.nreceptors == 1 model.add_source((3., 3., 2.), power_lvl, 0) solver = Solver.from_project(proj, TEST_SOLVERS_DIR) result = solver.solve(model).spectrum(0, 0).values assert_allclose(ref_result, result)
def test_combined_spectra(self): project = self.load_project( osp.join('projects-panel', 'TEST_CUBE_NO_RESU.xml')) model = Model.from_project(project, set_sources=False) model.add_source(Source((-20, -30, 2), Spectrum.constant(100.0))) model.add_source(Source((10, 50, 2), Spectrum.constant(150.0))) solver = Solver.from_project(project, solverdir=TEST_SOLVERS_DIR) result = solver.solve(model) combined_spectra = result.combined_spectra() expected_spectra = np.array([ 2.0602e-03, 1.9806e-03, 1.8942e-03, 1.8005e-03, 1.6987e-03, 1.5867e-03, 1.4610e-03, 1.3166e-03, 1.1486e-03, 9.5532e-04, 7.4293e-04, 5.2950e-04, 3.4102e-04, 1.9865e-04, 1.0676e-04, 5.4570e-05, 2.7016e-05, 1.2721e-05, 5.3064e-06, 1.7143e-06, 3.4595e-07, 3.1354e-08, 7.6832e-10, 2.3192e-12, 2.5400e-16, 1.6487e-22, 1.3216e-23, 1.3216e-23, 1.3216e-23, 1.3216e-23, 1.3216e-23 ]) for rec in range(result.nreceptors): np.testing.assert_almost_equal(combined_spectra[rec, :], expected_spectra, decimal=4)
def run_calculations(project): ''' Run all calculations ''' for calc in project.computations: print('Select calculation:', calc.name) project.select_computation(calc) print('Get Solver object from Tympan project located at %s' % ty_solverdir) solver = Solver.from_project(project, ty_solverdir) print('Build model') model = Model.from_project(project, set_sources=True, set_receptors=True) print('Launch solver') result = solver.solve(model) # print('Save results') # Import results to project project.import_result(model, result) # result must be destroyed otherwise it crashes del result
def solve(input_project, output_project, output_mesh, solverdir, multithreading_on=True, interactive=False, verbose=False): """ Solve an acoustic problem with Code_TYMPAN from Keywords arguments: input_project -- XML file containing the serialized project with the "calcul" to solve output_project -- XML file where to put the project updated with the results of the computation output_mesh -- a file in which to put the altimetry mesh once computed (ply format) solvedir -- directory containing the solver plugin ------- optional (debug): multithreading_on -- set it to False to solve the acoustic problem with only one thread interactive -- if True, pdb debugger will be invoked before running solving the acoustic problem, so that the program can be executed in interactive mode. The execution is logged into 'tympan.log', created in the directory of the input XML project (the one opened from the Code_TYMPAN GUI) """ if interactive: import pdb pdb.set_trace() ret = False # Load an existing project and retrieve its calcul to solve it try: logging.info("Trying to load project ...") project = Project.from_xml(input_project, verbose) except RuntimeError: logging.exception("Couldn't load the acoustic project from %s file", input_project) raise logging.info("Project loaded !") # Recompute and export altimetry altimesh = AltimetryMesh.from_site(project.site) altimesh.to_ply(output_mesh) # Update site and the project before building the solver model logging.info("Updating site ...") project.update_site_altimetry(altimesh, verbose) # Solver model model = Model.from_project(project) logging.info( "Solver model built.\nNumber of sources: %d\nNumber of receptors: %d", model.nsources, model.nreceptors) # Load solver plugin and run it on the current computation logging.info("Trying to load solver ...") solver = Solver.from_project(project, solverdir, verbose) if not multithreading_on: solver.nthread = 1 logging.info("Checking solver model ...") _check_solver_model(model, project.site) logging.debug("Calling C++ SolverInterface::solve() method") try: solver_result = solver.solve(model) except RuntimeError as exc: logging.error(str(exc)) logging.info("It doesn't work", str(exc)) raise logging.info("Solver computation done !") # Export solver results to the business model logging.info("Loading results from solver ...") project.import_result(model, solver_result) # Reserialize project try: logging.info("Trying to export result project to xml ...") project.to_xml(output_project) except ValueError: logging.exception("Couldn't export the acoustic results to %s file", output_project) raise
def solve(input_project, output_project, output_mesh, solverdir, parameters={}, multithreading_on=True, interactive=False, verbose=False, altimetry_parameters={}): """ Solve an acoustic problem with Code_TYMPAN from Keywords arguments: input_project -- XML file containing the serialized project with the "calcul" to solve output_project -- XML file where to put the project updated with the results of the computation output_mesh -- a file in which to put the altimetry mesh once computed (ply format) solverdir -- directory containing the solver plugin ------- optional : parameters -- Dictionary containing the solver parameters the user wants to change from their default value in the form {'parameter_name':value,...}. The possible parameters are: atmos_pressure, atmos_temperature, atmos_hygrometry, wind_direction, analytic_gradC, analytic_gradV, ray_tracing_order, discretization, nb_rays_per_source, max_length, size_receiver, accelerator, max_tree_depth, angle_diff_min, cylindre_thick, max_profondeur, use_sol, max_reflexion, max_diffraction, diffraction_use_random_sampler, nb_ray_with_diffraction, diffraction_drop_down_nb_rays, diffraction_filter_ray_at_creation, use_path_dif_validation, max_path_difference, diffraction_use_distance_as_filter, keep_debug_ray, use_post_filters, curve_ray_sampler, initial_angle_theta, final_angle_theta, initial_angle_phi, final_angle_phi, analytic_nb_ray, analytic_tMax, analyticH, analytic_dMax, analytic_type_transfo, mesh_element_size_max, show_scene, minSR_distance, nb_threads, use_real_ground, use_screen, use_lateral_diffraction, use_reflection, propa_conditions, h1parameter, mod_summation, use_meteo, use_fresnel_area, anime3D_sigma, anime3D_forceC, anime3D_keep_rays, debug_use_close_event_selector, debug_use_diffraction_angle_selector, debug_use_diffraction_path_selector, debug_use_fermat_selector, debug_use_face_selector ------- optional (debug): multithreading_on -- set it to False to solve the acoustic problem with only one thread interactive -- if True, pdb debugger will be invoked before running solving the acoustic problem, so that the program can be executed in interactive mode. The execution is logged into 'tympan.log', created in the directory of the input XML project (the one opened from the Code_TYMPAN GUI) """ if interactive: import pdb pdb.set_trace() # Load an existing project and retrieve its calcul to solve it try: logging.info("Trying to load project ...") project = Project.from_xml(input_project, verbose=verbose, **altimetry_parameters) except RuntimeError: logging.exception("Couldn't load the acoustic project from %s file", input_project) raise logging.info("Project loaded !") # Export altimetry project.export_altimetry(output_mesh) # Solver model model = Model.from_project(project) logging.info( "Solver model built.\nNumber of sources: %d\nNumber of receptors: %d", model.nsources, model.nreceptors) # Load solver plugin and run it on the current computation logging.info("Trying to load solver ...") solver = Solver.from_project(project, solverdir, verbose) # Setting the parameters chosen by the user for parameter in parameters: setattr(solver, parameter, parameters[parameter]) if not multithreading_on: solver.nb_threads = 1 logging.info("Checking solver model ...") _check_solver_model(model, project.site) logging.debug("Calling C++ SolverInterface::solve() method") try: solver_result = solver.solve(model) except RuntimeError as exc: logging.error(str(exc)) logging.info("It doesn't work", str(exc)) raise logging.info("Solver computation done !") # Export solver results to the business model logging.info("Loading results from solver ...") project.import_result(model, solver_result) # Reserialize project try: logging.info("Trying to export result project to xml ...") project.to_xml(output_project) except ValueError: logging.exception("Couldn't export the acoustic results to %s file", output_project) raise