-
Notifications
You must be signed in to change notification settings - Fork 1
add numerical polarizability #33
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
Draft
rubengerritsen
wants to merge
2
commits into
master
Choose a base branch
from
polarizability
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env python | ||
"""Example to perform a polarizability calculation.""" | ||
import numpy as np | ||
|
||
from pyvotca import DFTGWBSE, Molecule, NumericalPolarizability | ||
|
||
|
||
def run_polarizability(): | ||
mol = Molecule() | ||
|
||
# define a molecule | ||
mol.add_atom("C", 0, 0, 0) | ||
mol.add_atom("O", 1.3, 0.0, 0.0) | ||
|
||
# initialize a dftgwbse object | ||
votca = DFTGWBSE(mol) | ||
|
||
# setup the polarizability calculator | ||
pol = NumericalPolarizability(votca, dE=0.002) | ||
# run all simulations with different directions of the field | ||
#pol.run_permut() | ||
# perform the actual polarizability calculation | ||
pol_tensor = pol.calc_polarizability("dft_tot") | ||
|
||
print(pol_tensor) | ||
|
||
|
||
if __name__ == "__main__": | ||
run_polarizability() | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
"""Numerical polarizability. | ||
|
||
API | ||
--- | ||
.. autoclass:: NumericalPolarizability | ||
|
||
""" | ||
|
||
import numpy as np | ||
import os | ||
import itertools | ||
import h5py | ||
|
||
from .molecule import Molecule | ||
from .utils import BOHR2ANG | ||
from .xtp import DFTGWBSE | ||
|
||
__all__ = ["NumericalPolarizability"] | ||
|
||
|
||
class NumericalPolarizability: | ||
"""Compute the polarizability numerically using XTP.""" | ||
def __init__(self, | ||
xtp: DFTGWBSE, | ||
dE: float = 0.002, | ||
path_to_simulations: str = './polar_experiments/'): | ||
self.xtp = xtp | ||
self.dE = dE | ||
self.path = path_to_simulations | ||
|
||
def mkfldr(self, path: str): | ||
try: | ||
os.mkdir(path) | ||
except OSError: | ||
print("Creation of the directory %s failed" % path) | ||
|
||
def gen_name(self, name, E) -> str: | ||
return f"{name}_E{E[0]:.4f}E{E[1]:.4f}E{E[2]:.4f}" | ||
|
||
def run_permut(self): | ||
"""Run a VOTCA simulation for every displacement of the electric field with displacement dE in atomic units.""" | ||
# compute all necessary directions of the field (permutations) | ||
b = [self.dE, -self.dE, 0.0] | ||
a = [b, b, b] | ||
perm = [element for element in itertools.product(*a)] | ||
perm = [s for s in perm if all(s) == False] | ||
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. comparing to bool is usually not recommended, instead you can do the following: perm = [s for s in perm if not all(s)] |
||
# create a folder to contain all the results from the experiments | ||
if (not os.path.exists(self.path)): | ||
self.mkfldr(self.path) | ||
counter = 1 | ||
for E in perm: | ||
self.xtp.options.dftpackage = { | ||
"package": { | ||
"use_external_field": "true", | ||
"externalfield": f"{E[0]} {E[1]} {E[2]}" | ||
} | ||
} | ||
name = self.gen_name(self.xtp.mol.name, E) | ||
xtp_withField = DFTGWBSE(self.xtp.mol, | ||
threads=self.xtp.threads, | ||
options=self.xtp.options, | ||
jobname=name, | ||
jobdir=self.path) | ||
print( | ||
f"Running XTP for field: X{E[0]:7.3f} Y{E[1]:7.3f} Z{E[2]:7.3f} {counter}/{len(perm)}" | ||
) | ||
xtp_withField.run() | ||
counter += 1 | ||
|
||
def getEnergiesFromOrbFile(self, kind: str, i: int, j: int, k: int): | ||
""" Read the energies from the orb file for a certain kind of particle/excitation. | ||
|
||
i,j,k represents the direction of the electric field 1,0,0 corresponds to dE,0,0 etc. | ||
OUTPUT: numpy array with the energies of the particle/excitation kind. | ||
""" | ||
nameOrbFile = self.path + self.gen_name( | ||
self.xtp.mol.name, np.array([i * self.dE, j * self.dE, k * self.dE | ||
])) + ".orb" | ||
f = h5py.File(nameOrbFile, 'r') | ||
orb = f['QMdata'] | ||
if (kind == 'BSE_singlet' or kind == 'BSE_triplet' or kind == 'QPdiag'): | ||
return (np.array(orb[kind]['eigenvalues'][()])) | ||
elif (kind == 'QPpert'): | ||
return (np.array(orb['QPpert_energies'][()])) | ||
elif (kind == 'dft_tot'): | ||
return (np.array(orb.attrs['qm_energy'])) | ||
else: | ||
raise Exception( | ||
'Invalid kind, kind of particle/excitation should be one of:' + | ||
' BSE_singlet, BSE_triplet, QPdiag, QPpert and dft_tot\n' + | ||
f'the value of kind was: {kind}') | ||
|
||
def calc_polarizability(self, kind: str, energyLevel=None) -> np.ndarray: | ||
""" Computes the polarizability tensor for a particle/excitation type. | ||
|
||
INPUT: kind of particle/excitation (choices: BSE_singlet, BSE_triplet, | ||
QPdiag, QPpert and dft_tot) and optionally the energy level if | ||
not provided all energy levels will be returned | ||
OUTPUT: numpy array of polarizability tensors. | ||
""" | ||
# Get all the energies from the files (encoding mop = -1 0 1) | ||
ooo = self.getEnergiesFromOrbFile(kind, 0, 0, 0) | ||
moo = self.getEnergiesFromOrbFile(kind, -1, 0, 0) | ||
poo = self.getEnergiesFromOrbFile(kind, 1, 0, 0) | ||
omo = self.getEnergiesFromOrbFile(kind, 0, -1, 0) | ||
opo = self.getEnergiesFromOrbFile(kind, 0, 1, 0) | ||
oom = self.getEnergiesFromOrbFile(kind, 0, 0, -1) | ||
oop = self.getEnergiesFromOrbFile(kind, 0, 0, 1) | ||
|
||
mmo = self.getEnergiesFromOrbFile(kind, -1, -1, 0) | ||
pmo = self.getEnergiesFromOrbFile(kind, 1, -1, 0) | ||
mpo = self.getEnergiesFromOrbFile(kind, -1, 1, 0) | ||
ppo = self.getEnergiesFromOrbFile(kind, 1, 1, 0) | ||
|
||
mom = self.getEnergiesFromOrbFile(kind, -1, 0, -1) | ||
pom = self.getEnergiesFromOrbFile(kind, 1, 0, -1) | ||
mop = self.getEnergiesFromOrbFile(kind, -1, 0, 1) | ||
pop = self.getEnergiesFromOrbFile(kind, 1, 0, 1) | ||
|
||
omm = self.getEnergiesFromOrbFile(kind, 0, -1, -1) | ||
opm = self.getEnergiesFromOrbFile(kind, 0, 1, -1) | ||
omp = self.getEnergiesFromOrbFile(kind, 0, -1, 1) | ||
opp = self.getEnergiesFromOrbFile(kind, 0, 1, 1) | ||
|
||
# Compute second derivatives of the energy with respect to the field | ||
# strength | ||
coeff = (-1. / (self.dE**2)) | ||
axx = coeff * (moo - 2 * ooo + poo) | ||
ayy = coeff * (omo - 2 * ooo + opo) | ||
azz = coeff * (oom - 2 * ooo + oop) | ||
axy = 0.25 * coeff * (mmo - pmo - mpo + ppo) | ||
axz = 0.25 * coeff * (mom - pom - mop + pop) | ||
ayz = 0.25 * coeff * (omm - opm - omp + opp) | ||
|
||
# Create polarization tensor | ||
polar_m = np.squeeze(np.array([[axx, axy, axz], [axy, ayy, ayz], | ||
[axz, ayz, azz]]), | ||
axis=-1) | ||
|
||
# if the entries of the matrix are arrays we need to reshape to get an | ||
# array of matrices | ||
if (axx.size > 1): | ||
polar_m = np.moveaxis(polar_m, [2], [0]) | ||
else: # there can't be an energyLevel | ||
energyLevel = None | ||
|
||
if (energyLevel is None): | ||
return polar_m | ||
else: | ||
return polar_m[energyLevel] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do you want to raise an error whenever the directory already exists? what kind of errors are you trying to catch?
I think that you are looking for something like:
or