def equil_and_relax(T, fname="", np_layer=0, nptype="spherical"): bc = init_bc(50) mc = Montecarlo(bc.atoms, T) print ("Running at temperature {}K. Initializing from {}".format(T, fname)) layer_str = "-".join((str(item) for item in np_layers[np_layer])) run_identifier = "{}K_layer{}".format(T, layer_str) if fname != "": # Initialize atoms object from file from ase.io import read atoms = read(fname) symbs = [atom.symbol for atom in atoms] mc.set_symbols(symbs) run_identifier = "{}K_{}".format(T, atoms.get_chemical_formula()) else: if nptype == "spherical": # Initialize by setting a nano particle at the center nanop = get_nanoparticle(layer=np_layer) elif nptype == "cubic": nanop = get_cubic_nano_particle(layer=np_layer) run_identifier += "cubic" else: raise ValueError("Unknown type {}".format(nanop)) symbs = insert_nano_particle(bc.atoms.copy(), nanop) mc.set_symbols(symbs) print("Chemical formula: {}".format(mc.atoms.get_chemical_formula())) nsteps = int(4E7) camera = Snapshot(atoms=mc.atoms, trajfile=workdir+"/snapshots_equil{}.traj".format(run_identifier)) energy_evol = EnergyEvolution(mc) mc_backup = MCBackup(mc, backup_file=workdir+"/mc_backup{}.pkl".format(run_identifier)) mc.attach(energy_evol, interval=100000) mc.attach(camera, interval=nsteps/20) mc.attach(mc_backup, interval=500000) mc.runMC(steps=nsteps, equil=False) write(workdir+"/equillibriated600K.xyz", mc.atoms)
def pure_phase_entropy(small_bc): from cemc.mcmc import Montecarlo from ase.clease.tools import wrap_and_sort_by_position from ase.io import read from cemc.mcmc import SiteOrderParameter, Snapshot import dataset T = [1, 5, 10, 20, 30, 40, 50, 75, 100, 150, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, 420, 440, 460, 480, 500] calc = get_ce_calc(small_bc, kwargs, ecis, size=[10, 10, 10], db_name="data/db10x10x10Al3Mg.db") bc = calc.BC bc.atoms.set_calculator(calc) atoms = read("data/al3mg_template.xyz") atoms = wrap_and_sort_by_position(atoms) symbs = [atom.symbol for atom in atoms] calc.set_symbols(symbs) site_order = SiteOrderParameter(bc.atoms) db = dataset.connect("sqlite:///data/pure_al3mg3.db") syst = db["thermodynamic"] camera = Snapshot(trajfile="data/pure_phase_entropy.traj", atoms=bc.atoms) for temp in T: print("Current temperature: {}K".format(temp)) site_order.reset() mc = Montecarlo(bc.atoms, temp) mc.attach(site_order) mc.attach(camera, interval=50000) equil_param = {"window_length": 100000, "mode": "fixed"} mc.runMC(mode="fixed", steps=500000, equil=True, equil_params=equil_param) mean, stddev = site_order.get_average() thermo = mc.get_thermodynamic() thermo["site_order"] = mean thermo["site_order_std"] = stddev syst.insert(thermo)
def cluster_entropy(mc, size=8): from ase.io import read from cemc.mcmc import EnergyEvolution import dataset from cemc.mcmc import Snapshot T = [1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90, 100, 150, 200, 220, 240, 260, 280, 300, 320, 340, 360, 380, 400, 420, 440, 460, 480, 500] atoms = read("data/cluster_struct_new_trialmove/cluster{}_all.cif".format(size)) symbs = [atom.symbol for atom in atoms] mc.set_symbols(symbs) energy_evol = EnergyEvolution(mc) mc.attach(energy_evol, interval=10000) db = dataset.connect("sqlite:///{}/heat_almg_cluster_size.db".format(folder, size)) syst = db["thermodynamic"] energy_evol_tab = db["energy_evolution"] camera = Snapshot(trajfile=folder+"/cluster_entropy_size{}.traj".format(size), atoms=mc.atoms) mc.attach(camera, interval=50000) for temp in T: print("Temperature: {}".format(temp)) mc.reset() energy_evol.reset() mc.T = temp mc.runMC(steps=500000) thermo = mc.get_thermodynamic() thermo["size"] = size uid = syst.insert(thermo) rows = [] for E in energy_evol.energies: rows.append({"uid": uid, "energy": E}) energy_evol_tab.insert_many(rows)
def main(): energies = [] sizes = list(range(1, 6)) atoms = get_atoms() two_atoms(atoms) exit() insert_np(6, atoms) mc = Montecarlo(atoms, 0.1) camera = Snapshot(atoms=mc.atoms, trajfile="/work/sophus/nuc_cluster.traj") db = dataset.connect("sqlite:////work/sophus/mgsi_nuc_barrier_kamijo.db") tbl = db["systems"] mc.attach(camera, interval=100 * len(atoms)) num_mg = sum(1 for atom in atoms if atom.symbol == "Mg") while num_mg > 2: print(atoms.get_chemical_formula()) mc.runMC(mode="fixed", equil=False, steps=100 * len(atoms)) thermo = mc.get_thermodynamic() tbl.insert(thermo) # Remove one Mg atom and one Si atom symbols = [a.symbol for a in atoms] for i in range(20): i = symbols.index("Si") symbols[i] = "Al" i = symbols.index("Mg") symbols[i] = "Al" mc.set_symbols(symbols) num_mg = sum(1 for atom in atoms if atom.symbol == "Mg")
def main(): atoms = atoms_with_calc(50) # mc = SoluteChainMC(atoms, 350, cluster_elements=["Mg", "Si"], # cluster_names=["c2_01nn_0"]) T = [10] snapshot = Snapshot(trajfile="data/solute_chain{}_nostrain.traj".format(T[0]), atoms=atoms) first = True nano_part = get_nanoparticle() for temp in T: print("Current temperature {}".format(T)) mc = FixedNucleusMC( atoms, temp, network_name=["c2_01nn_0"], network_element=["Mg", "Si"], max_constraint_attempts=1E6) backup = MCBackup(mc, overwrite_db_row=False, db_name="data/mc_solute_no_strain.db") mc.attach(backup, interval=20000) strain = get_strain_observer(mc) mc.add_bias(strain) if first: first = False #mc.grow_cluster({"Mg": 1000, "Si": 1000}) symbs = insert_nano_particle(atoms.copy(), nano_part) mc.set_symbols(symbs) tag_by_layer_type(mc.atoms) cnst = ConstrainElementByTag(atoms=mc.atoms, element_by_tag=[["Mg", "Al"], ["Si", "Al"]]) mc.add_constraint(cnst) #mc.build_chain({"Mg": 500, "Si": 500}) fix_layer = FixEdgeLayers(thickness=5.0, atoms=mc.atoms) mc.add_constraint(fix_layer) mc.attach(snapshot, interval=20000) mc.runMC(steps=19000, init_cluster=False)
def main(): atoms = get_atoms(cubic=True) T = 1500 mc = Montecarlo(atoms, T) mc.insert_symbol_random_places("Mg", num=400, swap_symbs=["Al"]) mc.insert_symbol_random_places("Si", num=400, swap_symbs=["Al"]) snap = Snapshot(trajfile="data/mc_digi{}K.traj", atoms=atoms) mc.attach(snap, interval=50000) mc.runMC(steps=500000) T = 293 mc = Montecarlo(atoms, T) snap = Snapshot(trajfile="data/mc_digi{}K.traj", atoms=atoms) mc.attach(snap, interval=50000) mc.runMC(steps=500000)
def run(N, use_bias): #network_name=["c2_4p050_3", "c2_2p864_2"] bc = init_bc(N) mc = FixedNucleusMC( bc.atoms, T, network_name=["c2_2p864_2"], network_element=["Mg", "Si"], mpicomm=comm, max_constraint_attempts=1E6) #mc.max_allowed_constraint_pass_attempts = 1 nanop = get_nanoparticle() symbs = insert_nano_particle(bc.atoms.copy(), nanop) mc.set_symbols(symbs) mc.init_cluster_info() formula = "(I1+I2)/(2*I3)" # Needle #formula = "2*I1/(I2+I3)" # Plate inert_init = InertiaCrdInitializer( fixed_nucl_mc=mc, matrix_element="Al", cluster_elements=["Mg", "Si"], formula=formula) inert_rng_constraint = InertiaRangeConstraint( fixed_nuc_mc=mc, inertia_init=inert_init, verbose=True) if use_bias: bias = InertiaBiasPotential.load(fname=inertia_bias_file) bias.inertia_range = inert_rng_constraint mc.add_bias(bias) nsteps = 100000 if rank == 0: snap = Snapshot(atoms=bc.atoms, trajfile="{}/inertia.traj".format(workdir)) mc.attach(snap, interval=100000) # reac_path = ReactionPathSampler( # mc_obj=mc, react_crd=[0.05, 0.9], react_crd_init=inert_init, # react_crd_range_constraint=inert_rng_constraint, n_windows=30, # n_bins=10, data_file=h5file) # reac_path.run(nsteps=nsteps) # reac_path.save() inert_rng_constraint.range = [0.0, 0.9] fix_layer = FixEdgeLayers(thickness=5.0, atoms=mc.atoms) mc.add_constraint(inert_rng_constraint) mc.add_constraint(fix_layer) reac_path = AdaptiveBiasReactionPathSampler( mc_obj=mc, react_crd=[0.0, 0.9], react_crd_init=inert_init, n_bins=100, data_file="{}/adaptive_bias.h5".format(workdir), mod_factor=1E-5, delete_db_if_exists=True, mpicomm=None, db_struct="{}/adaptive_bias_struct.db".format(workdir)) reac_path.run() reac_path.save()
def main(size, T): atoms = create_surface(size) atoms = wrap_and_sort_by_position(atoms) conc_args = { "conc_ratio_min_1": [[64, 0, 0]], "conc_ratio_max_1": [[24, 40, 0]], "conc_ratio_min_2": [[64, 0, 0]], "conc_ratio_max_2": [[22, 21, 21]] } kwargs = { "crystalstructure": "fcc", "a": 4.05, "size": [4, 4, 4], "basis_elements": [["Al", "Mg", "Si"]], "conc_args": conc_args, "db_name": "data/almgsi.db", "max_cluster_size": 4 } ceBulk = BulkCrystal(**kwargs) eci_file = "data/almgsi_fcc_eci_newconfig.json" with open(eci_file, 'r') as infile: ecis = json.load(infile) db_name = "large_cell_db{}x{}x{}.db".format(size[0], size[1], size[2]) calc = get_ce_calc(ceBulk, kwargs, ecis, size=size, db_name=db_name) ceBulk = calc.BC ceBulk.atoms.set_calculator(calc) mc = Montecarlo(ceBulk.atoms, T) symbs = [atom.symbol for atom in atoms] mc.set_symbols(symbs) # Write a copy of the current atoms object from ase.io import write shape_str = "-".join((str(item) for item in size)) uid_str = "{}K_{}".format(T, shape_str) write(WORKDIR + "initial_config{}.xyz".format(uid_str), mc.atoms) backup = MCBackup(mc, backup_file=WORKDIR + "backup{}.xyz".format(uid_str), db_name=WORKDIR + "mc_surface.db") camera = Snapshot(atoms=mc.atoms, trajfile=WORKDIR + "surface{}.traj".format(uid_str)) evol = EnergyEvolution(mc) nsteps = int(10E6) mc.attach(backup, interval=100000) mc.attach(camera, interval=int(nsteps / 20)) mc.attach(evol, interval=10 * len(mc.atoms)) mc.runMC(mode="fixed", steps=nsteps, equil=False) write(WORKDIR + "final_config{}.xyz".format(uid_str), mc.atoms)
def gs_mgsi(): atoms = get_atoms(cubic=True) symbs = ["Mg" for _ in range(len(atoms))] for i in range(int(len(symbs) / 2)): symbs[i] = "Si" atoms.get_calculator().set_symbols(symbs) T = [ 1500, 1400, 1300, 1200, 1100, 1000, 900, 800, 600, 500, 400, 300, 200, 100, 50 ] snap = Snapshot(trajfile="mgsi_gs_search.traj", atoms=atoms) for temp in T: print("Temperature {}K".format(temp)) mc = Montecarlo(atoms, temp) mc.attach(snap, interval=10 * len(atoms)) mc.runMC(mode="fixed", steps=100 * len(atoms), equil=False)
def run_huge(): conc_args = { "conc_ratio_min_1":[[64,0,0]], "conc_ratio_max_1":[[24,40,0]], "conc_ratio_min_2":[[64,0,0]], "conc_ratio_max_2":[[22,21,21]] } kwargs = { "crystalstructure":"fcc", "size":[4,4,4], "basis_elements":[["Al","Mg","Si"]], "conc_args":conc_args, "db_name":"some_db.db", "max_cluster_size":4, "a":4.05, "ce_init_alg":"fast" } ceBulk = BulkCrystal( **kwargs ) with open(eci_file, 'r') as infile: eci = json.load(infile) calc = get_ce_calc(ceBulk, kwargs, eci=eci, size=[50,50,50]) calc.BC.atoms.set_calculator(calc) print(calc.BC.basis_functions) comp = { "Al":0.9, "Mg":0.05, "Si":0.05 } calc.set_composition(comp) T = 150 T = [2000, 1500, 1200, 1000, 800, 600, 400, 293] camera = Snapshot(trajfile=traj_file, atoms=calc.BC.atoms) for temp in T: print("Current temperature {}K".format(temp)) mc = Montecarlo(calc.BC.atoms, temp) mc.attach(camera, interval=125000*20) mc.runMC(mode="fixed", steps=125000*100, equil=False)
def run(T,mg_conc): conc_args = { "conc_ratio_min_1":[[1,0]], "conc_ratio_max_1":[[0,1]], } kwargs = { "crystalstructure":"fcc", "a":4.05, "size":[4,4,4], "basis_elements":[["Al","Mg"]], "conc_args":conc_args, "db_name":"data/temporary_bcdb.db", "max_cluster_size":4 } ceBulk = BulkCrystal( **kwargs ) print (ceBulk.basis_functions) eci_file = "data/ce_hydrostatic.json" with open( eci_file, 'r' ) as infile: ecis = json.load( infile ) print (ecis) #calc = CE( ceBulk, ecis, size=(3,3,3) ) calc = get_ce_calc( ceBulk, kwargs, ecis, size=[10,10,10], free_unused_arrays_BC=True ) ceBulk = calc.BC ceBulk.atoms.set_calculator( calc ) comp = { "Al":1.0-mg_conc, "Mg":mg_conc } calc.set_composition(comp) mc_obj = Montecarlo( ceBulk.atoms, T, mpicomm=comm ) pairs = PairCorrelationObserver( calc ) network = NetworkObserver( calc=calc, cluster_name="c2_1414_1", element="Mg", nbins=100 ) mode = "fixed" camera = Snapshot( "data/precipitation.traj", atoms=ceBulk.atoms ) mc_obj.attach( camera, interval=10000 ) mc_obj.attach( pairs, interval=1 ) mc_obj.attach( network, interval=500 ) mc_obj.runMC( mode=mode, steps=10000, equil=True ) pair_mean = pairs.get_average() pair_std = pairs.get_std() if ( rank == 0 ): data = { "pairs":pair_mean, "pairs_std":pair_std, "mg_conc":mg_conc, "temperature":T } pairfname = "data/precipitation_pairs/precipitation_pairs_{}_{}K.json".format(int(1000*mg_conc),int(T)) with open(pairfname,'w') as outfile: json.dump(data,outfile,indent=2, separators=(",",":")) print ( "Thermal averaged pair correlation functions written to {}".format(pairfname) ) atoms = network.get_atoms_with_largest_cluster() data = network.get_statistics() # This collects the histogram data from all processors size,occurence = network.get_size_histogram() data["histogram"] = {} data["histogram"]["size"] = size.tolist() data["histogram"]["occurence"] = occurence.tolist() data["temperature"] = T data["mg_conc"] = mg_conc cluster_fname = "data/cluster_statistics_{}_{}K.json".format(int(1000*mg_conc),int(T)) atoms_fname = "data/largest_cluster_{}_{}K.cif".format( int(1000*mg_conc), int(T) ) if ( rank == 0 ): print (occurence) if ( rank == 0 ): try: with open( cluster_fname,'r') as infile: data = json.load(infile) old_size = np.array(data["histogram"]["size"]) old_hist = np.array(data["histogram"]["occurence"]) if ( np.allclose(old_size,size) ): occurence += old_hist data["histogram"]["occurence"] = occurence.tolist() except Exception as exc: print (str(exc)) with open( cluster_fname, 'w' ) as outfile: json.dump( data, outfile, indent=2, separators=(",",":") ) print ("Cluster statistics written to {}".format(cluster_fname) ) write( atoms_fname, atoms ) print ("Atoms with largest cluster written to {}".format(atoms_fname)) #view(atoms) #plt.plot( network.size_histogram, ls="steps") #plt.show() print ("Proc: {} reached final barrier".format(rank)) comm.barrier()
from cemc.mcmc import SiteOrderParameter, EnergyEvolution, EnergyHistogram from cemc.mcmc import MCBackup # The Correlation Function Tracker computes the thermodynamic # average of all the correlation functions corr_func_obs = CorrelationFunctionTracker(calc) # The PairCorrelationObserver computes the thermodynamic average of all # the pair interactions. # If only the pair interactions are of interest, this observer should be # preferred over the CorrelationFunctionTracker pair_obs = PairCorrelationObserver(calc) # This function takes a snap shot of the system and collects # them in a trajectory file snapshot = Snapshot( trajfile="demo.traj", atoms=ceBulk.atoms ) # This observer stores the structure having the lowest energy low_en = LowestEnergyStructure( calc, mc_obj ) # This observer tracks networks of a certain atom type network_obs = NetworkObserver( calc=calc, cluster_name=[get_example_network_name(ceBulk)], element=["Mg"]) # This tracks the average number of sites that where the symbol as changed site_order = SiteOrderParameter(ceBulk.atoms) # Energy evolution. Useful to check if the system has been equilibrated energy_evol = EnergyEvolution(mc_obj) # Energy histogram: Sample a histogram of the visited energy states energy_hist = EnergyHistogram(mc_obj, n_bins=100)
from cemc.mcmc import SiteOrderParameter, EnergyEvolution, EnergyHistogram from cemc.mcmc import MCBackup, DiffractionObserver # The Correlation Function Tracker computes the thermodynamic # average of all the correlation functions corr_func_obs = CorrelationFunctionTracker(calc) # The PairCorrelationObserver computes the thermodynamic average of all # the pair interactions. # If only the pair interactions are of interest, this observer should be # preferred over the CorrelationFunctionTracker pair_obs = PairCorrelationObserver(calc) # This function takes a snap shot of the system and collects # them in a trajectory file snapshot = Snapshot( trajfile="demo.traj", atoms=atoms ) # This observer stores the structure having the lowest energy low_en = LowestEnergyStructure( calc, mc_obj ) # This observer tracks networks of a certain atom type network_obs = NetworkObserver( calc=calc, cluster_name=[get_example_network_name(ceBulk)], element=["Mg"]) # This tracks the average number of sites that where the symbol as changed site_order = SiteOrderParameter(atoms) # Energy evolution. Useful to check if the system has been equilibrated energy_evol = EnergyEvolution(mc_obj) # Energy histogram: Sample a histogram of the visited energy states energy_hist = EnergyHistogram(mc_obj, n_bins=100)