# Set bin width and range bin_width = 0.20 Emax = 20 Nbins = int(np.ceil(Emax / bin_width)) Emax_adjusted = bin_width * Nbins # Trick to get an integer number of bins bins = np.linspace(0, Emax_adjusted, Nbins + 1) # Define list of calculation input files and corresponding label names inputfile = "" # Instantiate figure which we will fill f_rho, ax_rho = plt.subplots(1, 1) # Read energy levels from file levels = smutil.read_energy_levels(inputfile) # Calculate level density rho = smutil.total_level_density(levels, bin_width, Emax) # Plot it ax_rho.step(bins, np.append(0, rho), where='pre', label="Level density") # Make the plot nice ax_rho.set_yscale('log') ax_rho.set_xlabel(r'$E_x \, \mathrm{(MeV)}$') ax_rho.set_ylabel(r'$\rho \, \mathrm{(MeV^{-1})}$') ax_rho.legend() # Show plot plt.show()
def run(self, spe_tbme): # Add some test in case spe_tbme is a Theano tensor? # print type(spe_tbme) # print spe_tbme.ndim # if type(spe_tbme) == "<class 'theano.tensor.var.TensorVariable'>": # print "Oh hello" # Nspetbme = spe_tbme.ndim # else: # Nspetbme = len(spe_tbme) # calc_tbme_matrix = np.zeros((self.Ntot,self.Nspe+self.Ntbme)) calc_dict = {} # print "From smensemble.run(): calc_tbme_matrix.shape =", calc_tbme_matrix.shape SPE = spe_tbme[0:self.Nspe] tbme_template = self.tbme_template # print "SPE =", SPE # TBME = np.copy(self.tbme_template) # print TBME[:,5] # print "spe_tbme =", spe_tbme # print "spe_tbme.shape =", spe_tbme.shape # TBME[:,5] = spe_tbme[self.Nspe:] # TBME_th = th.tensor.stack([tbme_template[0:5,:], spe_tbme[self.Nspe:].reshape((self.Ntbme,1))]) TBME_input1 = th.tensor.dmatrix("TBME_input1") TBME_input1.tag.test_value = self.tbme_template[:, 0:5] # print "TBME_input1.tag.test_value.shape =", TBME_input1.tag.test_value.shape TBME_input2 = th.tensor.dvector("TBME_input2") TBME_input2.tag.test_value = np.random.rand(self.Ntbme) # print "TBME_input2.tag.test_value.shape =", TBME_input2.tag.test_value.shape TBME_th = th.tensor.concatenate( [TBME_input1, TBME_input2.reshape((TBME_input2.shape[0], 1))], axis=1) TBME_values = th.function([TBME_input1, TBME_input2], TBME_th) TBME = TBME_values(tbme_template[:, 0:5], spe_tbme[self.Nspe:]) # Make new directory for each calculation to store everything os.chdir(self.fname_allstepsdir) fname_currentstepdir = "{:d}".format( int((np.abs(spe_tbme) * np.linspace(1, len(spe_tbme) + 1, len(spe_tbme))).sum() * 1e25)) # Each folder is named by the sum of parameter values. # This is to make it identifiable so we don't rerun an # ensemble with the same parameters, e.g. for taking # the gradient. It may be a stupid way to do it, suggestions # welcome. if not os.path.exists(fname_currentstepdir): os.makedirs(fname_currentstepdir) os.chdir(fname_currentstepdir) # Write interaction file: # print self.mass_scaling, self.scaling_A0, self.scaling_p smutil.write_interaction_file_msdict( "interaction.snt", SPE, TBME, self.model_space, self.core, comments="Autogenerated by ShellModelEnsemble class.", mass_scaling=self.mass_scaling, scaling_A0=self.scaling_A0, scaling_p=self.scaling_p) i_level = 0 for nucleusName, nucleusAttr in self.nucleiList.items(): rundirName = "{:s}".format( nucleusName ) # Temporary directory where KSHELL runs for current nucleus, e.g. O18 outputFilename = "summary_{:s}.txt".format( nucleusName ) # The name of the summary file for current nucleus that we want to keep by moving it up # outputFilenameFinal = "summary-{:s}.txt".format(nucleusName) # TODO remove if not needed # nucleusSMC = smcalc.shellmodelcalculation(nucleusName, # A=nucleusAttr["A"], Z=nucleusAttr["Z"], # levels=nucleusAttr["levels"], # truncation=nucleusAttr["truncation"], # core=self.core, model_space=self.model_space, SPE=SPE, # TBME=TBME, kshell_dir=KSHELL_DIR, calc_tbme=True, # ensemble_run=True) nucleusSMC = smcalc.shellmodelcalculation( nucleusName, A=nucleusAttr["A"], Z=nucleusAttr["Z"], levels=nucleusAttr["levels"], truncation=nucleusAttr["truncation"], core=self.core, model_space=self.model_space, SPE=SPE, TBME=TBME, kshell_dir=KSHELL_DIR, calc_tbme=True, ensemble_run=False) # Run calculation and extract spe&tbme expectation values, wrapped in some tests to avoid unnecessary steps: if os.path.isfile( os.path.join(rundirName, outputFilename) ): # If the rundir exists for current nucleus then we must try: # It could be that the step was cancelled in the middle of this nucleus. So we try to catch the exception that the results file is not complete # levels = smutil.read_energy_levels(os.path.join(rundirName, outputFilename)) calc_tbmes = smutil.read_calc_tbme( os.path.join(rundirName, outputFilename), self.tbme_template) calc_E = smutil.read_energy_levels( os.path.join(rundirName, outputFilename)) except: # If exception is raised then it means the output file is incomplete and we need to rerun this nucleus. # nucleusSMC.run(is_mpi=self.is_mpi, n_restart_vec=n_restart_vec, max_lanc_vec=max_lanc_vec) nucleusSMC.run( is_mpi=self.is_mpi ) # n_restart_vec, max_lanc_vec set in initialization for tuning run levels = smutil.read_energy_levels( os.path.join(rundirName, outputFilename)) calc_tbmes = smutil.read_calc_tbme( os.path.join(rundirName, outputFilename), self.tbme_template) calc_E = smutil.read_energy_levels( os.path.join(rundirName, outputFilename)) shutil.copyfile( os.path.join(rundirName, outputFilename), outputFilename ) # Copy results file to current step directory and remove current rundir shutil.rmtree(rundirName) elif os.path.isfile( outputFilename ): # The current nucleus has been completed, and the summary has been copied to outputFilenameFinal, which we then read # levels = smutil.read_energy_levels(outputFilename) calc_tbmes = smutil.read_calc_tbme(outputFilename, self.tbme_template) calc_E = smutil.read_energy_levels(outputFilename) else: # Nothing exists for this nucleus, so we run it from scratch # nucleusSMC.run(is_mpi=self.is_mpi, n_restart_vec=n_restart_vec, max_lanc_vec=max_lanc_vec) nucleusSMC.run( is_mpi=self.is_mpi ) # n_restart_vec, max_lanc_vec set in initialization for tuning run # levels = smutil.read_energy_levels(os.path.join(rundirName, outputFilename)) calc_tbmes = smutil.read_calc_tbme( os.path.join(rundirName, outputFilename), self.tbme_template) calc_E = smutil.read_energy_levels( os.path.join(rundirName, outputFilename)) # Copy summary file to current step directory and remove current rundir shutil.copyfile(os.path.join(rundirName, outputFilename), outputFilename) #shutil.rmtree(rundirName) # May be commented out for debug # Transfer entries in calc_tbmes from current nucleus to total matrix for current run: # print calc_tbmes # for i in range(len(calc_tbmes[:,0])): # calc_tbme_matrix[i_level,:] = calc_tbmes[i,:] # i_level += 1 calc_dict[nucleusName] = { "calc_tbme": calc_tbmes, "calc_E": calc_E } # Make dictionary containing both energy levels and tbme expectation values for current nucleus. # calc_tbme have the same ordering as calc_E by construction # clean up os.chdir( "../../") # Exit current fname_currentstepdir and steps folder # 20180116: Adding an option to remove the complete current fname_currentstepdir, to avoid using a lot of storage. This means all information we store is the PyMC3 trace. Not sure it's the best solution. # shutil.rmtree(os.path.join(self.fname_allstepsdir, fname_currentstepdir)) return calc_dict