-
Notifications
You must be signed in to change notification settings - Fork 23
Implement orbital-based calculation #236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| # cython: language_level=3 | ||
| from cpython.mem cimport PyMem_Malloc, PyMem_Free | ||
|
|
||
| cdef extern from "tdnac.c": | ||
| void TD_NAC(int istep, int nst, int nbasis, int norb, int nocc, int nvirt, double dt, \ | ||
| int *orb_ini, int *orb_final, double **nacme, double **ao_overlap, \ | ||
| double **mo_coef_old, double **mo_coef_new, double ***ci_coef_old, double ***ci_coef_new) | ||
|
|
||
| def wf_overlap(qm, molecule, istep_py, dt_py): | ||
| cdef: | ||
| int *orb_ini | ||
| int *orb_final | ||
| double **nacme | ||
| double **ao_overlap | ||
| double **mo_coef_old | ||
| double **mo_coef_new | ||
| double ***ci_coef_old | ||
| double ***ci_coef_new | ||
|
|
||
| int istep, ist, nst, ibasis, jbasis, iorb, jorb, nbasis, norb, nocc, nvirt | ||
| double dt | ||
|
|
||
| # Assign size variables | ||
| dt = dt_py | ||
| istep = istep_py | ||
| nst = molecule.nst | ||
| nbasis = qm.nbasis | ||
| norb = qm.norb | ||
| nocc = qm.nocc | ||
| nvirt = qm.nvirt | ||
|
|
||
| # Allocate NACME variables | ||
| orb_ini = <int*> PyMem_Malloc(1 * sizeof(int)) | ||
| orb_final = <int*> PyMem_Malloc(1 * sizeof(int)) | ||
|
|
||
| nacme = <double**> PyMem_Malloc(nst * sizeof(double*)) | ||
|
|
||
| ao_overlap = <double**> PyMem_Malloc(nbasis * sizeof(double*)) | ||
| mo_coef_old = <double**> PyMem_Malloc(norb * sizeof(double*)) | ||
| mo_coef_new = <double**> PyMem_Malloc(norb * sizeof(double*)) | ||
|
|
||
| for ist in range(nst): | ||
| nacme[ist] = <double*> PyMem_Malloc(nst * sizeof(double)) | ||
|
|
||
| for ibasis in range(nbasis): | ||
| ao_overlap[ibasis] = <double*> PyMem_Malloc(nbasis * sizeof(double)) | ||
|
|
||
| for iorb in range(norb): | ||
| mo_coef_old[iorb] = <double*> PyMem_Malloc(nbasis * sizeof(double)) | ||
| mo_coef_new[iorb] = <double*> PyMem_Malloc(nbasis * sizeof(double)) | ||
|
|
||
| ci_coef_old = <double***> PyMem_Malloc(nst * sizeof(double**)) | ||
| ci_coef_new = <double***> PyMem_Malloc(nst * sizeof(double**)) | ||
|
|
||
| for ist in range(nst): | ||
| ci_coef_old[ist] = <double**> PyMem_Malloc(nocc * sizeof(double*)) | ||
| ci_coef_new[ist] = <double**> PyMem_Malloc(nocc * sizeof(double*)) | ||
|
|
||
| for ist in range(nst): | ||
| for iorb in range(nocc): | ||
| ci_coef_old[ist][iorb] = <double*> PyMem_Malloc(nvirt * sizeof(double)) | ||
| ci_coef_new[ist][iorb] = <double*> PyMem_Malloc(nvirt * sizeof(double)) | ||
|
|
||
| # Assign NACME variables from python to C | ||
| orb_ini[0] = qm.orb_ini[0] | ||
| orb_final[0] = qm.orb_final[0] | ||
|
|
||
| for ist in range(nst): | ||
| for jst in range(nst): | ||
| nacme[ist][jst] = 0. | ||
|
|
||
| for ibasis in range(nbasis): | ||
| for jbasis in range(nbasis): | ||
| ao_overlap[ibasis][jbasis] = qm.ao_overlap[ibasis, jbasis] | ||
|
|
||
| for iorb in range(norb): | ||
| for ibasis in range(nbasis): | ||
| mo_coef_old[iorb][ibasis] = qm.mo_coef_old[iorb, ibasis] | ||
| mo_coef_new[iorb][ibasis] = qm.mo_coef_new[iorb, ibasis] | ||
|
|
||
| for ist in range(nst): | ||
| for iorb in range(nocc): | ||
| for jorb in range(nvirt): | ||
| ci_coef_old[ist][iorb][jorb] = qm.ci_coef_old[ist, iorb, jorb] | ||
| ci_coef_new[ist][iorb][jorb] = qm.ci_coef_new[ist, iorb, jorb] | ||
|
|
||
| # Calculate TDNAC term for CIoverlap | ||
| TD_NAC(istep, nst, nbasis, norb, nocc, nvirt, dt, orb_ini, orb_final, nacme, \ | ||
| ao_overlap, mo_coef_old, mo_coef_new, ci_coef_old, ci_coef_new) | ||
|
Comment on lines
+88
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function call is to |
||
|
|
||
| # Assign NACME variables from C to python | ||
| for ist in range(nst): | ||
| for jst in range(nst): | ||
| molecule.nacme[ist, jst] = nacme[ist][jst] | ||
|
|
||
| for iorb in range(norb): | ||
| for ibasis in range(nbasis): | ||
| qm.mo_coef_old[iorb, ibasis] = mo_coef_new[iorb][ibasis] | ||
|
|
||
| for ist in range(nst): | ||
| for iorb in range(nocc): | ||
| for jorb in range(nvirt): | ||
| qm.ci_coef_old[ist, iorb, jorb] = ci_coef_new[ist][iorb][jorb] | ||
|
|
||
| # Deallocate NACME variables | ||
| PyMem_Free(orb_ini) | ||
| PyMem_Free(orb_final) | ||
|
|
||
| for ist in range(nst): | ||
| PyMem_Free(nacme[ist]) | ||
|
|
||
| PyMem_Free(nacme) | ||
|
|
||
| for ibasis in range(nbasis): | ||
| PyMem_Free(ao_overlap[ibasis]) | ||
|
|
||
| for iorb in range(norb): | ||
| PyMem_Free(mo_coef_old[iorb]) | ||
| PyMem_Free(mo_coef_new[iorb]) | ||
|
|
||
| PyMem_Free(ao_overlap) | ||
| PyMem_Free(mo_coef_old) | ||
| PyMem_Free(mo_coef_new) | ||
|
|
||
| for ist in range(nst): | ||
| for iorb in range(nocc): | ||
| PyMem_Free(ci_coef_old[ist][iorb]) | ||
| PyMem_Free(ci_coef_new[ist][iorb]) | ||
|
|
||
| for ist in range(nst): | ||
| PyMem_Free(ci_coef_old[ist]) | ||
| PyMem_Free(ci_coef_new[ist]) | ||
|
|
||
| PyMem_Free(ci_coef_old) | ||
| PyMem_Free(ci_coef_new) | ||
|
Comment on lines
+32
to
+135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The manual memory management with Example: # ... allocations ...
try:
# Assign NACME variables from python to C
# ...
# Calculate TDNAC term for CIoverlap
TD_NAC(...)
# Assign NACME variables from C to python
# ...
finally:
# Deallocate NACME variables
PyMem_Free(orb_ini)
# ... all other PyMem_Free callsThis would make the code more robust against memory leaks. |
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
externblock has several issues:"tdnac.c", but it should probably be"tdnac_orb.c"to use the new orbital-based implementation.TD_NAC, but the function intdnac_orb.cis namedTD_NAC_ORB.TD_NACdeclared here does not match the intended signature ofTD_NAC_ORBintdnac_orb.c(which itself has issues).These issues need to be fixed in conjunction with the issues in
tdnac_orb.c. After fixingtdnac_orb.c, this block should be updated to correctly declareTD_NAC_ORB.