git-svn-id: svn://svn.icms.temple.edu/lammps-ro/trunk@16053 f3b2605a-c512-4ea7-a41b...
[lammps.git] / tools / i-pi / ipi / inputs / beads.py
blobf4caafcbb99b27b518ae694c2b9199f8237c7cc5
1 """Deals with creating the beads class.
3 Copyright (C) 2013, Joshua More and Michele Ceriotti
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http.//www.gnu.org/licenses/>.
19 Classes:
20 InputBeads: Deals with creating the Beads object from a file, and
21 writing the checkpoints.
22 """
24 import numpy as np
25 from ipi.engine.beads import *
26 from ipi.engine.atoms import Atoms
27 from ipi.utils.inputvalue import *
28 from ipi.utils.depend import *
29 from ipi.utils.units import *
30 from ipi.inputs.atoms import *
32 __all__ = ['InputBeads']
34 class InputBeads(Input):
35 """Beads input class.
37 Handles generating the appropriate beads class from the xml input file,
38 and generating the xml checkpoint tags and data from an instance of the
39 object.
41 Attributes:
42 nbeads: An optional integer giving the number of beads. Defaults to 0.
43 natoms: An optional integer giving the number of atoms. Defaults to 0.
45 Fields:
46 q: An optional array giving the bead positions. Defaults to an empty
47 array with no elements.
48 p: An optional array giving the bead momenta. Defaults to an empty
49 array with no elements.
50 m: An optional array giving the bead masses. Defaults to an empty array
51 with no elements.
52 names: An optional array giving the bead names. Defaults to an empty
53 array with no elements.
54 """
56 attribs = { "natoms" : (InputAttribute, {"dtype" : int, "default" : 0,
57 "help" : "The number of atoms."}),
58 "nbeads" : (InputAttribute, {"dtype" : int, "default" : 0,
59 "help" : "The number of beads."})
61 fields={ "q" : (InputArray, {"dtype" : float,
62 "default" : input_default(factory=np.zeros, args = (0,)),
63 "help" : "The positions of the beads. In an array of size [nbeads, 3*natoms].",
64 "dimension" : "length"}),
65 "p" : (InputArray, {"dtype" : float,
66 "default" : input_default(factory=np.zeros, args = (0,)),
67 "help" : "The momenta of the beads. In an array of size [nbeads, 3*natoms].",
68 "dimension" : "momentum"}),
69 "m" : (InputArray, {"dtype" : float,
70 "default" : input_default(factory=np.zeros, args = (0,)),
71 "help" : "The masses of the atoms, in the format [m1, m2, ... ].",
72 "dimension" : "mass"}),
73 "names" : (InputArray, {"dtype" : str,
74 "default" : input_default(factory=np.zeros, args=(0,), kwargs={'dtype': np.dtype('|S6')}),
75 "help" : "The names of the atoms, in the format [name1, name2, ... ]."}) }
77 default_help = "Describes the bead configurations in a path integral simulation."
78 default_label = "BEADS"
81 def store(self, beads):
82 """Takes a Beads instance and stores a minimal representation of it.
84 Args:
85 beads: A Beads object from which to initialise from.
86 """
88 super(InputBeads,self).store()
89 self.natoms.store(beads.natoms)
90 self.nbeads.store(beads.nbeads)
92 self.q.store(depstrip(beads.q))
93 self.p.store(depstrip(beads.p))
94 self.m.store(depstrip(beads.m))
95 self.names.store(depstrip(beads.names))
97 def fetch(self):
98 """Creates a beads object.
100 Returns:
101 A beads object of the appropriate type and with the appropriate
102 properties given the attributes of the InputBeads object.
105 super(InputBeads,self).fetch()
106 beads = Beads(self.natoms.fetch(),self.nbeads.fetch())
108 # tries to fill up with as much data as available and valid
109 q = self.q.fetch()
110 if (q.shape == (beads.nbeads,3*beads.natoms)):
111 beads.q = q
112 elif (beads.nbeads == 1 and q.shape == (3*beads.natoms,)):
113 beads.q = q
114 elif len(q) != 0:
115 raise ValueError("Array shape mismatches for q in <beads> input.")
117 p = self.p.fetch()
118 if (p.shape == (beads.nbeads,3*beads.natoms)):
119 beads.p = p
120 elif (beads.nbeads == 1 and p.shape == (3*beads.natoms,)):
121 beads.p = p
122 elif len(p) != 0:
123 raise ValueError("Array shape mismatches for p in <beads> input.")
125 m = self.m.fetch()
126 if (m.shape == (beads.natoms,)):
127 beads.m = m
128 elif len(m) != 0:
129 raise ValueError("Array shape mismatches for m in <beads> input.")
131 n = self.names.fetch()
132 if (n.shape == (beads.natoms,)):
133 beads.names = n
134 elif len(n) != 0:
135 raise ValueError("Array shape mismatches for names in <beads> input.")
137 return beads