davidkleiven / CEMC

DEPRECATED: Monte Carlo package targeted at systems studied with the Cluster Expansion.
MIT License
3 stars 2 forks source link

Datatype problem in get_ce_calc #47

Closed davidkleiven closed 5 years ago

davidkleiven commented 5 years ago

There seems to be data type issue in the get_ce_calc function. The error is caused by the following piece of code

from __future__ import print_function
from ase.clease import CEBulk as BulkCrystal
from ase.clease import GenerateStructures
from ase.clease import Evaluate
from cemc import get_ce_calc
import json
import numpy as np

# Database used to have all the structures
ECI_FILE = "data/almgzn.json"
db_name = "data/almgzn.db"

def main():

    # Concentation arguments. NOTE: The way of specifying concentrations
    # will be changed in the future
    conc_args = {
        "conc_ratio_min_1": [[8, 0, 0]],
        "conc_ratio_max_1": [[0, 8, 0]],
        "conc_ratio_min_2": [[6, 3, 4]],
        "conc_ratio_max_2": [[2, 5, 6]]
    }

    argms = {
        "crystalstructure":"fcc", "a":4.05, "size":[4, 4, 4],
        "basis_elements":[["Al", "Mg", "Zn"]], "conc_args":conc_args,
        "db_name":db_name, "max_cluster_size":4, "max_cluster_dia":[0, 0, 5.2, 4.1, 4.1]
    }

##    arguments = {
##    crystalstructure="fcc", a=4.05, size=[4, 4, 4],
##    basis_elements=[["Al", "Mg", "Zn"]], conc_args=conc_args,
##    db_name=db_name, max_cluster_size=4, max_cluster_dia=[0, 0, 5.2, 4.1, 4.1]
##    }

    ceBulk = BulkCrystal( **argms)

    ceBulk.reconfigure_settings()

    mc_cell_size = [10, 10, 10]

    with open(ECI_FILE, 'r') as infile:
        eci = json.load(infile)

    calc = get_ce_calc(ceBulk, argms, eci=eci, size=mc_cell_size, db_name=db_name)

    ceBulk = calc.BC
    ceBulk.atoms.set_calculator( calc )

@phymalidoust can you ellaborate on what the exact error message is?

davidkleiven commented 5 years ago

It is recommended to use a separate database name for the big cell e.g.

...
calc = get_ce_calc(ceBulk, argms, eci=eci, size=mc_cell_size, db_name="different_db_name.db")
...

instead of

calc = get_ce_calc(ceBulk, argms, eci=eci, size=mc_cell_size, db_name=db_name)

This should be highlighted in the documentation.

The datatypes should be

  1. ceBulk - instance of CEBulk
  2. argms - dictionary with the arguments given when initializing the small call
  3. eci - dictionary with the effective cluster interactions
  4. size - list with the cell dimensions of the new cell (e.g. [10, 10, 10])
  5. db_name - string with the database name that should be used for the large cell.
phymalidoust commented 5 years ago

The exact error is: RuntimeError: '>' not supported between instances of 'int' and 'list'

It still persists after using a separate name for the second database argument

davidkleiven commented 5 years ago

There was an error in one of the sanity checks, such that you get a datatype error when passing a list as max_cluster_dia. This issue is fixed by PR #48.

phymalidoust commented 5 years ago

Now it passes through "get_ce_calc()" but stops with a new error: Division by Zero

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cemc/tools/gsfinder.py", line 28, in get_gs calc = CE( BC, ecis ) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cemc/ce_calculator.py", line 155, in init cf_names) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ase/clease/corrFunc.py", line 156, in get_cf_by_cluster_names cf_temp = sp / count ZeroDivisionError: float division by zero

davidkleiven commented 5 years ago

Ok, this is most likely due the fact that GSFinder was intended to be used only when constructing the ClusterExpansion, and then a calculator is not attached. We should fix this such that it checks if a calculator has already been attached and in that case it should not initialize a new calculator, such that we can use this class also for this case.

davidkleiven commented 5 years ago

Ok, should be fixed now. The attached script works for me.

from __future__ import print_function
from ase.clease import CEBulk as BulkCrystal
from ase.clease import GenerateStructures
from ase.clease import Evaluate
from cemc import get_ce_calc
import json
import numpy as np

# Database used to have all the structures
ECI_FILE = "data/almgzn.json"
db_name = "data/almgzn.db"

def main():

    # Concentation arguments. NOTE: The way of specifying concentrations
    # will be changed in the future
    conc_args = {
        "conc_ratio_min_1": [[8, 0, 0]],
        "conc_ratio_max_1": [[0, 8, 0]],
        "conc_ratio_min_2": [[6, 3, 4]],
        "conc_ratio_max_2": [[2, 5, 6]]
    }

    argms = {
        "crystalstructure":"fcc", "a":4.05, "size":[4, 4, 4],
        "basis_elements":[["Al", "Mg", "Zn"]], "conc_args":conc_args,
        "db_name":db_name, "max_cluster_size":4, "max_cluster_dia":[0, 0, 5.2, 4.1, 4.1]
    }

    ceBulk = BulkCrystal( **argms)

    ceBulk.reconfigure_settings()

    mc_cell_size = [10, 10, 10]

    # with open(ECI_FILE, 'r') as infile:
    #     eci = json.load(infile)
    eci = {"c1_0": 0.1}

    big_cell_db_name = "data/almgsizn_large_cell.db"
    calc = get_ce_calc(ceBulk, argms, eci=eci, size=mc_cell_size, db_name=big_cell_db_name)

    ceBulk = calc.BC
    ceBulk.atoms.set_calculator( calc )

    # Create an instance of the structure generator
    struc_generator = GenerateStructures(ceBulk, struct_per_gen=4)

    # Evaluate and fit the ECIs
    # evaluate(ceBulk)

    alvc = np.arange(0.03, 1.0, 0.025)

    for nal in range(0 , len(alvc)):
        al0 = alvc[nal]
        znvc = np.arange(0.03, 1.0 - al0 - 0.01, 0.025)
        for nzn in range(0 , len(znvc)):
            zn0 = znvc[nzn]
            mg0 = 1 - al0 - zn0 
            enrg = random_gs(ceBulk , struc_generator , al0 , zn0 , mg0)
            fd = open('/Volumes/Macintosh2/ClusterExpansion/codes/Mohm/runscripts/data/MCEnrgy.txt' , 'a+')
            print(enrg,al0,zn0,mg0,file=fd)
            fd.close()

def random_gs(bc, struct_gen , aal , zzn , mmg):
    from cemc.tools import GSFinder
    # with open(ECI_FILE, 'r') as infile:
    #     eci = json.load(infile)
    eci = {"c1_0": 0.1}
    T = np.linspace(10, 2000, 30)[::-1]
    composition = {"Al": aal, "Zn": zzn, "Mg": mmg}
    gs_finder = GSFinder()
    gs = gs_finder.get_gs(bc, eci, composition=composition, temps=T, n_steps_per_temp=2500)
    #print("Energy: {}".format(gs["energy"]))
    try:
        struct_gen.insert_structure(init_struct=gs["atoms"])
    except RuntimeError as exc:
        print(str(exc))
        pass

    return gs["energy"]

if __name__ == "__main__":
    main()

@phymalidoust I would recommend to not try to store these systems in the database, via the insert_structure function. That function is going to try to calculate the correlation function from scratch, and for 1000 atoms that is going to take for ever.

davidkleiven commented 5 years ago

@phymalidoust note that I set the ECIs to some dummy values since I did not have the ECI file.

phymalidoust commented 5 years ago

When I use the simple dictionary for ci, say, ci = = {"c1_0": 0.1}, it passes okay. But with the attached Jason file (got for almgzn with 4x4x4 dimension) I get the devision by zero error (change .txt -> .json). almgzn.txt

Here is the error:

gs = gs_finder.get_gs(bc, eci, composition=composition, temps=T, n_steps_per_temp=2500)

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cemc/tools/gsfinder.py", line 28, in get_gs calc = CE( BC, ecis ) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cemc/ce_calculator.py", line 155, in init cf_names) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ase/clease/corrFunc.py", line 156, in get_cf_by_cluster_names cf_temp = sp / count ZeroDivisionError: float division by zero

davidkleiven commented 5 years ago

I cannot reproduce this. It works for me with those values.

phymalidoust commented 5 years ago

Found the error. If you now comment out "max_cluster_dia" it stops with the 'zero division error'. This argument then should have to be there now - is a requirement-.