Skip to content

Commit 3873297

Browse files
authored
Add new utility functions (#361)
2 parents 9dfbae6 + c7790a1 commit 3873297

File tree

22 files changed

+84
-31
lines changed

22 files changed

+84
-31
lines changed

MANIFEST.in

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/openmm/metad/Metadynamics-ADP.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@
351351
"outputs": [],
352352
"source": [
353353
"plot_grid = pysages.Grid(lower=(-pi, -pi), upper=(pi, pi), shape=(64, 64), periodic=True)\n",
354-
"xi = (compute_mesh(plot_grid) + 1) / 2 * plot_grid.size + plot_grid.lower\n",
354+
"xi = compute_mesh(plot_grid)\n",
355355
"\n",
356356
"alpha = 1 if method.deltaT is None else (T.value_in_unit(unit.kelvin) + method.deltaT) / method.deltaT\n",
357357
"kT = kB * T.value_in_unit(unit.kelvin)\n",

examples/openmm/metad/Metadynamics-ADP.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ Next we use the biasing potential to estimate the free energy surface. For well-
211211

212212
```python id="6W7Xf0ilqAcm"
213213
plot_grid = pysages.Grid(lower=(-pi, -pi), upper=(pi, pi), shape=(64, 64), periodic=True)
214-
xi = (compute_mesh(plot_grid) + 1) / 2 * plot_grid.size + plot_grid.lower
214+
xi = compute_mesh(plot_grid)
215215

216216
alpha = 1 if method.deltaT is None else (T.value_in_unit(unit.kelvin) + method.deltaT) / method.deltaT
217217
kT = kB * T.value_in_unit(unit.kelvin)

examples/openmm/metad/alanine-dipeptide.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def get_args(argv):
8888
("time-steps", "t", int, 5e5, "Number of simulation steps"),
8989
]
9090
parser = argparse.ArgumentParser(description="Example script to run metadynamics")
91-
for (name, short, T, val, doc) in available_args:
91+
for name, short, T, val, doc in available_args:
9292
parser.add_argument("--" + name, "-" + short, type=T, default=T(val), help=doc)
9393
return parser.parse_args(argv)
9494

@@ -126,7 +126,7 @@ def main(argv=[]):
126126

127127
# generate CV values on a grid to evaluate bias potential
128128
plot_grid = pysages.Grid(lower=(-pi, -pi), upper=(pi, pi), shape=(64, 64), periodic=True)
129-
xi = (compute_mesh(plot_grid) + 1) / 2 * plot_grid.size + plot_grid.lower
129+
xi = compute_mesh(plot_grid)
130130

131131
# determine bias factor depending on method (for standard = 1
132132
# and for well-tempered = (T+deltaT)/deltaT)

examples/openmm/metad/nacl/Metadynamics_NaCl.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@
358358
"source": [
359359
"# generate CV values on a grid to evaluate bias potential\n",
360360
"plot_grid = pysages.Grid(lower=(0), upper=(4), shape=(500), periodic=False)\n",
361-
"xi = (compute_mesh(plot_grid) + 1) / 2 * plot_grid.size + plot_grid.lower\n",
361+
"xi = compute_mesh(plot_grid)\n",
362362
"xi = xi.flatten()\n",
363363
"\n",
364364
"alpha = 1 if method.deltaT is None else (T.value_in_unit(unit.kelvin) + method.deltaT) / method.deltaT\n",

examples/openmm/metad/nacl/Metadynamics_NaCl.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ Next we use the biasing potential to estimate the free energy surface. For well-
221221
```python id="6W7Xf0ilqAcm"
222222
# generate CV values on a grid to evaluate bias potential
223223
plot_grid = pysages.Grid(lower=(0), upper=(4), shape=(500), periodic=False)
224-
xi = (compute_mesh(plot_grid) + 1) / 2 * plot_grid.size + plot_grid.lower
224+
xi = compute_mesh(plot_grid)
225225
xi = xi.flatten()
226226

227227
alpha = 1 if method.deltaT is None else (T.value_in_unit(unit.kelvin) + method.deltaT) / method.deltaT

examples/openmm/metad/nacl/nacl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def get_args(argv):
106106
("time-steps", "t", int, 5e5, "Number of simulation steps"),
107107
]
108108
parser = argparse.ArgumentParser(description="Example script to run metadynamics")
109-
for (name, short, T, val, doc) in available_args:
109+
for name, short, T, val, doc in available_args:
110110
parser.add_argument("--" + name, "-" + short, type=T, default=T(val), help=doc)
111111
return parser.parse_args(argv)
112112

@@ -147,7 +147,7 @@ def main(argv=[]):
147147

148148
# Generate CV values on a grid to evaluate bias potential
149149
plot_grid = pysages.Grid(lower=(0), upper=(4), shape=(500), periodic=False)
150-
xi = (compute_mesh(plot_grid) + 1) / 2 * plot_grid.size + plot_grid.lower
150+
xi = compute_mesh(plot_grid)
151151
xi = xi.flatten()
152152

153153
# Determine bias factor depending on method

examples/openmm/spectral_abf/alanine-dipeptide.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def main(argv=[]):
104104

105105
# generate CV values on a grid to evaluate bias potential
106106
plot_grid = pysages.Grid(lower=(-pi, -pi), upper=(pi, pi), shape=(64, 64), periodic=True)
107-
xi = (compute_mesh(plot_grid) + 1) / 2 * plot_grid.size + plot_grid.lower
107+
xi = compute_mesh(plot_grid)
108108

109109
# Set min free energy to zero
110110
A = fes_fn(xi)

pysages/approxfun/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
collect_exponents,
1515
compute_mesh,
1616
scale,
17+
unit_mesh,
1718
)

pysages/approxfun/core.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class AbstractFit(ABC):
4343
def __init__(self, grid: Grid):
4444
ns = collect_exponents(grid)
4545
self.grid = grid
46-
self.mesh = compute_mesh(grid)
46+
self.mesh = unit_mesh(grid)
4747
self.exponents = ns
4848
self.pinv = pinv(self)
4949

@@ -104,7 +104,15 @@ def scale(x, grid: Grid):
104104
return (x - grid.lower) * 2 / grid.size - 1
105105

106106

107-
def compute_mesh(grid):
107+
def compute_mesh(grid: Grid):
108+
"""
109+
Returns a dense mesh with the same shape and domain as `grid`.
110+
"""
111+
mesh = unit_mesh(grid)
112+
return (mesh + 1) / 2 * grid.size + grid.lower
113+
114+
115+
def unit_mesh(grid: Grid):
108116
"""
109117
Returns a dense mesh with the same shape as `grid`, but on the hypercube [-1, 1]ⁿ,
110118
where `n` is the dimensionality of `grid`. The resulting mesh is Chebyshev-distributed
@@ -224,7 +232,7 @@ def pinv(model: SpectralSobolev1Fit): # noqa: F811 # pylint: disable=C0116,E010
224232
def build_fitter(model: SpectralGradientFit):
225233
"""
226234
Returns a function which takes an approximation `dy` to the gradient
227-
of a function `f` evaluated over the set `x = compute_mesh(model.grid)`,
235+
of a function `f` evaluated over the set `x = unit_mesh(model.grid)`,
228236
and returns a `fun: Fun` object which approximates `f` over
229237
the domain [-1, 1]ⁿ.
230238
"""
@@ -253,7 +261,7 @@ def fit(dy):
253261
def build_fitter(model: SpectralSobolev1Fit): # noqa: F811 # pylint: disable=C0116,E0102
254262
"""
255263
Returns a function which takes approximations `y` and dy` to a function
256-
`f` and its gradient evaluated over the set `x = compute_mesh(model.grid)`,
264+
`f` and its gradient evaluated over the set `x = unit_mesh(model.grid)`,
257265
and in turn returns a `fun: Fun` object which approximates `f` over
258266
the domain [-1, 1]ⁿ.
259267
"""

0 commit comments

Comments
 (0)