Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions CHANGES/v2.11.md
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,14 @@ The entries below give the details; the main cases are:
e.g. with [REFERENCE_GRID](REFERENCE_GRID.md) or in [KL_ENTROPY](KL_ENTROPY.md) ([RDF](RDF.md), [PAIRENTROPY](PAIRENTROPY.md) and [MULTICOLVARDENS](MULTICOLVARDENS.md) use it).
[ACCUMULATE](ACCUMULATE.md), [MARGINAL](MARGINAL.md), [INTERPOLATE_GRID](INTERPOLATE_GRID.md), [FOURIER_TRANSFORM](FOURIER_TRANSFORM.md) and the contour actions follow the extensions of their input grid,
and the grid of KDE and the data of ACCUMULATE are stored in the checkpoint (see [CHECKPOINT](CHECKPOINT.md)).
- The memory used by [KDE](KDE.md) and [HISTOGRAM](HISTOGRAM.md) grows with the number of pairs of a kernel and a grid point where the kernel is not zero,
instead of the number of kernels times the number of grid points (e.g. 1000 Gaussians in 3D on a grid of about 80x80x80 points: 160 MB instead of 4.2 GB,
and 10000 kernels on a grid of 400x450 points no longer run out of memory); they are also faster. On arm64 the kernels centred outside a grid with `NO_EXTEND`
were also added to points beyond their support: this is fixed. The spline interpolation of a grid ([EVALUATE_FUNCTION_FROM_GRID](EVALUATE_FUNCTION_FROM_GRID.md),
[INTERPOLATE_GRID](INTERPOLATE_GRID.md)) is about twice as fast, and is right at the upper end of a periodic argument (e.g. at pi).
- [REFERENCE_GRID](REFERENCE_GRID.md) reads the grids written by [METAD](METAD.md) and [PBMETAD](PBMETAD.md) (GRID_WFILE) and the free energies of [sum_hills](sum_hills.md), and
[EXTERNAL](EXTERNAL.md) and the grids read by [METAD](METAD.md) (GRID_RFILE) can be files written by [DUMPGRID](DUMPGRID.md): the two kinds of grid file differ in the meaning
of `nbins` (points or bins) and in the names of the derivatives, and the grid readers recognise both. The files written are unchanged.
- [FOURIER_TRANSFORM](FOURIER_TRANSFORM.md) of a grid with a different number of points along the two arguments wrote outside its arrays (crash or wrong transform).
- The `CUTOFF` keyword of [DISTANCE_MATRIX](DISTANCE_MATRIX.md) is now `LINKCELL_CUTOFF`, to make its purpose clearer (`CUTOFF` is still read, with a warning).
- [COMBINE](COMBINE.md) has a new CIRCULAR flag to combine periodic arguments as angles: the output is the argument of $\sum_i c_i e^{i p_i \theta_i}$
Expand Down Expand Up @@ -847,6 +855,19 @@ The entries below give the details; the main cases are:
- `ActionWithGrid` moved from `src/gridtools` to `src/core` and `GridCoordinatesObject` from `src/gridtools` to `src/tools`; both are now in the namespace `PLMD`
(they were in `PLMD::gridtools`): include `core/ActionWithGrid.h` and `tools/GridCoordinatesObject.h`. A grid of an `ActionWithGrid` can change size during the run
(KDE extends it): an action that sets up its output from its input grid must do it again when the number of points of the input changes.
- `tools/GridExtension` plans the extensions of the grids of METAD/PBMETAD and KDE (same rules: the points needed plus at least a quarter of the points,
`GRID_SIZE_LIMIT`). `GridCoordinatesObject` keeps its bounds as doubles: `setLattice` sets a grid without parsing strings, `extend` adds points on the same lattice
(as `GridBase::resize`), `onSameLattice` and `getShiftFrom` give the exact shift between a grid and its extension, `writeCheckpoint` and `readCheckpoint` store it.
- `tools/GridLattice` is the geometry of a grid without data (range, spacing, periodicity, indices, points, neighbours), with the dimension-templated code that was in `GridBase`.
`GridBase` (METAD, PBMETAD, EXTERNAL, ves...) derives from it and the flat grids of `GridCoordinatesObject` (gridtools) contain one, so that both use the same code.
The indices of a point along a periodic direction are brought back into the grid, and the neighbours of a point outside the grid are computed from its own lattice position
(`GridCoordinatesObject` converted a negative position to an unsigned number: on arm64 the neighbours of a kernel centred below `GRID_MIN` were those of the first point).
- The indices of the points of `GridCoordinatesObject` are `std::size_t` (`getIndex`, `getNumberOfPoints`, `getNeighbors`, `getSplineNeighbors`, `getStride`...), as in `GridBase`.
- `GridLattice::splineInterpolation` is the spline interpolation of `GridBase` (METAD, EXTERNAL, ves...) and of the gridtools `Interpolator` (they were two copies of the same code),
with the values and derivatives read through a function given by the caller.
- `tools/GridFile` writes and reads the grid files of `GridBase` (METAD, EXTERNAL, ves...) and of [DUMPGRID](DUMPGRID.md) and [REFERENCE_GRID](REFERENCE_GRID.md):
`GridFileWriter` writes either style (`nbins` the number of points and derivatives `der_x`, or `nbins` the number of bins and derivatives `d<function>_x`),
`GridFileReader` reads both and gives the number of bins; `GridBase::writeHeader` has been removed.
- Module declarations are consolidated in `module.yml` (`plugin.yml` for plugins):
`type` replaces `module.type`, `use` replaces Makefile `USE=`, and `conda` replaces `conda.mk`.
Native build fields use the single-line syntax documented in `conda/module-options.md`.
Expand Down
1 change: 1 addition & 0 deletions regtest/basic/rt-make-gridextension/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../scripts/test.make
1 change: 1 addition & 0 deletions regtest/basic/rt-make-gridextension/config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type=make
85 changes: 85 additions & 0 deletions regtest/basic/rt-make-gridextension/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "plumed/tools/GridExtension.h"
#include "plumed/tools/GridCoordinatesObject.h"
#include "testUtils.h"
#include <iostream>
#include <limits>
#include <string>
#include <vector>

using namespace PLMD;
using Mode=GridExtension::Mode;

template <typename T>
std::string str(const std::vector<T>& v) {
std::string s;
for(unsigned i=0; i<v.size(); ++i) {
s+=(i>0?" ":"")+std::to_string(v[i]);
}
return s;
}

void print(tee& out, const std::string& name, const GridExtension::Plan& p) {
out << name << ": changed " << p.changed << " lower " << str(p.lower) << " upper " << str(p.upper)
<< " npoints " << p.npoints << " toolarge " << p.toolarge
<< " nonfinite " << (p.nonfinite==std::string::npos ? std::string("none") : std::to_string(p.nonfinite)) << "\n";
}

void print(tee& out, const std::string& name, const GridCoordinatesObject& g) {
out << name << ": min";
for(const auto& s : g.getMin()) {
out << " " << s;
}
out << " max";
for(const auto& s : g.getMax()) {
out << " " << s;
}
out << " points " << str(g.getNbin(true)) << " total " << g.getNumberOfPoints() << "\n";
}

int main() {
tee out("output");
out << "checkSizeLimit(0.5): " << GridExtension::checkSizeLimit(0.5) << "\n";
out << "checkSizeLimit(1e10): " << GridExtension::checkSizeLimit(1e10) << "\n";
out << "checkSizeLimit(1e8): '" << GridExtension::checkSizeLimit(1e8) << "'\n";

GridExtension e;
out << "raised " << e.setSizeLimit(1000,{11,11}) << " limit " << e.getSizeLimit() << "\n";
// nothing needed beyond the grid (101 points: indices 0..100)
print(out,"inside",e.plan({101},{Mode::extend},{0},{100}));
// two points missing below: growth to a quarter of the points (26)
print(out,"below",e.plan({101},{Mode::extend},{-2},{50}));
// 40 points missing above: more than the growth
print(out,"above",e.plan({101},{Mode::extend},{10},{140}));
// periodic (fixed) direction and an extended one
print(out,"fixed",e.plan({20,11},{Mode::fixed,Mode::extend},{-5,-1},{30,10}));
// the growth would exceed the limit: only the missing points
print(out,"fallback",e.plan({30,30},{Mode::extend,Mode::extend},{-1,0},{29,29}));
// too large even without growth
print(out,"toolarge",e.plan({30,30},{Mode::extend,Mode::extend},{-10,0},{29,29}));
// placement: exactly the needed range, also fewer points than now
print(out,"place",e.plan({2},{Mode::place},{3},{9}));
print(out,"place-shift",e.plan({2},{Mode::place},{-7},{-3}));
print(out,"nonfinite",e.plan({11,11},{Mode::extend,Mode::extend},{0,std::numeric_limits<double>::quiet_NaN()},{10,10}));
print(out,"far",e.plan({11},{Mode::extend},{-1e16},{10}));

// a grid on a lattice, extended as GridBase::resize
GridCoordinatesObject g;
g.setup("flat",{false,true},0,0.0);
g.setLattice({-1.0,-3.14159265358979},{0.1,3.14159265358979/10},{20,20},{"-1","-pi"},{"1","pi"});
print(out,"lattice",g);
out << "lattice coordinate of 0.25: " << g.getLatticeCoordinate(0,0.25) << "\n";
GridCoordinatesObject h(g);
h.extend({5,0},{-2,0});
print(out,"extended",h);
out << "same lattice " << h.onSameLattice(g) << " shift " << str(h.getShiftFrom(g)) << " back " << str(g.getShiftFrom(h)) << "\n";
GridCoordinatesObject k(h);
k.extend({-3,0},{7,0});
out << "same lattice " << k.onSameLattice(g) << " shift " << str(k.getShiftFrom(g)) << "\n";
GridCoordinatesObject other;
other.setup("flat",{false,true},0,0.0);
other.setLattice({-1.0,-3.14159265358979},{0.1,3.14159265358979/10},{20,20},{"-1","-pi"},{"1","pi"});
out << "a new lattice with the same numbers: same lattice " << other.onSameLattice(g) << "\n";
other.setLattice({-1.05,-3.14159265358979},{0.1,3.14159265358979/10},{20,20});
out << "a shifted lattice: same lattice " << other.onSameLattice(g) << "\n";
return 0;
}
21 changes: 21 additions & 0 deletions regtest/basic/rt-make-gridextension/output.reference
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
checkSizeLimit(0.5): GRID_SIZE_LIMIT should be at least 1
checkSizeLimit(1e10): GRID_SIZE_LIMIT cannot be larger than 4294967295
checkSizeLimit(1e8): ''
raised 0 limit 1000
inside: changed 0 lower 0 upper 0 npoints 101 toolarge 0 nonfinite none
below: changed 1 lower 26 upper 0 npoints 127 toolarge 0 nonfinite none
above: changed 1 lower 0 upper 40 npoints 141 toolarge 0 nonfinite none
fixed: changed 1 lower 0 3 upper 0 0 npoints 280 toolarge 0 nonfinite none
fallback: changed 1 lower 1 0 upper 0 0 npoints 930 toolarge 0 nonfinite none
toolarge: changed 1 lower 10 0 upper 0 0 npoints 1200 toolarge 1 nonfinite none
place: changed 1 lower -3 upper 8 npoints 7 toolarge 0 nonfinite none
place-shift: changed 1 lower 7 upper -4 npoints 5 toolarge 0 nonfinite none
nonfinite: changed 0 lower 0 0 upper 0 0 npoints 0 toolarge 0 nonfinite 1
far: changed 0 lower 0 upper 0 npoints 0 toolarge 0 nonfinite 0
lattice: min -1 -pi max 1 pi points 21 20 total 420
lattice coordinate of 0.25: 12.5
extended: min -1.5 -pi max 0.8 pi points 24 20 total 480
same lattice 1 shift 5 0 back -5 0
same lattice 1 shift 2 0
a new lattice with the same numbers: same lattice 1
a shifted lattice: same lattice 0
1 change: 1 addition & 0 deletions regtest/basic/rt-make-gridlattice/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../scripts/test.make
1 change: 1 addition & 0 deletions regtest/basic/rt-make-gridlattice/config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type=make
159 changes: 159 additions & 0 deletions regtest/basic/rt-make-gridlattice/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#include "plumed/tools/GridLattice.h"
#include "plumed/tools/Grid.h"
#include "plumed/tools/GridCoordinatesObject.h"
#include "testUtils.h"
#include <cmath>
#include <cstdio>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

using namespace PLMD;

template <typename T>
std::string str(const T* v, std::size_t n) {
std::string s;
for(std::size_t i=0; i<n; ++i) {
s+=(i>0?" ":"")+std::to_string(v[i]);
}
return s;
}

std::string ind(const unsigned* v, std::size_t n) {
std::string s;
for(std::size_t i=0; i<n; ++i) {
s+=(i>0?" ":"")+(v[i]==std::numeric_limits<unsigned>::max() ? std::string("out") : std::to_string(v[i]));
}
return s;
}

std::string num(double x) {
char buf[64];
std::snprintf(buf,sizeof(buf),"%.17g",x);
return buf;
}

// fewer digits, for results that depend on the order of the floating point operations (FMA, libm)
std::string approx(double x) {
if(std::fabs(x)<1e-10) {
x=0.0;
}
char buf[64];
std::snprintf(buf,sizeof(buf),"%.10f",x);
return buf;
}

int main() {
tee out("output");
// x: non-periodic, from -1 to 1 with 4 bins (5 points); y: periodic, from -pi to pi with 6 bins (6 points)
GridLattice g({false,true});
g.setFromBounds({"-1","-pi"},{"1","pi"},{4,6});
g.setArgNames({"x","y"});
out << "points along " << str(g.getNumberOfPointsAlong().data(),2) << " total " << g.getNumberOfPoints() << "\n";
out << "min " << g.getMin()[0] << " " << g.getMin()[1] << " max " << g.getMax()[0] << " " << g.getMax()[1] << "\n";

// indices of points (periodic: brought back into the grid; non-periodic: "out" outside)
const std::vector<std::vector<double>> points={{0.1,0.1},{-1.0,-3.14159265358979},{1.0,3.14159265358979},{1.2,3.2},
{-1.5,-4.0},{0.9,10.0},{0.0,-10.0}
};
for(const auto& p : points) {
unsigned idx[2];
g.getIndices(p.data(),idx,2);
out << "point " << p[0] << " " << p[1] << ": indices " << ind(idx,2) << " isInside " << g.isInside(p.data())
<< " inRange " << g.inRange(p.data()) << "\n";
}
// coordinates of the points
for(GridLattice::index_t i=0; i<g.getNumberOfPoints(); i+=7) {
std::vector<double> x(2);
g.getPoint(i,x);
out << "point " << i << " indices " << str(g.getIndices(i).data(),2) << " at " << num(x[0]) << " " << num(x[1]) << "\n";
}
// neighbours (size_t and unsigned outputs, signed centre also outside the grid)
const std::vector<std::vector<long long>> centers={{2,3},{0,0},{4,5},{-2,1},{6,-7},{-5,0}};
const unsigned neigh[2]= {1,1};
for(const auto& c : centers) {
std::vector<GridLattice::index_t> n1(g.getMaxNeighbors(neigh));
std::vector<unsigned> n2(g.getMaxNeighbors(neigh));
const std::size_t k1=g.getNeighbors(c.data(),neigh,n1.data());
const std::size_t k2=g.getNeighbors(c.data(),neigh,n2.data());
out << "neighbours of " << c[0] << " " << c[1] << ": " << (k1>0 ? str(n1.data(),k1) : std::string("none")) << (k1==k2 && std::equal(n1.begin(),n1.begin()+k1,n2.begin()) ? "" : " MISMATCH") << "\n";
}
// a neighbourhood larger than the periodic direction: each point once
const unsigned bigneigh[2]= {0,5};
const long long c0[2]= {1,2};
std::vector<unsigned> nb(g.getMaxNeighbors(bigneigh));
out << "large neighbourhood: " << str(nb.data(),g.getNeighbors(c0,bigneigh,nb.data())) << "\n";
// neighbours of a point outside the grid
const std::vector<GridLattice::index_t> outside=g.getNeighbors(std::vector<double>({-1.3,0.0}),std::vector<unsigned>({1,0}));
out << "neighbours of (-1.3,0): " << str(outside.data(),outside.size()) << "\n";
// spline neighbours at the end of the non-periodic direction and across the periodic boundary
const unsigned s1[2]= {4,5};
GridLattice::index_t sn[4];
const unsigned ns=g.getSplineNeighbors(s1,2,sn,4);
out << "spline neighbours of 4 5: " << str(sn,ns) << "\n";
try {
const unsigned bad[2]= {5,0};
g.getIndex(bad,2);
} catch(const PLMD::Exception& e) {
out << "getIndex outside: error\n";
}

// GridBase and GridCoordinatesObject give the same geometry (same lattice code)
Grid grid("f",{"x","y"},{"-1","-pi"},{"1","pi"},{4,6},false,false,{false,true},{"0.","-pi"},{"0.","pi"});
GridCoordinatesObject gco;
gco.setup("flat",{false,true},0,0.0);
std::vector<double> spacing;
gco.setBounds({"-1","-pi"},{"1","pi"},{4,6},spacing);
bool same=(grid.getNumberOfPoints()==gco.getNumberOfPoints());
for(std::size_t i=0; i<gco.getNumberOfPoints(); ++i) {
std::vector<double> a(2), b(2);
grid.getPoint(i,a);
gco.getGridPointCoordinates(i,b);
same=same && a==b;
std::size_t nn;
std::vector<std::size_t> gn;
gco.getNeighbors(b,{1,2},nn,gn);
const std::vector<GridLattice::index_t> bn=grid.getNeighbors(a,{1,2});
same=same && nn==bn.size() && std::equal(bn.begin(),bn.end(),gn.begin());
}
out << "GridBase and GridCoordinatesObject: " << (same ? "same" : "DIFFERENT") << "\n";

// spline interpolation of f=cos(y)+x^2 (y periodic), from its values and derivatives at the points
auto f=[&](GridLattice::index_t index,double* d) {
std::vector<double> p(2);
g.getPoint(index,p);
d[0]=2*p[0];
d[1]=-std::sin(p[1]);
return std::cos(p[1])+p[0]*p[0];
};
for(const auto& p : std::vector<std::vector<double>>({{0.1,0.3},{-0.77,2.9},{0.99,-3.1},{0.3,3.14159265358979},{0.3,-3.14159265358979},{0.3,6.0}})) {
double d[2];
const double v=g.splineInterpolation(p.data(),d,f);
out << "spline at " << p[0] << " " << p[1] << ": " << approx(v) << " der " << approx(d[0]) << " " << approx(d[1])
<< " exact " << approx(std::cos(p[1])+p[0]*p[0]) << "\n";
}
// GridBase gives the same spline from its own values and derivatives
Grid sgrid("f",{"x","y"},{"-1","-pi"},{"1","pi"},{4,6},true,true,{false,true},{"0.","-pi"},{"0.","pi"});
for(GridLattice::index_t i=0; i<sgrid.getNumberOfPoints(); ++i) {
std::vector<double> d(2);
const double v=f(i,d.data());
sgrid.setValueAndDerivatives(i,v,d);
}
bool samespline=true;
for(const auto& p : std::vector<std::vector<double>>({{0.1,0.3},{-0.77,2.9},{0.99,-3.1}})) {
std::vector<double> d1(2), d2(2);
const double v1=sgrid.getValueAndDerivatives(p,d1);
const double v2=g.splineInterpolation(p.data(),d2.data(),f);
samespline=samespline && v1==v2 && d1==d2;
}
out << "GridBase spline: " << (samespline ? "same" : "DIFFERENT") << "\n";
try {
const double outside[2]= {1.6,0.0};
double d[2];
g.splineInterpolation(outside,d,f);
} catch(const PLMD::Exception& e) {
out << "spline outside: error\n";
}
return 0;
}
33 changes: 33 additions & 0 deletions regtest/basic/rt-make-gridlattice/output.reference
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
points along 5 6 total 30
min -1 -pi max 1 pi
point 0.1 0.1: indices 2 3 isInside 1 inRange 1
point -1 -3.14159: indices 0 0 isInside 1 inRange 1
point 1 3.14159: indices 4 5 isInside 1 inRange 1
point 1.2 3.2: indices 4 0 isInside 1 inRange 0
point -1.5 -4: indices out 5 isInside 0 inRange 0
point 0.9 10: indices 3 0 isInside 1 inRange 1
point 0 -10: indices 2 5 isInside 1 inRange 1
point 0 indices 0 0 at -1 -3.1415926535897931
point 7 indices 2 1 at 0 -2.0943951023931957
point 14 indices 4 2 at 1 -1.0471975511965979
point 21 indices 1 4 at -0.5 1.0471975511965974
point 28 indices 3 5 at 0.5 2.0943951023931948
neighbours of 2 3: 11 12 13 16 17 18 21 22 23
neighbours of 0 0: 25 26 0 1 5 6
neighbours of 4 5: 23 24 28 29 3 4
neighbours of -2 1: none
neighbours of 6 -7: none
neighbours of -5 0: none
large neighbourhood: 16 21 26 1 6 11
neighbours of (-1.3,0): 15
spline neighbours of 4 5: 29 4
getIndex outside: error
GridBase and GridCoordinatesObject: same
spline at 0.1 0.3: 0.9623801304 der 0.1801734060 -0.3086531476 exact 0.9653364891
spline at -0.77 2.9: -0.2422157866 der -1.8830662211 -0.2405498055 exact -0.3780581651
spline at 0.99 -3.1: 0.0002031548 der 0.1408128962 0.0429079573 exact -0.0190351503
spline at 0.3 3.14159: -0.9100000000 der 0.6000000000 0.0000000000 exact -0.9100000000
spline at 0.3 -3.14159: -0.9100000000 der 0.6000000000 0.0000000000 exact -0.9100000000
spline at 0.3 6: 1.0438238031 der 0.5922583215 0.3135388521 exact 1.0501702867
GridBase spline: same
spline outside: error
Loading
Loading