Skip to content

Commit f1e9b0e

Browse files
authored
Merge pull request #93 from chjacob-tubs/master
Adding python interface for calculation of energy density only
2 parents a486a3f + 9eb8068 commit f1e9b0e

File tree

6 files changed

+428
-51
lines changed

6 files changed

+428
-51
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ install:
190190
# https://github.com/pypa/pipenv/issues/3224
191191
pip install --user --upgrade -e git+https://github.com/pypa/pipenv.git#egg=pipenv
192192
fi
193-
- pipenv $PYTHON install
193+
- pipenv $PYTHON install --dev
194194

195195
before_script:
196196
- source $(pipenv --venv)/bin/activate

Pipfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ numpy = "*"
1515
Sphinx = "*"
1616
sphinx_rtd_theme = "*"
1717
fprettify = "*"
18+
pathlib2 = "*"
19+
pytest = "*"
20+

python/xcfun.py

Lines changed: 118 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,49 @@ def type(self):
8989
else:
9090
return 0
9191

92+
def eval_energy_n(self, density, densgrad=None):
93+
"""
94+
Evaluate the xc potential (spin-compensated case).
95+
96+
input:
97+
density: 1D-numpy.array[0:nr_of_points],
98+
density at grid points
99+
densgrad: 2D-numpy.array[0:nr_of_points, 0:3],
100+
density gradient at grid points (1st index),
101+
2nd index: 0 - x, 1 - y, 2 - z
102+
- only required for GGA functionals
103+
output:
104+
return value: energy density in 2D-numpy array[0:nr_of_points]
105+
"""
106+
107+
if not (len(density.shape) == 1):
108+
raise XCFunException('Wrong shape of density argument in eval_energy_n '
109+
'[ %s instead of (nr_points,) ]' % str(density.shape))
110+
nr_points = density.size
111+
112+
if xc_is_metagga(self._func):
113+
raise XCFunException('xc potential not supported for meta-GGAs')
114+
elif xc_is_gga(self._func):
115+
if (densgrad is None):
116+
raise XCFunException('Density gradient required for GGA energy')
117+
118+
xc_eval_setup(self._func, XC_N_NX_NY_NZ, XC_PARTIAL_DERIVATIVES, 0)
119+
120+
if not (densgrad.shape == (nr_points, 3)):
121+
raise XCFunException('Wrong shape of densgrad argument in eval_energy_n '
122+
'[ %s instead of (%i, 3) ]' % (str(densgrad.shape), nr_points))
123+
124+
dens = numpy.zeros((density.size, 4))
125+
dens[:, 0] = density[:]
126+
dens[:, 1:4] = densgrad[:, 0:3]
127+
128+
else:
129+
xc_eval_setup(self._func, XC_N, XC_PARTIAL_DERIVATIVES, 0)
130+
131+
dens = density.reshape((density.size, 1))
132+
133+
return xc_eval(self._func, dens)[:,0]
134+
92135
def eval_potential_n(self, density, densgrad=None, denshess=None):
93136
"""
94137
Evaluate the xc potential (spin-compensated case).
@@ -109,23 +152,26 @@ def eval_potential_n(self, density, densgrad=None, denshess=None):
109152
2nd index: 0 - energy density, 1 - total xc potential
110153
"""
111154

155+
if not (len(density.shape) == 1):
156+
raise XCFunException('Wrong shape of density argument in eval_potential_n '
157+
'[ %s instead of (nr_points,) ]' % str(density.shape))
158+
nr_points = density.size
159+
112160
if xc_is_metagga(self._func):
113161
raise XCFunException('xc potential not supported for meta-GGAs')
114162
elif xc_is_gga(self._func):
115163
if (densgrad is None) or (denshess is None):
116-
raise XCFunException('Density gradient and Laplacian required for GGA potential')
164+
raise XCFunException('Density gradient and Hessian required for GGA potential')
117165

118166
xc_eval_setup(self._func, XC_N_2ND_TAYLOR, XC_POTENTIAL, 1)
119167

120-
if not (len(density.shape) == 1):
121-
raise XCFunException('Wrong shape of density argument in eval_potential_n')
122-
nr_points = density.size
123-
124168
if not (densgrad.shape == (nr_points, 3)):
125-
raise XCFunException('Wrong shape of densgrad argument in eval_potential_n')
169+
raise XCFunException('Wrong shape of densgrad argument in eval_potential_n '
170+
'[ %s instead of (%i, 3) ]' % (str(densgrad.shape), nr_points))
126171

127172
if not (denshess.shape == (nr_points, 6)):
128-
raise XCFunException('Wrong shape of denshess argument in eval_potential_n')
173+
raise XCFunException('Wrong shape of denshess argument in eval_potential_n '
174+
'[ %s instead of (%i, 6) ]' % (str(denshess.shape), nr_points))
129175

130176
dens = numpy.zeros((density.size, 10))
131177
dens[:, 0] = density[:]
@@ -139,6 +185,57 @@ def eval_potential_n(self, density, densgrad=None, denshess=None):
139185

140186
return xc_eval(self._func, dens)
141187

188+
def eval_energy_ab(self, density, densgrad=None):
189+
"""
190+
Evaluate the xc potential (spin-resolved case).
191+
192+
input:
193+
density: 1D-numpy.array[0:nr_of_points,0:2],
194+
density at grid points (1st index),
195+
2nd index: 0 - alpha density, 1 - beta density
196+
densgrad: 2D-numpy.array[0:nr_of_points, 0:3, 0:2],
197+
density gradient at grid points (1st index),
198+
2nd index: 0 - x, 1 - y, 2 - z,
199+
3rd index: 0 - alpha density gradien, 1 - beta density gradient,
200+
- only required for GGA functionals
201+
output:
202+
return value: energy density in 2D-numpy array[0:nr_of_points]
203+
"""
204+
205+
if not (len(density.shape) == 2):
206+
raise XCFunException('Wrong shape of density argument in eval_energy_ab '
207+
'[ %s instead of (nr_points, 2) ]' % str(density.shape))
208+
nr_points = density.shape[0]
209+
210+
if not (density.shape == (nr_points, 2)):
211+
raise XCFunException('Wrong shape of density argument in eval_energy_ab '
212+
'[ %s instead of (nr_points, 2) ]' % str(density.shape))
213+
214+
if xc_is_metagga(self._func):
215+
raise XCFunException('xc potential not supported for meta-GGAs')
216+
elif xc_is_gga(self._func):
217+
if (densgrad is None):
218+
raise XCFunException('Density gradient required for GGA energy')
219+
220+
xc_eval_setup(self._func, XC_A_B_AX_AY_AZ_BX_BY_BZ, XC_PARTIAL_DERIVATIVES, 0)
221+
222+
if not (densgrad.shape == (nr_points, 3, 2)):
223+
raise XCFunException('Wrong shape of densgrad argument in eval_energy_ab '
224+
'[ %s instead of (%i, 3, 2) ]' % (str(densgrad.shape), nr_points))
225+
226+
dens = numpy.zeros((density.shape[0], 8))
227+
dens[:, 0] = density[:, 0]
228+
dens[:, 1] = density[:, 1]
229+
dens[:, 2:5] = densgrad[:, 0:3, 0]
230+
dens[:, 5:8] = densgrad[:, 0:3, 1]
231+
232+
else:
233+
xc_eval_setup(self._func, XC_A_B, XC_PARTIAL_DERIVATIVES, 0)
234+
235+
dens = density
236+
237+
return xc_eval(self._func, dens)[:,0]
238+
142239
def eval_potential_ab(self, density, densgrad=None, denshess=None):
143240
"""
144241
Evaluate the xc potential (spin-resolved case).
@@ -162,26 +259,30 @@ def eval_potential_ab(self, density, densgrad=None, denshess=None):
162259
2nd index: 0 - energy density, 1 - alpha xc potential, 2 - beta xc potential
163260
"""
164261

262+
if not (len(density.shape) == 2):
263+
raise XCFunException('Wrong shape of density argument in eval_potential_ab '
264+
'[ %s instead of (nr_points, 2) ]' % str(density.shape))
265+
nr_points = density.shape[0]
266+
267+
if not (density.shape == (nr_points, 2)):
268+
raise XCFunException('Wrong shape of density argument in eval_potential_n '
269+
'[ %s instead of (nr_points, 2) ]' % str(density.shape))
270+
165271
if xc_is_metagga(self._func):
166272
raise XCFunException('xc potential not supported for meta-GGAs')
167273
elif xc_is_gga(self._func):
168274
if (densgrad is None) or (denshess is None):
169-
raise XCFunException('Density gradient and Laplacian required for GGA potential')
275+
raise XCFunException('Density gradient and Hessian required for GGA potential')
170276

171277
xc_eval_setup(self._func, XC_A_B_2ND_TAYLOR, XC_POTENTIAL, 1)
172278

173-
if not (len(density.shape) == 2):
174-
raise XCFunException('Wrong shape of density argument in eval_potential_ab')
175-
nr_points = density.shape[0]
176-
177-
if not (density.shape == (nr_points, 2)):
178-
raise XCFunException('Wrong shape of density argument in eval_potential_n')
179-
180279
if not (densgrad.shape == (nr_points, 3, 2)):
181-
raise XCFunException('Wrong shape of densgrad argument in eval_potential_n')
280+
raise XCFunException('Wrong shape of densgrad argument in eval_potential_n '
281+
'[ %s instead of (%i, 3, 2) ]' % (str(densgrad.shape), nr_points))
182282

183283
if not (denshess.shape == (nr_points, 6, 2)):
184-
raise XCFunException('Wrong shape of denshess argument in eval_potential_n')
284+
raise XCFunException('Wrong shape of denshess argument in eval_potential_n '
285+
'[ %s instead of (%i, 6, 2) ]' % (str(denshess.shape), nr_points))
185286

186287
dens = numpy.zeros((density.shape[0], 20))
187288
dens[:, 0] = density[:, 0]

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ sphinx
22
sphinx_rtd_theme
33
fprettify
44
numpy
5+
pathlib2
6+
pytest

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ endif()
3535
if(ENABLE_PYTHON_INTERFACE)
3636
add_test(
3737
NAME python-interface
38-
COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/test/test.py
38+
COMMAND ${PYTHON_EXECUTABLE} -m pytest ${PROJECT_SOURCE_DIR}/test/test.py
3939
)
4040
set_tests_properties(python-interface
4141
PROPERTIES

0 commit comments

Comments
 (0)