def __init__(self, template_filename, target_filename, common_dir, uncertain_params, cpo_name, link_xmlfiles=False): # Check that user has specified the objests to use as template if template_filename is None: msg = ("CPOEncoder must be given 'template_filename': a CPO file.") logging.error(msg) raise RuntimeError(msg) self.template_filename = template_filename self.target_filename = target_filename self.common_dir = common_dir self.uncertain_params = uncertain_params self.cpo_name = cpo_name self.link_xmlfiles = link_xmlfiles self.fixture_support = True # The CPO object cpo_filename = os.path.join(common_dir, template_filename) self.cpo_core = read(cpo_filename, cpo_name) # Mapping with cpo file # TODO move to switcher tools routine self.mapper = { "Te_boundary" : self.cpo_core.te.boundary.value[0], "Ti_boundary" : self.cpo_core.ti.boundary.value[0][0] }
def build_cpo(filelist, codename, cponame, cpoarray): #from IPython.parallel import Client #c = Client() #csize = len(c.ids) #print 'exec on '+str(csize)+' processes' l = len(filelist) for i in range(l): cpoarray[i] = ascii_cpo.read(filelist[i], cponame, cpoarray[i])
def get_flux_index(corep_file, coret_file): corep = read(corep_file, 'coreprof') coret = read(coret_file, 'coretransp') # rho_tor_norm_transp_flux rt = coret.values[0].rho_tor_norm n_flux = len(rt) print('rho_cores = ', rt) # rho_tor_norm vector in coreprof r = corep.rho_tor_norm # the closest rho_tor in coreprof rp = [r.flat[np.abs(r - rt[i]).argmin()] for i in range(n_flux)] print('rho_corep = ', rp) # the correponding index idx = [list(r).index(rp[i]) for i in range(n_flux)] return idx
def parse_sim_output(self, run_info={}): out_path = self._get_output_path(run_info, self.target_filename) # The CPO object cpo_core = read(out_path, self.cpo_name) # Get Quantity of Intersets qoi_values = self._get_qoi_values(cpo_core) quoi_dict = {} for qoi in self.output_columns: quoi_dict.update({qoi: qoi_values[qoi]}) # Output data frame data = pd.DataFrame(quoi_dict) return data
def get_cpo_from_dir(path, codename, cponame, time_pattern): """ Read CPO files corresponding to pattern in order to build and return a time serie. \nParameters ---------- path : str Path to the directory containing CPO files codename : str Name of the code which produced the CPO cponame : str Name of the CPO time_pattern : str Pattern to match the time slice part of the cpo file e.g: 'codename_cponame_timeslice\.cpo' where timeslice='0...' for slices between 0 and 999 \nReturns ------- cpo_list : CPO array """ db = ual.itm() prevdir = os.getcwd() os.chdir(path) ll = os.listdir('.') ll.sort() pattern = re.compile(codename+'_'+cponame+'_'+time_pattern+'\.cpo') lcpo = pattern.findall(ll.__str__()) lcpolen = len(lcpo) cpoobj = getattr(db,cponame+'Array') cpoobj.resize(lcpolen) cpoarr = getattr(cpoobj,'array') for i in range(lcpolen): cpoarr[i] = ascii_cpo.read(lcpo[i],cponame) os.chdir(prevdir) return cpoobj
default_campaign_dir_prefix='ETS_Campaign_') campaign_dir = ets_campaign.campaign_dir # Copy xml files needed in the ETS code os.system("mkdir " + campaign_dir + "/workflows") os.system("cp ../../workflows/ets.xml " + campaign_dir + "/workflows") os.system("cp ../../workflows/ets.xsd " + campaign_dir + "/workflows") # Copy CPO files in common directory common_dir = campaign_dir + "/common/" os.system("cp " + cpo_dir + "/*.cpo " + common_dir) # Get uncertain parameters distrubutions coret_file = common_dir + "ets_coretransp_in.cpo" coret = read(coret_file, "coretransp") diff_eff = coret.values[0].te_transp.diff_eff list_dist = [cp.Normal(diff_eff[i], 0.2 * diff_eff[i]) for i in range(4)] # Define the parameters dictionary for i in range(4): ets_campaign.vary_param(uncert_params[i], dist=list_dist[i]) # Create the sampler ets_sampler = uq.elements.sampling.PCESampler(ets_campaign) # Generate runs ets_campaign.add_runs(ets_sampler) ets_campaign.populate_runs_dir() # Execute runs
# Add the BC app (automatically set as current app) my_campaign.add_app(name="uq_ets_chease", params=params, encoder=encoder, decoder=decoder) # Create a collation element for this campaign collater = uq.collate.AggregateSamples(average=False) my_campaign.set_collater(collater) # Get uncertain parameters values #coret_file = common_dir + "ets_coretransp_in.cpo" #coret = read(coret_file, "coretransp") #diff_eff = coret.values[0].te_transp.diff_eff corep_file = common_dir + "ets_coreprof_in.cpo" corep = read(corep_file, "coreprof") Te_boundary = corep.te.boundary.value[0] Ti_boundary = corep.ti.boundary.value[0][0] # Create the sampler #vary = { uparams[k]: cp.Normal(diff_eff[k], 0.2*diff_eff[k]) for k in range(4)} vary = { uparams[0]: cp.Normal(Te_boundary, 0.2 * Te_boundary), uparams[1]: cp.Normal(Ti_boundary, 0.2 * Ti_boundary) } my_sampler = uq.sampling.PCESampler(vary=vary, polynomial_order=4) # Associate the sampler with the campaign my_campaign.set_sampler(my_sampler)
pctl_te = results['percentiles']['Te'] stot_te = results['sobols_total']['Te'] stats_ti = results['statistical_moments']['Ti'] pctl_ti = results['percentiles']['Ti'] stot_ti = results['sobols_total']['Ti'] print('>>> Ellapsed time: ', time.time() - time0) # Graphics for Descriptive satatistics __PLOTS = False # If True create plots subfolder under outputs folder if __PLOTS: from utils import plots uparams_names = list(params.keys()) corep = read(os.path.join(cpo_dir, "ets_coreprof_in.cpo"), "coreprof") rho = corep.rho_tor uparams_names = list(params.keys()) test_case = cpo_dir.split('/')[-1] plots.plot_stats_pctl(rho, stats_te, pctl_te, xlabel=r'$\rho_{tor} ~ [m]$', ylabel=r'$Te$', ftitle='Te profile ('+test_case+')', fname='plots/Te_STAT_'+test_case) plots.plot_sobols(rho, stot_te, uparams_names, ftitle=' Total-Order Sobol indices - QoI: Te', fname='plots/Te_SA_'+test_case) plots.plot_stats_pctl(rho, stats_ti, pctl_ti,
#st_te = results['sobol_total_order']['te'] stats_ti = results['statistical_moments']['ti'] pctl_ti = results['percentiles']['ti'] #s1_ti = results['sobol_first_order']['ti'] #st_ti = results['sobol_total_order']['ti'] # Elapsed time end_time = time.time() print('======= Elapsed times') print('- EXEC : ', exec_time / 60.) print('- TOTAL : ', (end_time - start_time) / 60.) # Graphics for descriptive satatistics corep_file = common_dir + '/ets_coreprof_in.cpo' corep = read(corep_file, 'coreprof') rho = corep.rho_tor plots.plot_stats_pctl(rho, stats_te, pctl_te, xlabel=r'$\rho_{tor} ~ [m]$', ylabel=r'$T_e ~ [eV]$', ftitle='UQ: Te profile (Uncertenties in Ion sources)', fname='figs/te_sr_ions-stats.png') plots.plot_stats_pctl(rho, stats_ti, pctl_ti, xlabel=r'$\rho_{tor} ~ [m]$', ylabel=r'$T_i ~ [eV]$',
header=0) # Add the ETS app (automatically set as current app) my_campaign.add_app(name="uq_ets", params=params, encoder=encoder, decoder=decoder ) # Create a collation element for this campaign collater = uq.collate.AggregateSamples(average=False) my_campaign.set_collater(collater) # Get uncertain parameters values coret_file = common_dir + "ets_coretransp_in.cpo" coret = read(coret_file, "coretransp") diff_eff = coret.values[0].te_transp.diff_eff corep_file = common_dir + "ets_coreprof_in.cpo" corep = read(corep_file, "coreprof") Te_boundary = corep.te.boundary.value[0] Ti_boundary = corep.ti.boundary.value[0][0] # Create the sampler print('Create the sampler') vary = {uncertain_params[k]: cp.Normal(diff_eff[k], 0.2*diff_eff[k]) for k in range(4)} vary.update({ uncertain_params[4]: cp.Normal(Te_boundary, 0.2*Te_boundary), uncertain_params[5]: cp.Normal(Ti_boundary, 0.2*Ti_boundary) })
def ets_test(uncert_params): # The ets_run executable (to run the ets model) bin_file = "../bin/" + SYS + "/ets_run " # Input/Output template input_json = "inputs/ets_in.json" output_json = os.path.join(TMP_DIR, "out_ets.json") # Initialize Campaign object ets_campaign = uq.Campaign(name='ETS_Campaign', state_filename=input_json, workdir=TMP_DIR, default_campaign_dir_prefix='UQP2_ETS_') campaign_dir = ets_campaign.campaign_dir # Copy xml files needed in the ETS code os.system("mkdir " + campaign_dir + "/workflows") os.system("cp ../../workflows/ets.xml " + campaign_dir + "/workflows") os.system("cp ../../workflows/ets.xsd " + campaign_dir + "/workflows") # Copy CPO files in common directory common_dir = campaign_dir + "/common/" os.system("cp " + CPO_DIR + "/*.cpo " + common_dir) # Get uncertain parameters distrubutions coret_file = common_dir + "ets_coretransp_in.cpo" coret = read(coret_file, "coretransp") diff_eff = coret.values[0].te_transp.diff_eff list_dist = [cp.Normal(diff_eff[i], 0.2 * diff_eff[i]) for i in range(4)] # Define the parameters dictionary for i in range(4): ets_campaign.vary_param(uncert_params[i], dist=list_dist[i]) # Create the sampler ets_sampler = uq.elements.sampling.PCESampler(ets_campaign) # Generate runs ets_campaign.add_runs(ets_sampler) ets_campaign.populate_runs_dir() # Execute runs cmd = bin_file + common_dir + " ets_input.nml" ets_campaign.apply_for_each_run_dir(uq.actions.ExecuteLocal(cmd)) # Aggregate the results from all runs. output_filename = ets_campaign.params_info['out_file']['default'] output_columns = ['c'] aggregate = uq.elements.collate.AggregateSamples( ets_campaign, output_filename=output_filename, output_columns=output_columns, header=0, ) aggregate.apply() # Analysis analysis = uq.elements.analysis.PCEAnalysis(ets_campaign, value_cols=output_columns) dist, cov = analysis.apply() # Results cov_mnat = cov["c"] return dist["c"]
return stats, sobols # Main if __name__ == "__main__": # Uncertain parameters uncert_params_in = ["D1", "D2", "D3", "D4"] # The 1st box: ETS dist = ets_test(uncert_params_in) # The 2nd box: CHEASE stats, sobols = chease_test(dist) # PLOTS eq_file = CPO_DIR + "/ets_equilibrium_in.cpo" eq = read(eq_file, "equilibrium") rho = eq.profiles_1d.rho_tor plots.plot_stats(rho, stats, xlabel=r'$\rho_{tor} ~ [m]$', ylabel=r'<B>', ftitle='B_av profile', fname='figs/b_prof.png') plot.plot_sobols_4(rho, sobols, uncert_params_in, 'b_av')
if range_pattern == None: range_pattern = '....' pattern = re.compile(codename + '_' + cponame + '_' + range_pattern + '\.cpo') lcpo = pattern.findall(ll.__str__()) lcpolen = len(lcpo) cpoobj = getattr(db, cponame + 'Array') cpoobj.resize(lcpolen) cpoarr = getattr(cpoobj, 'array') logger.info("Reading CPO %s from %s", cponame, codename) for i in range(lcpolen): logger.info("slice #%i", i) cpoarr[i] = read(lcpo[i], cponame) logger.info("Writing CPO into database") getattr(cpoobj, "setExpIdx")(db.expIdx) cpoobj.put() ########################## STORE PROVENANCE DATA ############################ logger.critical("Saving provenance data into topinfo CPO") # todo: get real name instead of username db.topinfo.dataprovider = usr db.topinfo.description = entry.get("DESCRIPTION") db.topinfo.firstputdate = datetime.now().strftime( '%Y-%m-%d (%Hh%Mm%Ss)') db.topinfo.dataversion = ver workflow = simulation.get('WORKFLOW')
import pylab from ascii_cpo import read time = [] te_1 = [] te_2 = [] te_3 = [] te_4 = [] te_5 = [] te_6 = [] print("reading coreprof and coretransp CPOs") for i in range(50): corep = read('cpos/ets_coreprof_'+str(i+1).zfill(4)+'.cpo', 'coreprof') time.append(corep.time) te = corep.te.value te_1.append(te[0]) te_2.append(te[19]) te_3.append(te[39]) te_4.append(te[59]) te_5.append(te[79]) te_6.append(te[99]) r = corep.rho_tor_norm fig = pylab.figure(figsize=(12,9)) ax1 = pylab.subplot(111) ax1.set_xlabel('time') ax1.set_ylabel(r'$t_e \quad [eV]$')
def plot_dv(args, step, ext='pdf', show=True): if args.title == None: title = "Static=0-20, Lim=0.2/0.09, Tau=0.01, Step=99" else: title = args.title sstep = str(step).zfill(4) corep = ascii_cpo.read(args.path + "/ets_coreprof_" + sstep + ".cpo", "coreprof") coretw = ascii_cpo.read( args.path + "/ets_coretransp-work_in_" + sstep + ".cpo", "coretransp") coretdv = ascii_cpo.read( args.path + "/imp4dv_coretransp_" + sstep + ".cpo", "coretransp") coretgem = ascii_cpo.read(args.path + "/gem_coretransp_" + sstep + ".cpo", "coretransp") fulltitle = title + ", Step=" + sstep if args.Te: f, (ax1, ax2) = pylab.subplots(2, figsize=(8, 12), sharex=True, sharey=False) ax1.set_title(fulltitle) ax1.plot(corep.rho_tor_norm, coretw.values[0].te_transp.diff_eff, 'b', label="used in ETS") ax1.plot(coretdv.values[0].rho_tor_norm, coretdv.values[0].te_transp.diff_eff, 'bo', label="IMP4dv coefs") ax1.plot(coretgem.values[0].rho_tor_norm, coretgem.values[0].te_transp.diff_eff, 'g^', label="GEM coefs") ax1.set_ylabel("Te/Diff_eff") ax1.legend() ax2.plot(corep.rho_tor_norm, coretw.values[0].te_transp.vconv_eff, 'b', label="used in ETS") ax2.plot(coretdv.values[0].rho_tor_norm, coretdv.values[0].te_transp.vconv_eff, 'bo', label="IMP4dv coefs") ax2.plot(coretgem.values[0].rho_tor_norm, coretgem.values[0].te_transp.vconv_eff, 'g^', label="GEM coefs") ax2.set_xlabel("rho_tor_norm") ax2.set_ylabel("Te/Vconv_eff") f.subplots_adjust(hspace=0) #pylab.tight_layout() if show: pylab.show() if args.out != None: f.savefig("Te_" + sstep + "_" + args.out + "." + ext) if args.Ti: f, (ax1, ax2) = pylab.subplots(2, figsize=(8, 12), sharex=True, sharey=False) ax1.set_title(fulltitle) ax1.plot(corep.rho_tor_norm, coretw.values[0].ti_transp.diff_eff, 'b', label="used in ETS") ax1.plot(coretdv.values[0].rho_tor_norm, coretdv.values[0].ti_transp.diff_eff, 'bo', label="IMP4dv coefs") ax1.plot(coretgem.values[0].rho_tor_norm, coretgem.values[0].ti_transp.diff_eff, 'g^', label="GEM coefs") ax1.set_ylabel("Ti/Diff_eff") ax1.legend() ax2.plot(corep.rho_tor_norm, coretw.values[0].ti_transp.vconv_eff, 'b', label="used in ETS") ax2.plot(coretdv.values[0].rho_tor_norm, coretdv.values[0].ti_transp.vconv_eff, 'bo', label="IMP4dv coefs") ax2.plot(coretgem.values[0].rho_tor_norm, coretgem.values[0].ti_transp.vconv_eff, 'g^', label="GEM coefs") ax2.set_xlabel("rho_tor_norm") ax2.set_ylabel("Ti/Vconv_eff") f.subplots_adjust(hspace=0) #pylab.tight_layout() if show: pylab.show() if args.out != None: f.savefig("Ti_" + sstep + "_" + args.out + "." + ext)
import pylab import ascii_cpo corep = ascii_cpo.read("ets_coreprof_in.cpo", "coreprof") pylab.figure(1) pylab.suptitle("Initial profiles for JET#92436/270") pylab.subplot(321) pylab.plot(corep.rho_tor_norm, corep.profiles1d.q.value) #pylab.xlabel("rho_tor_norm") pylab.ylabel("q") pylab.subplot(322) pylab.plot(corep.rho_tor_norm, corep.profiles1d.pr_th.value) #pylab.xlabel("rho_tor_norm") pylab.ylabel("pr_th") pylab.subplot(323) pylab.plot(corep.rho_tor_norm, corep.te.value) #pylab.xlabel("rho_tor_norm") pylab.ylabel("te") pylab.subplot(324) pylab.plot(corep.rho_tor_norm, corep.ti.value) #pylab.xlabel("rho_tor_norm") pylab.ylabel("ti") pylab.subplot(325) pylab.plot(corep.rho_tor_norm, corep.ne.value) pylab.xlabel("rho_tor_norm")