LCOV - code coverage report
Current view: top level - gridtools - GridCoordinatesObject.h (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 11 13 84.6 %
Date: 2025-12-04 11:19:34 Functions: 2 2 100.0 %

          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 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             : /// Get the neighbors for a set of indices of a point
     111             :   void getNeighbors( const std::vector<unsigned>& indices, const std::vector<unsigned>& nneigh,
     112             :                      unsigned& num_neighbors, std::vector<unsigned>& neighbors ) const ;
     113             : /// Get the points neighboring a particular spline point
     114             :   void getSplineNeighbors( const unsigned& mybox, unsigned& nneighbors, std::vector<unsigned>& mysneigh ) const ;
     115             : /// Get the spacing between grid points
     116             :   const std::vector<double>& getGridSpacing() const ;
     117             : /// Get the stride (the distance between the grid points of an index)
     118             :   const std::vector<unsigned>& getStride() const ;
     119             : /// Get the type of the grid
     120             :   std::string getGridType() const ;
     121             : };
     122             : 
     123             : inline
     124             : unsigned GridCoordinatesObject::getNumberOfPoints() const {
     125      557936 :   return npoints;
     126             : }
     127             : 
     128             : inline
     129     4535381 : const std::vector<double>& GridCoordinatesObject::getGridSpacing() const {
     130     4535381 :   if( gtype==flat ) {
     131     4535381 :     return dx;
     132             :   }
     133           0 :   plumed_merror("dont understand what spacing means for spherical grids");
     134             :   return dx;
     135             : }
     136             : 
     137             : inline
     138             : double GridCoordinatesObject::getCellVolume() const {
     139             :   if( gtype==flat ) {
     140             :     double myvol=1.0;
     141             :     for(unsigned i=0; i<dimension; ++i) {
     142             :       myvol *= dx[i];
     143             :     }
     144             :     return myvol;
     145             :   } else {
     146             :     return 4*pi / static_cast<double>( getNumberOfPoints() );
     147             :   }
     148             : }
     149             : 
     150             : inline
     151             : unsigned GridCoordinatesObject::getDimension() const {
     152     1260107 :   return dimension;
     153             : }
     154             : 
     155             : inline
     156             : bool GridCoordinatesObject::isPeriodic( const unsigned& i ) const {
     157             :   plumed_dbg_assert( gtype==flat );
     158       16844 :   return pbc[i];
     159             : }
     160             : 
     161             : inline
     162             : const std::vector<unsigned>& GridCoordinatesObject::getStride() const {
     163             :   plumed_dbg_assert( gtype==flat );
     164             :   return stride;
     165             : }
     166             : 
     167             : inline
     168      244098 : std::string GridCoordinatesObject::getGridType() const {
     169      244098 :   if( gtype==flat ) {
     170      243442 :     return "flat";
     171         656 :   } else if( gtype==fibonacci ) {
     172         656 :     return "fibonacci";
     173             :   }
     174           0 :   return "";
     175             : }
     176             : 
     177             : }
     178             : }
     179             : #endif

Generated by: LCOV version 1.16