Closed potus28 closed 1 year ago
Hi @potus28, thank you for your interest in our code. I'm not used the ASE CP2K calculator before. Here are a few suggestions on debugging this:
Hi @YuuuXie , thank you so much for the reply and your suggestions. I double-checked to see if it was an issue with the ASE CP2K calculator, but it is working as expected. I also tried using just FLARE and the code failed with the same " TypeError: cannot pickle '_thread.lock' object" error. I've attached a python program based on the tutorial from the FLARE Read the Docs page with the built-in ASE CP2K calculator. Please let me know if you need anything else. Thank you so much!
# # Build the System with ASE
# In[43]:
import numpy as np
from ase import units
from ase.spacegroup import crystal
from ase.build import bulk
from ase.visualize import view
np.random.seed(12345)
a = 3.52678
super_cell = bulk("C", "diamond", a=a, cubic=True).repeat((2,2,2))
view(super_cell,viewer="x3d")
# # Set up the FLARE Calculator for OTF Gaussian Process Regression
# In[44]:
from flare.gp import GaussianProcess
from flare.utils.parameter_helper import ParameterHelper
# set up hyperparameters for a 2+3 body kernel
kernels = ["twobody", "threebody"]
parameters = {'cutoff_twobody': 5.0,
"cutoff_threebody": 3.5}
pm = ParameterHelper(
kernels = kernels,
random = True,
parameters = parameters
)
hm = pm.as_dict()
hyps = hm["hyps"]
cut = hm["cutoffs"]
print("hyps", hyps)
# In[38]:
# Since we are using ASE, always set compontent to "mc" regardless if the system is single component or not
gp_model = GaussianProcess(
kernels = kernels,
component = "mc",
hyps = hyps,
cutoffs = cut,
hyp_labels = ["sig2", "ls2", "sig3", "ls3", "noise"],
opt_algorithm = "L-BFGS-B",
ncpus = 1
)
# # Optional Mapped Gaussian Process
# If you want to use Mapped Gaussian Process (MGP), the set up is as follows
# In[39]:
from flare.mgp import MappedGaussianProcess
grid_params = {"twobody": {"grid_num":[64]},
"threebody": {"grid_num":[16,16,16]}
}
mgp_model = MappedGaussianProcess(
grid_params,
unique_species=[6],
n_cpus=1,
var_map=None
)
# # FLARE ASE Calculator
# In[40]:
from flare.ase.calculator import FLARE_Calculator
# If you want to use MGP, switch use_mapping to True and mgp_model to mgp_model
flare_calculator = FLARE_Calculator(
gp_model,
par = True,
mgp_model = mgp_model ,
use_mapping= True
)
# # DFT Calculator
# In[33]:
from ase.calculators.cp2k import CP2K
# ASE calculator
input_data = '''
&FORCE_EVAL
&DFT
&SCF
&OUTER_SCF .TRUE.
MAX_SCF 50
&END OUTER_SCF
&OT .TRUE.
MINIMIZER DIIS
ALGORITHM IRAC
PRECONDITIONER FULL_SINGLE_INVERSE
&END OT
&END SCF
&XC
&XC_GRID
XC_DERIV NN10_SMOOTH
XC_SMOOTH_RHO NN10
&END XC_GRID
&END XC
&END DFT
&END FORCE_EVAL
'''
dft_calc = CP2K(
auto_write=False,
basis_set="DZVP-MOLOPT-SR-GTH",
basis_set_file="BASIS_MOLOPT",
charge=0,
cutoff = 400*units.Rydberg,
debug = False,
force_eval_method = "Quickstep",
xc = "PBE",
inp = input_data,
max_scf = 30,
poisson_solver ="auto",
potential_file = "POTENTIAL",
pseudo_potential = "GTH-PBE",
stress_tensor = True,
print_level = "LOW",
uks=True
)
# Double check to ensure the calculator is working
# In[47]:
super_cell.calc = dft_calc
en = super_cell.get_potential_energy()
print("Potential_energy (eV):", en)
# # OTF Molecular Dynamics Engine
# In[48]:
from ase import units
from ase.md.velocitydistribution import MaxwellBoltzmannDistribution, Stationary, ZeroRotation
temperature = 500
MaxwellBoltzmannDistribution(super_cell, temperature_K=temperature)
# Ensure that linear and angular momentum are zero
Stationary(super_cell)
ZeroRotation(super_cell)
md_engine = "VelocityVerlet"
md_kwargs = {}
#md_engine = "NVTBerendsen"
#md_kwargs = {"temperature": 500, "taut": 0.5e3 * units.fs}
# # Run MD with OFT Training!
# In[49]:
from flare.ase.otf import ASE_OTF
super_cell.calc = flare_calculator
otf_params = {'init_atoms': [0, 1, 2, 3],
'output_name': 'otf',
'std_tolerance_factor': 2,
'max_atoms_added' : 4,
'freeze_hyps': 10,
'write_model': 3} # If you will probably resume the training, please set to 3
test_otf = ASE_OTF(super_cell,
timestep = 1 * units.fs,
number_of_steps = 3,
dft_calc = dft_calc,
md_engine = md_engine,
md_kwargs = md_kwargs,
**otf_params)
test_otf.run()
Hi @potus28 , it seems that there is an issue with "deepcopy" the ASE CP2K calculator. I'm wondering, without flare, in your python script for using CP2K from ASE, could you try calc = deepcopy(cp2k_calc)
before or after the calculation, and see if there is the same error? If so, there might be an issue with the CP2K calculator, and worth reporting to the ASE team.
I have tried to modify the code of CP2K calculator in ASE (ase/calculators/cp2k.py), made it create shell before calculation and cancel shell after calculation. So that the function deepcopy(cp2k_calc) would not be conflicted with the running shell. The modified code is as follows:
200c200
< #self._shell = Cp2kShell(self.command, self._debug)
---
> self._shell = Cp2kShell(self.command, self._debug)
207,209c207,209
< #if self._shell:
< # self._release_force_env()
< # del(self._shell)
---
> if self._shell:
> self._release_force_env()
> del(self._shell)
245c245
< self._shell = Cp2kShell(self.command, self._debug)
---
>
313,315d312
<
< self._release_force_env()
< del(self._shell)
Hi @potus28, let me know if the above approach solves your problem!
And @jiongwalai, thank you very much for posting this patch! Maybe you can open an issue/pull request to ASE, letting them know about this problem, such that they can fix it in the new release.
HI @YuuuXie and @jiongwalai , sorry for the late response and I hope y'all have had a great holiday season. Thank y'all so much for the suggestions I will try this approach this week!
Hi @YuuuXie and @jiongwalai, following this approach, I was able to run a OTF simulation with ASE using CP2K as the DFT calculator. Thanks again for y'all's help!
Hello,
I am trying to use Flare++ for on-the-fly training using CP2K as the DFT calculator. The code for my simulation is as follows:
And here are the contents of mycalculators.py. All this file does is help automate additional input with a CP2K calculator:
However, I run into this error at the beginning of the simulation on the first DFT call:
I'm not sure what the error is, but it looks like something is going wrong with using CP2K as the DFT calculator. Is there anything I can change in the simulation so that I can use CP2K? Any help would be much appreciated. Thanks!