LCOV - code coverage report
Current view: top level - gridtools - GridToXYZ.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 42 52 80.8 %
Date: 2026-03-30 13:16:06 Functions: 6 7 85.7 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2016-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             : #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             : 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 override;
      48             : };
      49             : 
      50       13789 : PLUMED_REGISTER_ACTION(GridToXYZ,"GRID_TO_XYZ")
      51             : 
      52           6 : void GridToXYZ::registerKeywords( Keywords& keys ) {
      53           6 :   GridPrintingBase::registerKeywords( keys );
      54          12 :   keys.add("optional","COMPONENT","if your input is a vector field use this to specify the component of the input vector field for which you wish to output");
      55          12 :   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           6 : }
      59             : 
      60           2 : GridToXYZ::GridToXYZ(const ActionOptions&ao):
      61             :   Action(ao),
      62           2 :   GridPrintingBase(ao) {
      63           2 :   if( ingrid->getDimension()!=3 ) {
      64           0 :     error("cannot print grid xyz file if grid does not contain three dimensional data");
      65             :   }
      66           2 :   fmt = " " + fmt;
      67             : 
      68           2 :   if( ingrid->getNumberOfComponents()==1 ) {
      69           2 :     mycomp=0;
      70             :   } else {
      71           0 :     int tcomp=-1;
      72           0 :     parse("COMPONENT",tcomp);
      73           0 :     if( tcomp<0 ) {
      74           0 :       error("component of vector field was not specified - use COMPONENT keyword");
      75             :     }
      76           0 :     mycomp=tcomp*(1+ingrid->getDimension());
      77           0 :     if( ingrid->noDerivatives() ) {
      78           0 :       mycomp=tcomp;
      79             :     }
      80           0 :     log.printf("  using %dth component of grid \n",tcomp );
      81             :   }
      82             :   fmt="%f";
      83             :   std::string precision;
      84           4 :   parse("PRECISION",precision);
      85           2 :   if(precision.length()>0) {
      86             :     int p;
      87           2 :     Tools::convert(precision,p);
      88           2 :     log<<"  with precision "<<p<<"\n";
      89             :     std::string a,b;
      90           2 :     Tools::convert(p+5,a);
      91           2 :     Tools::convert(p,b);
      92           4 :     fmt="%"+a+"."+b+"f";
      93             :   }
      94             :   std::string unitname;
      95           4 :   parse("UNITS",unitname);
      96           2 :   if(unitname!="PLUMED") {
      97           2 :     Units myunit;
      98           2 :     myunit.setLength(unitname);
      99           2 :     lenunit=plumed.getAtoms().getUnits().getLength()/myunit.getLength();
     100           2 :   } else {
     101           0 :     lenunit=1.0;
     102             :   }
     103           2 :   checkRead();
     104           2 : }
     105             : 
     106           2 : void GridToXYZ::printGrid( OFile& ofile ) const {
     107           2 :   std::vector<double> point( 3 );
     108           2 :   ofile.printf("%u\n",ingrid->getNumberOfPoints());
     109           2 :   ofile.printf("Grid converted to xyz file \n");
     110         246 :   for(unsigned i=0; i<ingrid->getNumberOfPoints(); ++i) {
     111         244 :     ingrid->getGridPointCoordinates( i, point );
     112         244 :     ofile.printf("X");
     113             :     double val;
     114         488 :     if( ingrid->getType()=="flat" ) {
     115             :       val=1.0;
     116             :     } else {
     117         244 :       val=ingrid->getGridElement( i, 0 );
     118             :     }
     119         976 :     for(unsigned j=0; j<3; ++j) {
     120        1464 :       ofile.printf( (" " + fmt).c_str(), val*lenunit*point[j] );
     121             :     }
     122         244 :     ofile.printf("\n");
     123             :   }
     124           2 : }
     125             : 
     126             : }
     127             : }

Generated by: LCOV version 1.16