def extract_statical_polarizability(label,rtol,atol,path,input_for_alpha,posinp,code,ref): # seek for the field convergence ints=[1e-2,5e-3,1e-3,5e-4,1e-4] alpha_field=D.Dataset(label=label+'(field)',run_dir=path) for f in ints: alpha_field.append_run(id={'f':f},runner=SP.build_alpha_dataset(input=input_for_alpha,\ posinp=posinp,run_dir=path,runner=code,intensity=f)) try: data_field=alpha_field.seek_convergence(rtol=rtol,atol=atol) intensity=data_field[0]['f'] except: print ('Convergence in field not reached') intensity=ints[-1] # seek for the rmult convergence rmult_fine=input_for_alpha['dft']['rmult'][1] rmult_list=map(float,range(int(input_for_alpha['dft']['rmult'][0]),11)) alpha_rmult=D.Dataset(label=label+'(domain)',run_dir=path) for rm in rmult_list: input_for_alpha.set_rmult(coarse=rm,fine=rmult_fine) alpha_rmult.append_run(id={'f':intensity,'rmult':rm},\ runner=SP.build_alpha_dataset(input=input_for_alpha,posinp=posinp,run_dir=path,runner=code,intensity=intensity)) try: import numpy as np data_final=alpha_rmult.seek_convergence(rtol=rtol,atol=atol) AuToA = 0.5291772085**3 alpha_ref_ii = np.array(ref)/AuToA eps = 100.0 * (data_final[1]-alpha_ref_ii)/alpha_ref_ii print ('Relative difference in %',eps.tolist()) except LookupError as e: print ('Convergence in domain not reached',e) data_final=None return {'alpha_convergence': data_final, 'dataset_field': alpha_field, 'dataset_rmult': alpha_rmult}
def extract_statical_polarizability_linearity(label,rtol,atol,path,input_for_alpha,posinp,code,ref): #ints=[1e-2,5e-3,1e-3,5e-4,1e-4] ints= [1e-3,2e-3,3e-3,4e-3,5e-3] #for comparison with the MWL plots results = {} results['alpha_values'] = {} rmult_fine=input_for_alpha['dft']['rmult'][1] rmult_list=map(float,range(int(input_for_alpha['dft']['rmult'][0]),15)) for intensity in ints: # seek for the rmult convergence alpha_rmult=D.Dataset(label=label+'(domain)',run_dir=path) for rm in rmult_list: input_for_alpha.set_rmult(coarse=rm,fine=rmult_fine) alpha_rmult.append_run(id={'f':intensity,'rmult':rm},\ runner=SP.build_alpha_dataset(input=input_for_alpha,posinp=posinp,run_dir=path,runner=code,intensity=intensity)) try: import numpy as np data_final=alpha_rmult.seek_convergence(rtol=rtol,atol=atol) AuToA = 0.5291772085**3 alpha_ref_ii = np.array(ref)/AuToA eps = 100.0 * (data_final[1]-alpha_ref_ii)/alpha_ref_ii print ('Relative difference in %',eps.tolist()) except LookupError as e: print ('Convergence in domain not reached',e) data_final=None results['alpha_values'][intensity] = {'alpha_convergence': data_final, 'dataset_rmult': alpha_rmult} return results
def find_gs_domain(label,rtol,atol,path,input,posinp,code): """ Use the seek_convergence method of Dataset to perform a convergence procedure on the rmult value for a gs computation. """ crmult=input['dft']['rmult'][0] rmult_fine=input['dft']['rmult'][1] rmult_list=map(float,range(int(crmult),11)) seek_for_rmult = D.Dataset(label=label+'(GS)',run_dir=path,posinp=posinp) for rm in rmult_list: input.set_rmult(coarse=rm,fine=rmult_fine) seek_for_rmult.append_run(id={'rmult':rm},runner=code,input=input) input_gs,log_gs=get_converged_input_energy(seek_for_rmult,rtol,atol) return {'input_gs':input_gs, 'dataset_gs': seek_for_rmult,'log_gs':log_gs}
def build_alpha_dataset(**kwargs): """ Create the dataset and append the runs needed to compute the statical polarizability for a specific choice of the input parameters. Set also a postprocessing function to extract the value of alpha. The postprocessing function is set to the :py:func:`eval_alpha` function Args: kwargs['run_dir'] : th run_dir kwargs['intensity'] : the intensity of the field kwargs['input'] : the input file kwargs['posinp'] : the posinp kwargs['runner'] : the instance of SystemCalculator """ from BigDFT import InputActions as A, Datasets as D lbl = 'alpha_' + str(kwargs['intensity']) study = D.Dataset(label=lbl, **kwargs) study.set_postprocessing_function(eval_alpha) #study.set_postprocessing_function(eval_alpha_from_energy) f = kwargs['intensity'] inp = kwargs['input'] hgrids = inp['dft']['hgrids'] gnrm_cv = inp['dft']['gnrm_cv'] for ind, sign in enumerate(['+', '-']): for idir, coord in enumerate(['x', 'y', 'z']): el = np.zeros(3) el[idir] = (1 - 2 * ind) * f inp.apply_electric_field(el.tolist()) idd = { 'hgrids': hgrids, 'gnrm_cv': gnrm_cv, 'rmult': inp['dft']['rmult'][0], 'dir': coord, 'sign': sign, 'F': f } study.append_run(process_id_default_values(idd), runner=kwargs['runner'], input=inp) return study
def build_alpha_dataset(**kwargs): """ Create the dataset and append the runs needed to compute the statical polarizability for a specific choice of the input parameters. Set also a postprocessing function to extract the value of alpha. Args: kwargs['run_dir'] : th run_dir kwargs['intensity'] : the intensity of the field kwargs['input'] : the input file kwargs['posinp'] : the posinp kwargs['ppf'] : the postprocessing function kwargs['runner'] : the instance of SystemCalculator """ lbl = 'alpha_' + str(kwargs['intensity']) study = D.Dataset(label=lbl, run_dir=kwargs['run_dir'], intensity=kwargs['intensity'], posinp=kwargs['posinp']) study.set_postprocessing_function(kwargs['ppf']) f = kwargs['intensity'] inp = kwargs['input'] for ind, sign in enumerate(['+', '-']): for idir, coord in enumerate(['x', 'y', 'z']): el = np.zeros(3) el[idir] = (1 - 2 * ind) * f inp.apply_electric_field(el.tolist()) idd = { 'rmult': inp['dft']['rmult'][0], 'dir': coord, 'sign': sign, 'F': f } study.append_run(id=idd, runner=kwargs['runner'], input=inp) return study
def get_converged_input_energy(dataset,rtol,atol): data=dataset.seek_convergence(atol=atol,rtol=rtol,attribute='energy') return dataset.runs[dataset.names.index(D.name_from_id(data[0]))]['input'],data[1]
def nsp_workflow(alpha_conv=1.0e-2,wf_convergence=1.0e-6,hgrids=0.3,\ rmult_fine=9.0,term_verb=True,data_folder='Data',**kwargs): """ Perform the complete nsp workflow to compute the statical polarizability of a molecule in a specific study(xc+psp) Args: kwargs['molecule'] : the molecule type kwargs['study'] : the tuple (xc,psp) kwargs['code'] : the instance of SystemCalculator """ molecule = kwargs['molecule'] study = kwargs['study'] code = kwargs['code'] code.update_global_options(verbose=term_verb) results = {} if not os.path.isdir(data_folder): os.mkdir(data_folder) os.chdir(data_folder) if not os.path.isdir(molecule): os.mkdir(molecule) os.chdir(molecule) path = study[0] + '-' + study[1] if not os.path.isdir(path): os.mkdir(path) print 'Compute alpha for : ', molecule, study[0], study[1] posinp = Molecules.Molecule(molecule) #rel tol for the gs convergence (using the total energy as control quantity) gs_rtol = 10 * wf_convergence inp = I.Inputfile() inp.set_hgrid(hgrids) set_xc[study](inp, molecule) inp.set_wavefunction_convergence(wf_convergence) if term_verb: print 'Seek for gs convergence' rmult_coarse = [1.0 * i for i in range(3, 12)] data = [] for r in rmult_coarse: gs_study = D.Dataset(label=molecule + '_GS', run_dir=path, posinp=posinp) gs_study.set_postprocessing_function(SP.get_energy) inp.set_rmult(coarse=r, fine=rmult_fine) idd = {'rmult': r} gs_study.append_run(id=idd, runner=code, input=inp) data.append(gs_study) results['gs_conv'] = SP.seek_convergence(rt=gs_rtol,term_verb=term_verb,\ label='rmult',values=rmult_coarse,data=data) if term_verb: print 'Seek for alpha convergence' # field intensity convergence gs_conv = results['gs_conv']['converged_value'] inp.set_rmult(coarse=gs_conv, fine=rmult_fine) if term_verb: print 'Convergence on the field intensity' results['field_conv']=SP.perform_field_convergence(term_verb=term_verb,rt=alpha_conv,\ run_dir=path,input=inp,runner=code,posinp=posinp,ppf=SP.eval_alpha) f = results['field_conv']['converged_value'] # rmult convergence rmult_list = SP.build_rmult_list([gs_conv, rmult_fine]) if term_verb: print 'Convergence on rmult' results['rmult_conv']=SP.perform_rmult_convergence(term_verb=term_verb,\ rt=alpha_conv,run_dir=path,intensity=f,rmult=rmult_list,input=inp,\ runner=code,posinp=posinp,ppf=SP.eval_alpha) os.chdir('../../') return results
def single_study_workflow(alpha_conv=1.0e-2, wf_convergence=1.0e-6, hgrids=0.3, rmult_fine=9.0, term_verb=True, **kwargs): """ Perform the complete workflow to compute the statical polarizability of a specific study(molecule+xc+psp) Args: kwargs['molecule'] : the molecule type kwargs['xc'] : the xc functional kwargs['psp'] : the pseudopotential """ molecule = kwargs['molecule'] xc = kwargs['xc'] psp = kwargs['psp'] study = {} if not os.path.isdir(molecule): os.mkdir(molecule) os.chdir(molecule) path = xc + '-' + psp if not os.path.isdir(path): os.mkdir(path) print '' print 'Compute alpha for : ', molecule, xc, psp posinp = Molecules.Molecule(molecule) gs_rtol = 10 * wf_convergence #rel tol for the gs convergence (using the total energy as control quantity) inp = I.Inputfile() inp.set_hgrid(hgrids) inp.set_xc(xc.upper()) inp.set_wavefunction_convergence(wf_convergence) #gs convergence rmult_coarse = [1.0 * i for i in range(3, 12)] data = [] code = C.SystemCalculator(skip=True, verbose=False) for r in rmult_coarse: gs_study = D.Dataset(label=molecule + '_GS', run_dir=path, posinp=posinp) gs_study.set_postprocessing_function(SP.get_energy) inp.set_rmult(coarse=r, fine=rmult_fine) idd = {'rmult': r} gs_study.append_run(id=idd, runner=code, input=inp) data.append(gs_study) if term_verb: print 'Seek for gs convergence' study['gs_conv'] = SP.seek_convergence(rt=gs_rtol, term_verb=term_verb, label='rmult', values=rmult_coarse, data=data) if term_verb: print 'Seek for alpha convergence' # alpha field intensity convergence conv_val = study['gs_conv']['converged_value'] gslog = 'log-' + data[rmult_coarse.index(conv_val)].names[0] + '.yaml' gs = lf.Logfile(path + os.sep + gslog) inp.set_rmult(gs.log['dft']['rmult']) if term_verb: 'Convergence on the field intensity' study['field_conv'] = SP.perform_field_convergence(term_verb=term_verb, rt=alpha_conv, run_dir=path, input=inp, runner=code, posinp=posinp, ppf=SP.eval_alpha) f = study['field_conv']['converged_value'] # alpha rmult convergence rmult_list = SP.build_rmult_list(gs) if term_verb: 'Convergence on rmult' study['rmult_conv'] = SP.perform_rmult_convergence(term_verb=term_verb, rt=alpha_conv, run_dir=path, intensity=f, rmult=rmult_list, input=inp, runner=code, posinp=posinp, ppf=SP.eval_alpha) os.chdir('../') return study