LCOV - code coverage report
Current view: top level - contour - DumpContour.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 42 44 95.5 %
Date: 2025-12-04 11:19:34 Functions: 7 8 87.5 %

          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 "core/ActionPilot.h"
      23             : #include "core/ActionRegister.h"
      24             : #include "tools/OFile.h"
      25             : #include "core/PlumedMain.h"
      26             : #include "core/ActionSet.h"
      27             : #include "FindContour.h"
      28             : 
      29             : namespace PLMD {
      30             : namespace contour {
      31             : 
      32             : //+PLUMEDOC GRIDANALYSIS DUMPCONTOUR
      33             : /*
      34             : Print the contour
      35             : 
      36             : This is a special action that is used to print the output from a [FIND_CONTOUR](FIND_CONTOUR.md) action.
      37             : The following example illustrates how this method is used to output a xyz file that contains the set of
      38             : points that [FIND_CONTOUR](FIND_CONTOUR.md) found on the isocontour of interest.
      39             : 
      40             : ```plumed
      41             : UNITS NATURAL
      42             : 
      43             : # This calculates the value of a set of symmetry functions for the atoms of interest
      44             : fcc: FCCUBIC ...
      45             :   SPECIES=1-96000 SWITCH={CUBIC D_0=1.2 D_MAX=1.5}
      46             :   ALPHA=27 PHI=0.0 THETA=-1.5708 PSI=-2.35619
      47             : ...
      48             : 
      49             : # Transform the symmetry functions with a switching function
      50             : tfcc: LESS_THAN ARG=fcc SWITCH={SMAP R_0=0.5 A=8 B=8}
      51             : 
      52             : # Now compute the center of the solid like region
      53             : center: CENTER ATOMS=1-96000 WEIGHTS=tfcc
      54             : 
      55             : # This determines the positions of the atoms of interest relative to the center of the solid region
      56             : dens_dist: DISTANCES ORIGIN=center ATOMS=1-96000 COMPONENTS
      57             : # This computes the numerator in the expression above for the phase field
      58             : dens_numer: KDE ...
      59             :   VOLUMES=tfcc ARG=dens_dist.x,dens_dist.y,dens_dist.z
      60             :   GRID_BIN=80,80,80 BANDWIDTH=1.0,1.0,1.0
      61             : ...
      62             : # This computes the denominator
      63             : dens_denom: KDE ...
      64             :   ARG=dens_dist.x,dens_dist.y,dens_dist.z
      65             :   GRID_BIN=80,80,80 BANDWIDTH=1.0,1.0,1.0
      66             : ...
      67             : # This computes the final phase field
      68             : dens: CUSTOM ARG=dens_numer,dens_denom FUNC=x/y PERIODIC=NO
      69             : 
      70             : # Find the isocontour
      71             : cont: FIND_CONTOUR ARG=dens CONTOUR=0.5
      72             : # Use the special method for outputting the contour to a file
      73             : DUMPCONTOUR ARG=cont FILE=surface.xyz STRIDE=1 FMT=%8.4f
      74             : ```
      75             : 
      76             : */
      77             : //+ENDPLUMEDOC
      78             : 
      79             : class DumpContour :
      80             :   public ActionPilot {
      81             : private:
      82             :   FindContour* fc;
      83             :   std::string fmt, filename;
      84             : public:
      85             :   static void registerKeywords( Keywords& keys );
      86             :   explicit DumpContour(const ActionOptions&ao);
      87           2 :   ~DumpContour() {}
      88           2 :   void calculate() override {}
      89           2 :   void apply() override {}
      90             :   void update() override ;
      91             : };
      92             : 
      93             : PLUMED_REGISTER_ACTION(DumpContour,"DUMPCONTOUR")
      94             : 
      95           3 : void DumpContour::registerKeywords( Keywords& keys ) {
      96           3 :   Action::registerKeywords( keys );
      97           3 :   ActionPilot::registerKeywords( keys );
      98           6 :   keys.addInputKeyword("compulsory","ARG","vector","the labels of the FIND_CONTOUR action that you would like to output");
      99           3 :   keys.add("compulsory","STRIDE","1","the frequency with which the grid should be output to the file.");
     100           3 :   keys.add("compulsory","FILE","density","the file on which to write the grid.");
     101           3 :   keys.add("compulsory","FMT","%f","the format that should be used to output real numbers");
     102           3 : }
     103             : 
     104           1 : DumpContour::DumpContour(const ActionOptions&ao):
     105             :   Action(ao),
     106           1 :   ActionPilot(ao) {
     107             : 
     108             :   std::string argname;
     109           1 :   parse("ARG",argname);
     110           1 :   fc=plumed.getActionSet().selectWithLabel<FindContour*>( argname );
     111           1 :   if( !fc ) {
     112           0 :     error("cannot find FIND_CONTOUR action with label " + argname );
     113             :   }
     114           1 :   addDependency(fc);
     115             : 
     116           2 :   parse("FILE",filename);
     117           1 :   if(filename.length()==0) {
     118           0 :     error("name out output file was not specified");
     119             :   }
     120             : 
     121           1 :   log.printf("  outputting contour with label %s to file named %s",argname.c_str(), filename.c_str() );
     122           1 :   parse("FMT",fmt);
     123           1 :   log.printf(" with format %s \n", fmt.c_str() );
     124           2 :   fmt = " " + fmt;
     125           1 : }
     126             : 
     127           2 : void DumpContour::update() {
     128           2 :   OFile ofile;
     129           2 :   ofile.link(*this);
     130           2 :   ofile.setBackupString("analysis");
     131           2 :   ofile.open( filename );
     132             : 
     133           2 :   unsigned maxp = fc->active_cells.size(), ncomp = fc->getNumberOfComponents();
     134             :   unsigned ntasks = 0;
     135       32930 :   for(unsigned i=0; i<maxp; ++i) {
     136       32928 :     ntasks += fc->active_cells[i];
     137             :   }
     138             : 
     139           2 :   ofile.printf("%d\n", ntasks );
     140           2 :   ofile.printf("Points found on isocontour\n");
     141       32930 :   for(unsigned i=0; i<maxp; ++i) {
     142       32928 :     if( fc->active_cells[i]==0 ) {
     143       31820 :       continue ;
     144             :     }
     145             :     const char* defname="X";
     146             :     const char* name=defname;
     147        1108 :     ofile.printf("%s", name);
     148        4432 :     for(unsigned j=0; j<ncomp; ++j ) {
     149        6648 :       ofile.printf((" " + fmt).c_str(), (fc->copyOutput(j))->get(i)  );
     150             :     }
     151        1108 :     ofile.printf("\n");
     152             :   }
     153           2 : }
     154             : 
     155             : 
     156             : }
     157             : }

Generated by: LCOV version 1.16