def save_figure(output_file=None): global _plot_number # If no name for the output file is given # simply enumerate the different plots if output_file is None: output_file = 'acropolis_plot_{}.pdf'.format(_plot_number) _plot_number += 1 plt.savefig(output_file) print_info("Figure has been saved as '{}'".format(output_file), "acropolis.plot.save_figure")
def run_disintegration(self): # Print a warning if the injection energy # is larger than 1GeV, as this might lead # to wrong results if not universal and int( self._sE0 ) > 1e3: print_warning( "Injection energy > 1 GeV. Results cannot be trusted.", "acropolis.models.AbstractMode.run_disintegration" ) # Print a warning if the temperature range # of the model is not covered by the data # in cosmo_file.dat cf_temp_rg = self._sII.cosmo_range() if not (cf_temp_rg[0] <= self._sTrg[0] <= self._sTrg[1] <= cf_temp_rg[1]): print_warning( "Temperature range not covered by input data. Results cannot be trusted.", "acropolis.models.AbstractMode.run_disintegration" ) # If the energy is below all thresholds, # simply return the initial abundances if self._sE0 <= Emin: print_info( "Injection energy is below all thresholds. No calculation required.", "acropolis.models.AbstractModel.run_disintegration", verbose_level=1 ) return self._squeeze_decays( self._sII.bbn_abundances() ) # Calculate the different transfer matrices ########################################### # 1. pre-decay pred_mat = self._pred_matrix() # 2. photodisintegration pdi_mat = self._pdi_matrix() # 3. post-decay postd_mat = self._postd_matrix() # Combine transf_mat = postd_mat.dot( pdi_mat.dot( pred_mat ) ) # Calculate the final abundances Yf = np.column_stack( list( transf_mat.dot( Y0i ) for Y0i in self._sII.bbn_abundances().transpose() ) ) return Yf
def import_data_from_db(): pkg_dir, _ = path.split(__file__) db_file = path.join(pkg_dir, "data", "rates.db.gz") ratedb = None if not usedb or not path.exists(db_file): return ratedb start_time = time() print_info("Extracting and reading database files.", "acropolis.db.import_data_from_db") ratefl = gzip.open(db_file, "rb") ratedb = pickle.load(ratefl) ratefl.close() end_time = time() print_info("Finished after " + str(int((end_time - start_time) * 1e4) / 10) + "ms.") return ratedb
def get_all_matp(self): NT = len(self._sTemp) start_time = time() print_info( "Calculating transfer matrices for all temperatures.", "acropolis.nucl.MatrixGenerator.get_all_matp", verbose_level=2 ) all_mpdi = np.zeros( (NT, NY, NY) ) all_mdcy = np.zeros( (NT, NY, NY) ) for i, temp in enumerate(self._sTemp): progress = 100*i/NT print_info( "Progress: {:.1f}%".format(progress), "acropolis.nucl.MatrixGenerator.get_all_matp", eol="\r", verbose_level=2 ) all_mpdi[i, :, :], all_mdcy[i, :, :] = self.get_matp(temp) end_time = time() print_info( "Finished after {:.1f}s.".format(end_time - start_time), "acropolis.nucl.MatrixGenerator.get_all_matp", verbose_level=2 ) return self._sTemp, (all_mpdi, all_mdcy)
def run_disintegration(self): # Print a warning of the injection energy # is larger than 1GeV, as this might lead # to wrong results if int(self._sE0) > 1e3: print_warning( "Injection energy > 1 GeV. Results cannot be trusted.", "acropolis.models.AbstractMode.run_disintegration") # If the energy is below all thresholds, # simply return the initial abundances if self._sE0 <= Emin: print_info( "Injection energy is below all thresholds. No calculation required.", "acropolis.models.AbstractModel.run_disintegration") return self._squeeze_decays(self._sII.bbn_abundances()) # Calculate the different transfer matrices ########################################### # 1. pre-decay pred_mat = self._pred_matrix() # 2. photodisintegration pdi_mat = self._pdi_matrix() # 3. post-decay postd_mat = self._postd_matrix() # Combine transf_mat = postd_mat.dot(pdi_mat.dot(pred_mat)) # Calculate the final abundances Yf = np.column_stack( list( transf_mat.dot(Y0i) for Y0i in self._sII.bbn_abundances().transpose())) return Yf
def import_data_from_db(): pkg_dir, _ = path.split(__file__) db_file = path.join(pkg_dir, "data", "rates.db.gz") ratedb = None if not usedb or not path.exists(db_file): return ratedb start_time = time() print_info("Extracting and reading database files.", "acropolis.db.import_data_from_db", verbose_level=1) ratefl = gzip.open(db_file, "rb") ratedb = pickle.load(ratefl) ratefl.close() end_time = time() print_info("Finished after {:.1f}ms.".format(1e3 * (end_time - start_time)), "acropolis.db.import_data_from_db", verbose_level=1) return ratedb
def perform_scan(self, cores=1): num_cpus = cpu_count() if cores == -1 else cores start_time = time() print_info( "Running scan for {} on {} cores.".format(self._sModel.__name__, num_cpus), "acropolis.scans.BufferedScanner.perform_scan", verbose_level=3 ) with Pool(processes=num_cpus) as pool: # Loop over all possible combinations, by... # ...1. looping over the 'parallel' parameter (map) # ...2. looping over all parameter combinations, # thereby exclusing the 'parallel' parameter (perform_non_parallel_scan) async_results = pool.map_async( self._perform_non_parallel_scan, self._sScanp[self._sId_pp], 1 ) progress = 0 while ( progress < 100 ) or ( not async_results.ready() ): progress = 100*( self._sDp - async_results._number_left )/self._sDp print_info( "Progress: {:.1f}%".format(progress), "acropolis.scans.BufferedScanner.perform_scan", eol="\r", verbose_level=3 ) sleep(1) parallel_results = async_results.get() pool.terminate() parallel_results = np.array(parallel_results) old_shape = parallel_results.shape parallel_results.shape = (old_shape[0]*old_shape[1], len( self._sScanp_id ) + 3*NY) # + 1) end_time = time() print_info( "Finished after {:.1f}min.".format( (end_time - start_time)/60 ), "acropolis.scans.BufferedScanner.perform_scan", verbose_level=3 ) return parallel_results
def get_pdi_grids(self): (Tmin, Tmax) = self._sTrg NT = int(log10(Tmax/Tmin)*NT_pd) # Create an array containing all # temperature points ('log spacing') Tr = np.logspace( log10(Tmin), log10(Tmax), NT ) # Create a dictionary to store the pdi # rates for all reactions and temperatures pdi_grids = {rid:np.zeros(NT) for rid in _lrid} start_time = time() print_info( "Calculating non-thermal spectra and reaction rates.", "acropolis.nucl.NuclearReactor.get_pdi_grids", verbose_level=1 ) # Loop over all the temperatures and # calculate the corresponding thermal rates for i, Ti in enumerate(Tr): progress = 100*i/NT print_info( "Progress: {:.1f}%".format(progress), "acropolis.nucl.NuclearReactor.get_pdi_grids", eol="\r", verbose_level=1 ) rates_at_i = self._pdi_rates(Ti) # Loop over the different reactions for rid in _lrid: pdi_grids[rid][i] = rates_at_i[rid] end_time = time() print_info( "Finished after {:.1f}s.".format(end_time - start_time), "acropolis.nucl.NuclearReactor.get_pdi_grids", verbose_level=1 ) # Go get some sun return (Tr, pdi_grids)
def get_matp(self, T): # Generate empty matrices mpdi, mdcy = np.zeros( (_nnuc, _nnuc) ), np.zeros( (_nnuc, _nnuc) ) start_time = time() print_info( "Calculating final transfer matrix.", "acropolis.nucl.MatrixGenerator.get_matp", verbose_level=1 ) nt = 0 # Rows: Loop over all relevant nuclei for nr in range(_nnuc): # Columns: Loop over all relevant nuclei for nc in range(_nnuc): nt += 1 progress = 100*nt/_nnuc**2 print_info( "Progress: {:.1f}%".format(progress), "acropolis.nucl.MatrixGenerator.get_matp", eol="\r", verbose_level=1 ) # Define the kernels for the integration in log-log space Ik_pdi = lambda y: self._pdi_kernel_ij( nr, nc, exp(y) ) * exp(y) Ik_dcy = lambda y: self._dcy_kernel_ij( nr, nc, exp(y) ) * exp(y) # Perform the integration (in log-log space) mpdi[nr, nc] = quad(Ik_pdi, log(self._sTmax), log(T), epsrel=eps, epsabs=0)[0] mdcy[nr, nc] = quad(Ik_dcy, log(self._sTmax), log(T), epsrel=eps, epsabs=0)[0] end_time = time() print_info( "Finished after {:.1f}ms.".format( 1e3*(end_time - start_time) ), "acropolis.nucl.MatrixGenerator.get_matp", verbose_level=1 ) return (mpdi, mdcy)
def get_matp(self, T): # Generate empty matrices mpdi, mdcy = np.zeros( (_nnuc, _nnuc) ), np.zeros( (_nnuc, _nnuc) ) start_time = time() print_info( "Running non-thermal nucleosynthesis.", "acropolis.nucl.MatrixGenerator.get_matp" ) nt = 0 # Rows: Loop over all relevant nuclei for nr in range(_nnuc): # Columns: Loop over all relevant nuclei for nc in range(_nnuc): nt += 1 print_info( "Progress: " + str( int( 1e3*nt/_nnuc**2 )/10 ) + "%", "acropolis.nucl.MatrixGenerator.get_matp", eol="\r" ) # Define the kernels for the integration in log-log space Ik_pdi = lambda y: self._pdi_kernel_ij( nr, nc, exp(y) ) * exp(y) Ik_dcy = lambda y: self._dcy_kernel_ij( nr, nc, exp(y) ) * exp(y) # Perform the integration (in log-log space) mpdi[nr, nc] = quad(Ik_pdi, log(self._sTmax), log(T), epsrel=eps, epsabs=0)[0] mdcy[nr, nc] = quad(Ik_dcy, log(self._sTmax), log(T), epsrel=eps, epsabs=0)[0] end_time = time() print_info( "Finished after " + str( int( (end_time - start_time)*1e4 )/10 ) + "ms." ) return (mpdi, mdcy)