Skip to content

Commit

Permalink
Renamed force_extras, and added outputs for force components
Browse files Browse the repository at this point in the history
  • Loading branch information
ceriottm committed Apr 21, 2024
1 parent b37bb6a commit 08e12dc
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
</initialize>
<forces>
<force forcefield='committee' nbeads='1'>
<!-- adding these to force_extras will make the committee extras be treated properly with ring-polymer contraction, ect. -->
<force_extras> [committee_pot, committee_force, committee_virial, baseline_pot, baseline_force, baseline_virial, wb_mixing] </force_extras>
<!-- adding these to interpolate_extras will make the committee extras be treated properly with ring-polymer contraction, ect. -->
<interpolate_extras> [committee_pot, committee_force, committee_virial, baseline_pot, baseline_force, baseline_virial, wb_mixing] </interpolate_extras>
</force>
</forces>
<motion mode='dynamics'>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
</initialize>
<forces>
<force forcefield='committee' nbeads='1'>
<!-- adding these to force_extras will make the committee extras be treated properly with ring-polymer contraction, ect. -->
<force_extras> [committee_pot, committee_force, committee_virial, baseline_pot, baseline_force, baseline_virial, wb_mixing] </force_extras>
<!-- adding these to interpolate_extras will make the committee extras be treated properly with ring-polymer contraction, ect. -->
<interpolate_extras> [committee_pot, committee_force, committee_virial, baseline_pot, baseline_force, baseline_virial, wb_mixing] </interpolate_extras>
</force>
</forces>
<motion mode='dynamics'>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<temperature units="kelvin"> 300.0 </temperature>
<bias>
<force forcefield="plumed" nbeads="1">
<force_extras> [ doo, mtd.bias ] </force_extras>
<interpolate_extras> [ doo, mtd.bias ] </interpolate_extras>
</force>
</bias>
</ensemble>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<simulation mode='md' verbosity='high'>
<output prefix='simulation'>
<properties stride='1' filename='out'> [ step, time{picosecond}, conserved, temperature{kelvin}, kinetic_cv, potential, pressure_cv{megapascal} ] </properties>
<trajectory filename='pos' stride='1'> positions </trajectory>
<properties stride='1' filename='out'> [ step, time{picosecond}, conserved, temperature{kelvin}, kinetic_cv, potential, pressure_cv{megapascal}, pot_component_raw(0), pot_component_raw(1) ] </properties>
<trajectory filename='pos' stride='10'> positions </trajectory>
<trajectory filename='f-0c' stride='10'> forces_component_raw(0) </trajectory>
<checkpoint stride='200'/>
</output>
<total_steps>100</total_steps>
Expand Down
4 changes: 2 additions & 2 deletions examples/temp/pes/pswater/combine-extras/input.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
</initialize>
<forces>
<force forcefield='driver' weight="1.0">
<force_extras> ['dipole'] </force_extras>
<interpolate_extras> ['dipole'] </interpolate_extras>
</force>
<force forcefield='driver2' weight="2.0" >
<force_extras> ['dipole'] </force_extras>
<interpolate_extras> ['dipole'] </interpolate_extras>
</force>
</forces>
<ensemble>
Expand Down
2 changes: 1 addition & 1 deletion examples/temp/pes/pswater/contract-extras/input.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<!-- The line below is the one that tells i-PI that the 'dipole' extras quantity will be
treated as a physical quantity like forces. It can then be interpolated, and if there are
several force components, it will be automatically summed with the respective weights -->
<force_extras> [dipole] </force_extras>
<interpolate_extras> [dipole] </interpolate_extras>
</force>
</forces>
<ensemble>
Expand Down
26 changes: 13 additions & 13 deletions ipi/engine/forces.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def __init__(
weight=1.0,
name="",
mts_weights=None,
force_extras=None,
interpolate_extras=None,
epsilon=-0.001,
):
"""Initializes ForceComponent
Expand All @@ -316,7 +316,7 @@ def __init__(
will be weighted by this factor. The combination is a weighted sum.
name: The name of the forcefield.
mts_weights: Weight of forcefield at each mts level.
force_extras: A list of properties that should be treated as physical quantities,
interpolate_extras: A list of properties that should be treated as physical quantities,
converted to numpy arrays and treated with ring polymer contraction. If
different force components have this field, they will also be summed with
the respective weight like a forces object.
Expand All @@ -330,10 +330,10 @@ def __init__(
self.mts_weights = np.asarray([])
else:
self.mts_weights = np.asarray(mts_weights)
if force_extras is None:
self.force_extras = []
if interpolate_extras is None:
self.interpolate_extras = []
else:
self.force_extras = force_extras
self.interpolate_extras = interpolate_extras
self.epsilon = epsilon

def bind(self, beads, cell, fflist, output_maker):
Expand Down Expand Up @@ -464,21 +464,21 @@ def extra_gather(self):
)
fc_extra[e].append(b.extra[e])

# force_extras should be numerical, thus can be converted to numpy arrays.
# interpolate_extras should be numerical, thus can be converted to numpy arrays.
# we enforce the type and numpy will raise an error if not.
for e in self.force_extras:
for e in self.interpolate_extras:
try:
fc_extra[e] = np.asarray(fc_extra[e], dtype=float)
except KeyError:
raise KeyError(
"force_extras required "
"interpolate_extras required "
+ e
+ " to promote, but was not found among extras "
+ str(list(fc_extra.keys()))
)
except:
raise Exception(
"force_extras has to be numerical to be treated as a physical quantity. It is not -- check the quantity that is being passed."
"interpolate_extras has to be numerical to be treated as a physical quantity. It is not -- check the quantity that is being passed."
)
return fc_extra

Expand Down Expand Up @@ -577,7 +577,7 @@ def __init__(self, baseforce, scaling=1):
self._weight = depend_value(name="weight", value=0)
dpipe(self.bf._weight, self._weight)
self.mts_weights = self.bf.mts_weights
self.force_extras = self.bf.force_extras
self.interpolate_extras = self.bf.interpolate_extras

def get_pots(self):
return (
Expand Down Expand Up @@ -813,7 +813,7 @@ def make_rpc(rpc, beads):
nbeads=newb,
weight=fc.weight,
mts_weights=fc.mts_weights,
force_extras=fc.force_extras,
interpolate_extras=fc.interpolate_extras,
epsilon=fc.epsilon,
)
newbeads = Beads(beads.natoms, newb)
Expand Down Expand Up @@ -1450,8 +1450,8 @@ def extra_combine(self):
for k in range(self.nforces):
# combines the extras from the different force components
for e, v in self.mforces[k].extras.items():
if e in self.mforces[k].force_extras:
# extras that are tagged as force_extras are treated exactly as if they were an energy/force/stress
if e in self.mforces[k].interpolate_extras:
# extras that are tagged as interpolate_extras are treated exactly as if they were an energy/force/stress
v = (
self.mforces[k].weight
* self.mforces[k].mts_weights.sum()
Expand Down
30 changes: 30 additions & 0 deletions ipi/engine/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -2833,6 +2833,36 @@ def __init__(self):
is printed verbatim. Will print out one file per bead, unless the bead attribute is set by the user.""",
"func": (lambda: self.system.forces.extras),
},
"forces_component": {
"dimension": "force",
"help": """The contribution to the system forces from one of the force components.
Takes one mandatory argument index (zero-based) that indicates which component of the
potential must be returned. The optional argument 'bead' will print the potential associated
with the specified bead, otherwise the centoid force is computed.
If the potential is weighed, the weight will be applied. """,
"func": lambda index, bead="-1": (
self.system.forces.forces_component(int(index)).sum(axis=0)
/ self.system.beads.nbeads
if int(bead) < 0
else self.system.forces.forces_component(int(index))[int(bead)]
),
},
"forces_component_raw": {
"dimension": "force",
"help": """The contribution to the system forces from one of the force components.
Takes one mandatory argument index (zero-based) that indicates which component of the
potential must be returned. The optional argument 'bead' will print the potential associated
with the specified bead, otherwise the centoid force is computed.
If the potential is weighed, the weight will be applied. """,
"func": lambda index, bead="-1": (
self.system.forces.forces_component(int(index), False).sum(axis=0)
/ self.system.beads.nbeads
if int(bead) < 0
else self.system.forces.forces_component(int(index), False)[
int(bead)
]
),
},
"extras_component": {
"help": """The additional data returned by the client code, printed verbatim or expanded as a dictionary. See "extras".
Fetches the extras from a specific force component, indicated in parentheses [extras_component(idx)]. """,
Expand Down
6 changes: 3 additions & 3 deletions ipi/inputs/forces.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class InputForceComponent(Input):
"dimension": "force",
},
),
"force_extras": (
"interpolate_extras": (
InputArray,
{
"dtype": str,
Expand Down Expand Up @@ -107,7 +107,7 @@ def store(self, forceb):
self.nbeads.store(forceb.nbeads)
self.weight.store(forceb.weight)
self.mts_weights.store(forceb.mts_weights)
self.force_extras.store(forceb.force_extras)
self.interpolate_extras.store(forceb.interpolate_extras)
self.fd_epsilon.store(forceb.epsilon)
self.name.store(forceb.name)
self.forcefield.store(forceb.ffield)
Expand All @@ -126,7 +126,7 @@ def fetch(self):
weight=self.weight.fetch(),
name=self.name.fetch(),
mts_weights=self.mts_weights.fetch(),
force_extras=self.force_extras.fetch(),
interpolate_extras=self.interpolate_extras.fetch(),
epsilon=self.fd_epsilon.fetch(),
)

Expand Down

0 comments on commit 08e12dc

Please sign in to comment.