LCOV - code coverage report
Current view: top level - gridtools - GridToXYZ.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 41 46 89.1 %
Date: 2020-11-18 11:20:57 Functions: 9 11 81.8 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2016-2019 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             : #include "GridPrintingBase.h"
      23             : #include "core/ActionRegister.h"
      24             : #include "core/PlumedMain.h"
      25             : #include "tools/OFile.h"
      26             : #include "core/Atoms.h"
      27             : 
      28             : namespace PLMD {
      29             : namespace gridtools {
      30             : 
      31             : //+PLUMEDOC GRIDANALYSIS GRID_TO_XYZ
      32             : /*
      33             : Output the function on the grid to an xyz file
      34             : 
      35             : \par Examples
      36             : 
      37             : */
      38             : //+ENDPLUMEDOC
      39             : 
      40           2 : class GridToXYZ : public GridPrintingBase {
      41             : private:
      42             :   double lenunit;
      43             :   unsigned mycomp;
      44             : public:
      45             :   static void registerKeywords( Keywords& keys );
      46             :   explicit GridToXYZ(const ActionOptions&ao);
      47             :   void printGrid( OFile& ofile ) const ;
      48             : };
      49             : 
      50        6454 : PLUMED_REGISTER_ACTION(GridToXYZ,"GRID_TO_XYZ")
      51             : 
      52           3 : void GridToXYZ::registerKeywords( Keywords& keys ) {
      53           3 :   GridPrintingBase::registerKeywords( keys );
      54          12 :   keys.add("optional","COMPONENT","if your input is a vector field use this to specifiy the component of the input vector field for which you wish to output");
      55          15 :   keys.add("compulsory","UNITS","PLUMED","the units in which to print out the coordinates. PLUMED means internal PLUMED units");
      56          12 :   keys.add("optional", "PRECISION","The number of digits in trajectory file");
      57           6 :   keys.remove("FMT");
      58           3 : }
      59             : 
      60           2 : GridToXYZ::GridToXYZ(const ActionOptions&ao):
      61             :   Action(ao),
      62           2 :   GridPrintingBase(ao)
      63             : {
      64           4 :   if( ingrid->getDimension()!=3 ) error("cannot print grid xyz file if grid does not contain three dimensional data");
      65           4 :   fmt = " " + fmt;
      66             : 
      67           2 :   if( ingrid->getNumberOfComponents()==1 ) {
      68           2 :     mycomp=0;
      69             :   } else {
      70           0 :     int tcomp=-1; parse("COMPONENT",tcomp);
      71           0 :     if( tcomp<0 ) error("component of vector field was not specified - use COMPONENT keyword");
      72           0 :     mycomp=tcomp*(1+ingrid->getDimension()); if( ingrid->noDerivatives() ) mycomp=tcomp;
      73           0 :     log.printf("  using %dth component of grid \n",tcomp );
      74             :   }
      75             :   fmt="%f";
      76           4 :   std::string precision; parse("PRECISION",precision);
      77           2 :   if(precision.length()>0) {
      78           2 :     int p; Tools::convert(precision,p);
      79           2 :     log<<"  with precision "<<p<<"\n";
      80             :     std::string a,b;
      81           2 :     Tools::convert(p+5,a);
      82           2 :     Tools::convert(p,b);
      83          10 :     fmt="%"+a+"."+b+"f";
      84             :   }
      85           4 :   std::string unitname; parse("UNITS",unitname);
      86           2 :   if(unitname!="PLUMED") {
      87           4 :     Units myunit; myunit.setLength(unitname);
      88           4 :     lenunit=plumed.getAtoms().getUnits().getLength()/myunit.getLength();
      89             :   }
      90           0 :   else lenunit=1.0;
      91           2 :   checkRead();
      92           2 : }
      93             : 
      94           2 : void GridToXYZ::printGrid( OFile& ofile ) const {
      95           2 :   std::vector<double> point( 3 );
      96           4 :   ofile.printf("%u\n",ingrid->getNumberOfPoints());
      97           2 :   ofile.printf("Grid converted to xyz file \n");
      98         492 :   for(unsigned i=0; i<ingrid->getNumberOfPoints(); ++i) {
      99         244 :     ingrid->getGridPointCoordinates( i, point );
     100         244 :     ofile.printf("X");
     101             :     double val;
     102         488 :     if( ingrid->getType()=="flat" ) val=1.0;
     103         244 :     else val=ingrid->getGridElement( i, 0 );
     104        3172 :     for(unsigned j=0; j<3; ++j) { ofile.printf( (" " + fmt).c_str(), val*lenunit*point[j] ); }
     105         244 :     ofile.printf("\n");
     106             :   }
     107           2 : }
     108             : 
     109             : }
     110        4839 : }

Generated by: LCOV version 1.13