Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2015-2023 The plumed team 3 : (see the PEOPLE file at the root of the distribution for a list of names) 4 : 5 : See http://www.plumed.org for more information. 6 : 7 : This file is part of plumed, version 2. 8 : 9 : plumed is free software: you can redistribute it and/or modify 10 : it under the terms of the GNU Lesser General Public License as published by 11 : the Free Software Foundation, either version 3 of the License, or 12 : (at your option) any later version. 13 : 14 : plumed is distributed in the hope that it will be useful, 15 : but WITHOUT ANY WARRANTY; without even the implied warranty of 16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 : GNU Lesser General Public License for more details. 18 : 19 : You should have received a copy of the GNU Lesser General Public License 20 : along with plumed. If not, see <http://www.gnu.org/licenses/>. 21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ 22 : #ifndef __PLUMED_gridtools_GridCoordinatesObject_h 23 : #define __PLUMED_gridtools_GridCoordinatesObject_h 24 : 25 : #include <string> 26 : #include <cstring> 27 : #include <vector> 28 : #include "tools/Exception.h" 29 : #include "tools/Tools.h" 30 : #include "tools/View.h" 31 : 32 : namespace PLMD { 33 : namespace gridtools { 34 : 35 : class GridCoordinatesObject { 36 : private: 37 : /// Have the bounds been setup on the grid 38 : bool bounds_set; 39 : /// The way that grid points are constructed 40 : enum {flat,fibonacci} gtype; 41 : /// The number of points in the grid 42 : unsigned npoints; 43 : /// Stuff for fibonacci grids 44 : double root5, golden, igolden, log_golden2; 45 : /// Fib increment here is equal to 2*pi*(INVERSE GOLDEN RATIO) 46 : double fib_offset, fib_increment, fib_shift; 47 : std::vector<std::vector<unsigned> > fib_nlist; 48 : /// The minimum and maximum of the grid stored as doubles 49 : std::vector<double> min, max; 50 : /// The numerical distance between adjacent grid points 51 : std::vector<unsigned> stride; 52 : /// The number of bins in each grid direction 53 : std::vector<unsigned> nbin; 54 : /// Is this direction periodic 55 : std::vector<bool> pbc; 56 : /// The minimum and maximum in the grid stored as strings 57 : std::vector<std::string> str_min, str_max; 58 : /// The spacing between grid points 59 : std::vector<double> dx; 60 : /// The dimensionality of the grid 61 : unsigned dimension; 62 : /// Get the index of the closest point on the fibonacci sphere 63 : unsigned getFibonacciIndex( View<const double> p ) const ; 64 : /// Get the flat grid coordinates 65 : void getFlatGridCoordinates( const unsigned& ipoint, std::vector<unsigned>& tindices, std::vector<double>& x ) const ; 66 : /// Get the coordinates on the Fibonacci grid 67 : void getFibonacciCoordinates( const unsigned& ipoint, std::vector<double>& x ) const ; 68 : public: 69 : /// Setup the grid 70 : void setup( const std::string& geom, const std::vector<bool>& ipbc, const unsigned& np, const double& fib_cutoff ); 71 : /// Set the minimum and maximum of the grid 72 : void setBounds( const std::vector<std::string>& smin, const std::vector<std::string>& smax, const std::vector<std::size_t>& nbins, std::vector<double>& spacing ); 73 : /// Convert an index into indices 74 : void convertIndexToIndices( const unsigned& index, const std::vector<unsigned>& nnbin, std::vector<unsigned>& indices ) const ; 75 : /// Check if a point is within the grid boundaries 76 : bool inbounds( const std::vector<double>& point ) const ; 77 : bool inbounds( View<const double> point ) const ; 78 : /// Convert a point in space the correspoinding grid point 79 : unsigned getIndex( const std::vector<double>& p ) const ; 80 : unsigned getIndex( View<const double> point ) const ; 81 : /// Flatten the grid and get the grid index for a point 82 : unsigned getIndex( const std::vector<unsigned>& indices ) const ; 83 : /// Get the indices fof a point 84 : void getIndices( const unsigned& index, std::vector<unsigned>& indices ) const ; 85 : /// Get the indices of a particular point 86 : void getIndices( const std::vector<double>& point, std::vector<unsigned>& indices ) const ; 87 : void getIndices( View<const double> point, std::vector<unsigned>& indices ) const ; 88 : /// Get the number of points in the grid 89 : unsigned getNumberOfPoints() const; 90 : /// Get the coordinates for a point in the grid 91 : void getGridPointCoordinates( const unsigned&, std::vector<double>& ) const ; 92 : void getGridPointCoordinates( const unsigned&, std::vector<unsigned>&, std::vector<double>& ) const ; 93 : /// Create a coordinate that has this value of the grid 94 : void putCoordinateAtValue( const unsigned&, const double&, std::vector<double>& ) const ; 95 : /// Get the dimensionality of the function 96 : unsigned getDimension() const ; 97 : /// Is the grid periodic in the ith direction 98 : bool isPeriodic( const unsigned& i ) const ; 99 : /// Get the number of grid points for each dimension 100 : std::vector<std::size_t> getNbin( const bool& shape ) const ; 101 : /// Get the vector containing the minimum value of the grid in each dimension 102 : std::vector<std::string> getMin() const ; 103 : /// Get the vector containing the maximum value of the grid in each dimension 104 : std::vector<std::string> getMax() const ; 105 : /// Return the volume of one of the grid cells 106 : double getCellVolume() const ; 107 : /// Get the set of points neighouring a particular location in space 108 : void getNeighbors( const std::vector<double>& pp, const std::vector<unsigned>& nneigh, 109 : unsigned& num_neighbours, std::vector<unsigned>& neighbors ) const ; 110 : void getNeighbors( const View<const double> pp, const std::vector<unsigned>& nneigh, 111 : unsigned& num_neighbours, std::vector<unsigned>& neighbors ) const ; 112 : /// Get the neighbors for a set of indices of a point 113 : void getNeighbors( const std::vector<unsigned>& indices, const std::vector<unsigned>& nneigh, 114 : unsigned& num_neighbors, std::vector<unsigned>& neighbors ) const ; 115 : /// Get the points neighboring a particular spline point 116 : void getSplineNeighbors( const unsigned& mybox, unsigned& nneighbors, std::vector<unsigned>& mysneigh ) const ; 117 : /// Get the spacing between grid points 118 : const std::vector<double>& getGridSpacing() const ; 119 : /// Get the stride (the distance between the grid points of an index) 120 : const std::vector<unsigned>& getStride() const ; 121 : /// Get the type of the grid 122 : std::string getGridType() const ; 123 : }; 124 : 125 : inline 126 : unsigned GridCoordinatesObject::getNumberOfPoints() const { 127 558361 : return npoints; 128 : } 129 : 130 : inline 131 4536954 : const std::vector<double>& GridCoordinatesObject::getGridSpacing() const { 132 4536954 : if( gtype==flat ) { 133 4536954 : return dx; 134 : } 135 0 : plumed_merror("dont understand what spacing means for spherical grids"); 136 : return dx; 137 : } 138 : 139 : inline 140 : double GridCoordinatesObject::getCellVolume() const { 141 : if( gtype==flat ) { 142 : double myvol=1.0; 143 : for(unsigned i=0; i<dimension; ++i) { 144 : myvol *= dx[i]; 145 : } 146 : return myvol; 147 : } else { 148 : return 4*pi / static_cast<double>( getNumberOfPoints() ); 149 : } 150 : } 151 : 152 : inline 153 : unsigned GridCoordinatesObject::getDimension() const { 154 1240334 : return dimension; 155 : } 156 : 157 : inline 158 : bool GridCoordinatesObject::isPeriodic( const unsigned& i ) const { 159 : plumed_dbg_assert( gtype==flat ); 160 16844 : return pbc[i]; 161 : } 162 : 163 : inline 164 : const std::vector<unsigned>& GridCoordinatesObject::getStride() const { 165 : plumed_dbg_assert( gtype==flat ); 166 : return stride; 167 : } 168 : 169 : inline 170 244989 : std::string GridCoordinatesObject::getGridType() const { 171 244989 : if( gtype==flat ) { 172 244040 : return "flat"; 173 949 : } else if( gtype==fibonacci ) { 174 949 : return "fibonacci"; 175 : } 176 0 : return ""; 177 : } 178 : 179 : } 180 : } 181 : #endif