root / ase / calculators / singlepoint.py @ 8
Historique | Voir | Annoter | Télécharger (2,16 ko)
1 |
import numpy as np |
---|---|
2 |
|
3 |
|
4 |
class SinglePointCalculator: |
5 |
"""Special calculator for a single configuration.
|
6 |
|
7 |
Used to remember the energy, force and stress for a given
|
8 |
configuration. If the positions, atomic numbers, unit cell, or
|
9 |
boundary conditions are changed, then asking for
|
10 |
energy/forces/stress will raise an exception."""
|
11 |
|
12 |
def __init__(self, energy, forces, stress, magmoms, atoms): |
13 |
"""Save energy, forces and stresses for the current configuration."""
|
14 |
self.energy = energy
|
15 |
if forces is not None: |
16 |
forces = np.array(forces, float)
|
17 |
self.forces = forces
|
18 |
if stress is not None: |
19 |
stress = np.array(stress, float)
|
20 |
self.stress = stress
|
21 |
if magmoms is not None: |
22 |
magmoms = np.array(magmoms, float)
|
23 |
self.magmoms = magmoms
|
24 |
self.atoms = atoms.copy()
|
25 |
|
26 |
def calculation_required(self, atoms, quantities): |
27 |
ok = self.atoms == atoms
|
28 |
return ('forces' in quantities and (self.forces is None or not ok) or |
29 |
'energy' in quantities and (self.energy is None or not ok) or |
30 |
'stress' in quantities and (self.stress is None or not ok) or |
31 |
'magmoms' in quantities and (self.magmoms is None or not ok)) |
32 |
|
33 |
def update(self, atoms): |
34 |
if self.atoms != atoms: |
35 |
raise RuntimeError('Energy, forces and stress no longer correct.') |
36 |
|
37 |
def get_potential_energy(self, atoms): |
38 |
self.update(atoms)
|
39 |
if self.energy is None: |
40 |
raise RuntimeError('No energy.') |
41 |
return self.energy |
42 |
|
43 |
def get_forces(self, atoms): |
44 |
self.update(atoms)
|
45 |
if self.forces is None: |
46 |
raise RuntimeError('No forces.') |
47 |
return self.forces |
48 |
|
49 |
def get_stress(self, atoms): |
50 |
self.update(atoms)
|
51 |
if self.stress is None: |
52 |
raise NotImplementedError |
53 |
return self.stress |
54 |
|
55 |
def get_spin_polarized(self): |
56 |
return self.magmoms is not None and self.magmoms.any() |
57 |
|
58 |
def get_magnetic_moments(self, atoms): |
59 |
self.update(atoms)
|
60 |
if self.magmoms is not None: |
61 |
return self.magmoms |
62 |
else:
|
63 |
return np.zeros(len(self.positions)) |