10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-06-22 21:22:17 +02:00

Merge pull request #35 from scemama/master

Introduce ezfio_interface.py and EZFIO.cfg in Full_CI
This commit is contained in:
Anthony Scemama 2015-03-25 11:25:00 +01:00
commit 5e82d807cf
12 changed files with 419 additions and 67 deletions

5
.gitignore vendored
View File

@ -1,3 +1,8 @@
quantum_package.rc
EZFIO
irpf90
EMSL_Basis
bin/
*.log
quantum_package_static.tar.gz

View File

@ -13,7 +13,7 @@ endif
LIBS=
PKGS=
OCAMLCFLAGS="-g -warn-error A"
OCAMLBUILD=ocamlbuild -j 0 -syntax camlp4o -cflags $(OCAMLCFLAGS) -lflags $(OCAMLCFLAGS) -ocamlopt ocamlc.opt
OCAMLBUILD=ocamlbuild -j 0 -syntax camlp4o -cflags $(OCAMLCFLAGS) -lflags $(OCAMLCFLAGS)
MLFILES=$(wildcard *.ml) ezfio.ml Qptypes.ml
MLIFILES=$(wildcard *.mli)
ALL_TESTS=$(patsubst %.ml,%.byte,$(wildcard test_*.ml))

268
scripts/ezfio_interface.py Executable file
View File

@ -0,0 +1,268 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Take a path in argv
Check if EZFIO.cfg exists.
EZFIO.cfg are in MODULE directories.
create : ezfio_interface.irp.f
folder_ezfio_inteface_config
Example EZFIO.cfg:
```
[thresh_SCF]
doc: Threshold on the convergence of the Hartree Fock energy
type: Threshold
default: 1.e-10
[do_pt2_end]
type: logical
doc: If true, compute the PT2 at the end of the selection
default: true
```
"""
import sys
import os
import os.path
import ConfigParser
from collections import defaultdict
from collections import namedtuple
Type = namedtuple('Type', 'ocaml fortran')
def bool_convertor(b):
return ( b.lower() in [ "true", ".true." ] )
def get_type_dict():
"""
This function makes the correspondance between the type of value read in
ezfio.cfg into the f90 and Ocam Type
return fancy_type[fancy_type] = namedtuple('Type', 'ocaml fortran')
For example fancy_type['Ndet'].fortran = interger
.ocaml = int
"""
# ~#~#~#~ #
# I n i t #
# ~#~#~#~ #
# Dict to change ocaml LowLevel type into FortranLowLevel type
ocaml_to_fortran = {"int": "integer",
"float": "double precision",
"logical": "logical",
"string": "character*60"}
fancy_type = defaultdict(dict)
# ~#~#~#~#~#~#~#~ #
# R a w _ t y p e #
# ~#~#~#~#~#~#~#~ #
fancy_type['integer'] = Type("int", "integer")
fancy_type['int' ] = Type("int", "integer")
fancy_type['float' ] = Type("float", "double precision")
fancy_type['double precision'] = Type("float", "double precision")
fancy_type['logical'] = Type("bool", "logical")
fancy_type['bool' ] = Type("bool", "logical")
# ~#~#~#~#~#~#~#~ #
# q p _ t y p e s #
# ~#~#~#~#~#~#~#~ #
src = os.environ['QPACKAGE_ROOT'] + "/ocaml/qptypes_generator.ml"
with open(src, "r") as f:
l = [i for i in f.read().splitlines() if i.strip().startswith("*")]
for i in l:
ocaml_fancy_type = i.split()[1].strip()
ocaml_type = i.split()[3]
fortran_type = ocaml_to_fortran[ocaml_type]
fancy_type[ocaml_fancy_type] = Type(ocaml_type, fortran_type)
return dict(fancy_type)
type_dict = get_type_dict()
def get_dict_config_file(config_file_path,folder):
"""
Read a ezfio.cfg
Return a dict d[provider_name] = {type, default, ezfio_name,ezfio_dir,doc}
"""
# ~#~#~#~ #
# I n i t #
# ~#~#~#~ #
d = defaultdict(dict)
list_option_required = ["default", "doc"]
list_option = ["ezfio_name", "output"]
# ~#~#~#~#~#~#~#~#~#~#~ #
# L o a d _ C o n f i g #
# ~#~#~#~#~#~#~#~#~#~#~ #
config_file = ConfigParser.ConfigParser()
config_file.readfp(open(config_file_path))
# ~#~#~#~#~#~#~#~#~ #
# F i l l _ d i c t #
# ~#~#~#~#~#~#~#~#~ #
provider_names = config_file.sections()
for p in provider_names:
provider_name = p.lower()
default_d = {"ezfio_name": provider_name, "output": "false" }
# Check if type if avalaible
type_ = config_file.get(p, "type")
if type_ not in type_dict:
print "{0} not avalaible. Choose in:".format(type_)
print ", ".join([i for i in type_dict])
sys.exit(1)
else:
d[provider_name]["type"] = type_dict[type_]
# Fill the dict with allother the information
for k in list_option_required:
try:
d[provider_name][k] = config_file.get(p, k)
except ConfigParser.NoOptionError:
print "You need a {0} for {1} in {2}".format(k,
provider_name,
config_file_path)
d[provider_name]["ezfio_dir"] = folder
for k in list_option:
try:
d[provider_name][k] = config_file.get(p, k).lower()
except ConfigParser.NoOptionError:
d[provider_name][k] = default_d[k]
# Convert string to bool
d[provider_name]["output"] = bool_convertor(d[provider_name]["output"])
return dict(d)
def create_ezfio_provider(dict_ezfio_cfg):
"""
From dict d[provider_name] = {type, default, ezfio_name,ezfio_dir,doc}
create the a list who containt all the code for the provider
return [code, ...]
"""
from ezfio_with_default import EZFIO_Provider
dict_code_provider = dict()
ez_p = EZFIO_Provider()
for provider_name, dict_info in dict_ezfio_cfg.iteritems():
if not dict_info["output"]:
ez_p.set_type(dict_info['type'].fortran)
ez_p.set_name(provider_name)
ez_p.set_doc(dict_info['doc'])
ez_p.set_ezfio_dir(dict_info['ezfio_dir'])
ez_p.set_ezfio_name(dict_info['ezfio_name'])
ez_p.set_default(dict_info['default'])
ez_p.set_output("output_%s" % dict_info['ezfio_dir'])
dict_code_provider[provider_name] = str(ez_p)
return dict_code_provider
def save_ezfio_provider(path_head, dict_code_provider):
"""
Write in "ezfio_interface.irp.f" the
value of dict_code_provider
"""
path = "{0}/ezfio_interface.irp.f".format(path_head)
print "Path = {}".format(path)
with open(path, "w") as f:
f.write("!DO NOT MODIFY BY HAND \n")
f.write("!Created by $QPACKAGE_ROOT/scripts/ezfio_interface.py \n")
f.write("!from file {0}/EZFIO.cfg\n".format(path_head))
f.write("\n")
for provider_name, code in dict_code_provider.iteritems():
f.write(code + "\n")
def create_ezfio_config(dict_ezfio_cfg, opt, folder):
"""
From dict_ezfio_cfg[provider_name] = {type, default, ezfio_name,ezfio_dir,doc}
Return the string ezfio_interface_config
"""
result = [ folder ]
lenmax = max( [ len(i) for i in dict_ezfio_cfg ] )+2
l = sorted(dict_ezfio_cfg.keys())
for provider_name in l:
provider_info = dict_ezfio_cfg[provider_name]
s = " {0} {1}".format( provider_name.lower().ljust(lenmax), provider_info["type"].fortran )
result.append(s)
return "\n".join(result)
def save_ezfio_config(folder, str_ezfio_config):
"""
Write the str_ezfio_config in
$QPACKAGE_ROOT/EZFIO/{0}.ezfio_interface_config".format(folder)
"""
ezfio_dir = "{0}/EZFIO".format(os.environ['QPACKAGE_ROOT'])
path = "{0}/config/{1}.ezfio_interface_config".format(ezfio_dir,
folder)
print "Path = {}".format(path)
with open(path, "w") as f:
f.write(str_ezfio_config)
def main():
"""Take in argument a EZFIO.cfg"""
try:
path = sys.argv[1]
except:
path = "EZFIO.cfg"
if "EZFIO.cfg" not in os.listdir(os.getcwd()):
sys.exit(0)
path = os.path.expanduser(path)
path = os.path.expandvars(path)
path = os.path.abspath(path)
print path
path_dirname = os.path.dirname(path)
folder = [i for i in path_dirname.split("/") if i][-1]
folder = folder.lower()
print "Find a EZFIO.cfg in {}".format(path)
dict_info_provider = get_dict_config_file(path,folder)
print "Generating the ezfio_interface.irp.f: \n"
d_config = create_ezfio_provider(dict_info_provider)
# for provider, code in d_config.iteritems():
# print code
print "Saving the ezfio_interface.irp.f"
save_ezfio_provider(path_dirname, d_config)
print "Generating the ezfio_config"
config_ezfio = create_ezfio_config(dict_info_provider, "config", folder)
# print config_ezfio
print "Saving ezfio_config"
save_ezfio_config(folder, config_ezfio)
if __name__ == "__main__":
main()

1
src/.gitignore vendored
View File

@ -1 +1,2 @@
Makefile.config
ezfio_interface.irp.f

View File

View File

@ -0,0 +1,5 @@
======================
CAS_SD_selected Module
======================
Selected CAS + SD module

37
src/Full_CI/EZFIO.cfg Normal file
View File

@ -0,0 +1,37 @@
[N_det_max_fci]
doc: Max number of determinants in the wave function
type: Det_number_max
default: 10000
[N_det_max_fci_property]
doc: Max number of determinants in the wave function when you select for a given property
type: Det_number_max
default: 10000
[do_pt2_end]
type: logical
doc: If true, compute the PT2 at the end of the selection
default: true
[PT2_max]
type: PT2_energy
doc: The selection process stops when the largest PT2 (for all the state is lower
than pt2_max in absolute value
default: 0.0001
[var_pt2_ratio]
type: Normalized_float
doc: The selection process stops when the energy ratio variational/(variational+PT2)
is equal to var_pt2_ratio
default: 0.75
[energy]
type: double precision
doc: "Calculated Full CI energy"
output: true
[energy_pt2]
type: double precision
doc: "Calculated Full CI energy"
output: true

View File

@ -1,8 +0,0 @@
full_ci
n_det_max_fci integer
n_det_max_fci_property integer
pt2_max double precision
do_pt2_end logical
energy double precision
energy_pt2 double precision
var_pt2_ratio double precision

View File

@ -1,41 +0,0 @@
BEGIN_SHELL [ /usr/bin/python ]
from ezfio_with_default import EZFIO_Provider
T = EZFIO_Provider()
T.set_type ( "integer" )
T.set_name ( "N_det_max_fci" )
T.set_doc ( "Max number of determinants in the wave function" )
T.set_ezfio_dir ( "full_ci" )
T.set_ezfio_name( "N_det_max_fci" )
T.set_output ( "output_full_ci" )
print T
T.set_type ( "integer" )
T.set_name ( "N_det_max_fci_property" )
T.set_doc ( "Max number of determinants in the wave function when you select for a given property" )
T.set_ezfio_dir ( "full_ci" )
T.set_ezfio_name( "N_det_max_fci_property" )
T.set_output ( "output_full_ci" )
print T
T.set_type ( "logical" )
T.set_name ( "do_pt2_end" )
T.set_doc ( "If true, compute the PT2 at the end of the selection" )
T.set_ezfio_name( "do_pt2_end" )
print T
T.set_type ( "double precision" )
T.set_name ( "pt2_max" )
T.set_doc ( """The selection process stops when the largest PT2 (for all the states)
is lower than pt2_max in absolute value""" )
T.set_ezfio_name( "pt2_max" )
print T
T.set_type ( "double precision" )
T.set_name ( "var_pt2_ratio" )
T.set_doc ( """The selection process stops when the energy ratio variational/(variational+PT2)
is equal to var_pt2_ratio""" )
T.set_ezfio_name( "var_pt2_ratio" )
print T
END_SHELL

View File

@ -0,0 +1,76 @@
program var_pt2_ratio_run
implicit none
integer :: i,k
double precision, allocatable :: pt2(:), norm_pert(:), H_pert_diag(:)
integer :: N_st, degree
N_st = N_states
allocate (pt2(N_st), norm_pert(N_st),H_pert_diag(N_st))
character*(64) :: perturbation
double precision, allocatable :: psi_det_save(:,:,:), psi_coef_save(:,:)
double precision :: E_fci, E_var, ratio, E_ref
integer :: Nmin, Nmax
pt2 = -(pt2_max+1.d0)
diag_algorithm = "Lapack"
ratio = 0.d0
Nmin=1
do while (dabs(pt2(1)) > pt2_max)
call H_apply_FCI(pt2, norm_pert, H_pert_diag, N_st)
psi_det = psi_det_sorted
psi_coef = psi_coef_sorted
soft_touch N_det psi_det psi_coef
call diagonalize_CI
ratio = (CI_energy(1) - HF_energy) / (CI_energy(1)+pt2(1) - HF_energy)
enddo
threshold_selectors = 1.d0
threshold_generators = 0.999d0
call diagonalize_CI
call H_apply_FCI_PT2(pt2, norm_pert, H_pert_diag, N_st)
E_ref = CI_energy(1) + pt2(1)
threshold_selectors = 0.99d0
threshold_generators = 0.98d0
var_pt2_ratio = (E_ref + pt2_max - HF_energy) / (E_ref - HF_energy)
TOUCH var_pt2_ratio
Nmax=max(10000,3*N_det)
Nmin=1
do while (Nmax-Nmin > 1)
ratio = (CI_energy(1) - HF_energy) / (E_ref - HF_energy)
if (ratio < var_pt2_ratio) then
Nmin = N_det
! Nmax = max(Nmax,Nmin+10)
! Select new determinants
call H_apply_FCI(pt2, norm_pert, H_pert_diag, N_st)
N_det = min(N_det,Nmax)
else
Nmax = N_det
N_det = Nmin + (Nmax-Nmin)/2
endif
psi_det = psi_det_sorted
psi_coef = psi_coef_sorted
soft_touch N_det psi_det psi_coef
call diagonalize_CI
call save_wavefunction
print *, 'Det min, Det max: ', Nmin, Nmax
print *, 'Ratio : ', ratio, ' ~ ', var_pt2_ratio
print *, 'HF_energy = ', HF_energy
print *, 'Est FCI = ', E_ref
print *, 'PT2 = ', pt2(1)
print *, 'N_det = ', N_det
print *, 'E = ', CI_energy(1)
call ezfio_set_full_ci_energy(CI_energy)
if (abort_all) then
exit
endif
enddo
deallocate(pt2,norm_pert)
end

View File

@ -15,8 +15,8 @@ $(info -------------------- Error --------------------)
$(info QPACKAGE_ROOT undefined. Run the setup_environment.sh script)
$(info -----------------------------------------------)
$(error )
else
$(info QPACKAGE_ROOT is defined.)
#else
#$(info QPACKAGE_ROOT is defined.)
endif
@ -30,8 +30,8 @@ $(info You can create Makefile.config)
$(info by modifying Makefile.config.example)
$(info -----------------------------------------------)
$(error )
else
$(info Makefile.config is present.)
#else
#$(info Makefile.config is present.)
endif
@ -50,8 +50,8 @@ $(info To upgrade IRPF90, run : )
$(info $(QPACKAGE_ROOT)/scripts/upgrade_irpf90.sh )
$(info -----------------------------------------------)
$(error )
else
$(info irpf90 version is OK.)
#else
#$(info irpf90 version is OK.)
endif
@ -69,8 +69,8 @@ $(info Your NEEDED_MODULES file is inconsistent. It should be)
$(info $(NEEDED_MODULES_OK))
$(info -----------------------------------------------)
$(error )
else
$(info NEEDED_MODULES files is consistent.)
#else
#$(info NEEDED_MODULES files is consistent.)
endif
@ -91,8 +91,8 @@ $(info You should document it before you compile, as)
$(info well as the ASSUMPTIONS.rst file.)
$(info -----------------------------------------------)
$(error )
else
$(info README.rst is present.)
#else
#$(info README.rst is present.)
endif
@ -104,8 +104,8 @@ $(info -------------------- Error --------------------)
$(info This is a Bug. At that point, the ASSUMPTIONS.rst)
$(info file should exist.)
$(info -----------------------------------------------)
else
$(info ASSUMPTIONS.rst is present.)
#else
#$(info ASSUMPTIONS.rst is present.)
endif
@ -125,19 +125,27 @@ $(info To upgrade EZFIO, run : )
$(info $(QPACKAGE_ROOT)/scripts/upgrade_ezfio.sh )
$(info -----------------------------------------------)
$(error )
else
$(info EZFIO version is OK.)
#else
#$(info EZFIO version is OK.)
endif
# Define the EZFIO rules
$(EZFIO): $(wildcard $(QPACKAGE_ROOT)/src/*.ezfio_config) $(wildcard $(QPACKAGE_ROOT)/src/*/*.ezfio_config)
$(EZFIO): $(wildcard $(QPACKAGE_ROOT)/src/*.ezfio_config) $(wildcard $(QPACKAGE_ROOT)/src/*/*.ezfio_config) $(wildcard $(QPACKAGE_ROOT)/src/*/EZFIO.cfg)
@echo Building EZFIO library
@cp $(wildcard $(QPACKAGE_ROOT)/src/*.ezfio_config) $(wildcard $(QPACKAGE_ROOT)/src/*/*.ezfio_config) $(EZFIO_DIR)/config
@cd $(EZFIO_DIR) ; export FC="$(FC)" ; export FCFLAGS="$(FCFLAGS)" ; export IRPF90="$(IRPF90)" ; $(MAKE) ; $(MAKE) Python
@echo =-=-=-=-=-=-=-=-=-=-=-
for dir in $(QPACKAGE_ROOT)/src/*/ ;\
do \
cd $$dir && \
$(QPACKAGE_ROOT)/scripts/ezfio_interface.py && \
cd .. ;\
done
cp $(wildcard $(QPACKAGE_ROOT)/src/*.ezfio_config) $(wildcard $(QPACKAGE_ROOT)/src/*/*.ezfio_config) $(EZFIO_DIR)/config
cd $(EZFIO_DIR) ; export FC="$(FC)" ; export FCFLAGS="$(FCFLAGS)" ; export IRPF90="$(IRPF90)" ; $(MAKE) ; $(MAKE) Python
ezfio: $(EZFIO)
# Create symbolic links of other modules
ifneq ($(PWD),$(QPACKAGE_ROOT)/src)

View File

@ -0,0 +1 @@
THIS FILE HAS TO BE FILLED