LCOV - code coverage report
Current view: top level - adjmat - DumpGraph.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 35 35 100.0 %
Date: 2024-10-11 08:09:47 Functions: 8 9 88.9 %

          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 "AdjacencyMatrixVessel.h"
      25             : #include "AdjacencyMatrixBase.h"
      26             : #include "core/PlumedMain.h"
      27             : #include "core/ActionSet.h"
      28             : #include "tools/OFile.h"
      29             : 
      30             : namespace PLMD {
      31             : namespace adjmat {
      32             : 
      33             : //+PLUMEDOC CONCOMP DUMPGRAPH
      34             : /*
      35             : Write out the connectivity of the nodes in the graph in dot format.
      36             : 
      37             : \par Examples
      38             : 
      39             : */
      40             : //+ENDPLUMEDOC
      41             : 
      42             : 
      43             : class DumpGraph : public ActionPilot {
      44             : private:
      45             : ///
      46             :   size_t maxconnections;
      47             : /// The vessel that contains the graph
      48             :   AdjacencyMatrixVessel* mymatrix;
      49             : /// The name of the file on which we are outputting the graph
      50             :   std::string filename;
      51             : public:
      52             : /// Create manual
      53             :   static void registerKeywords( Keywords& keys );
      54             : /// Constructor
      55             :   explicit DumpGraph( const ActionOptions& );
      56             : /// Calculate and apply do nothing
      57           4 :   void calculate() override {};
      58           4 :   void apply() override {};
      59             : /// Update will do the output
      60             :   void update() override;
      61             : };
      62             : 
      63       10427 : PLUMED_REGISTER_ACTION(DumpGraph,"DUMPGRAPH")
      64             : 
      65           5 : void DumpGraph::registerKeywords( Keywords& keys ) {
      66           5 :   Action::registerKeywords( keys ); ActionPilot::registerKeywords( keys );
      67          10 :   keys.add("compulsory","MATRIX","the action that calculates the adjacency matrix vessel we would like to analyze");
      68          10 :   keys.add("compulsory","STRIDE","1","the frequency with which you would like to output the graph");
      69          10 :   keys.add("compulsory","FILE","the name of the file on which to output the data");
      70          10 :   keys.add("compulsory","MAXCONNECT","0","maximum number of connections that can be formed by any given node in the graph. "
      71             :            "By default this is set equal to zero and the number of connections is set equal to the number "
      72             :            "of nodes.  You only really need to set this if you are working with a very large system and "
      73             :            "memory is at a premium");
      74             : 
      75           5 : }
      76             : 
      77           4 : DumpGraph::DumpGraph( const ActionOptions& ao):
      78             :   Action(ao),
      79             :   ActionPilot(ao),
      80           4 :   mymatrix(NULL)
      81             : {
      82           8 :   parse("MAXCONNECT",maxconnections); std::string mstring; parse("MATRIX",mstring);
      83           4 :   AdjacencyMatrixBase* mm = plumed.getActionSet().selectWithLabel<AdjacencyMatrixBase*>( mstring );
      84           4 :   if( !mm ) error("found no action in set with label " + mstring + " that calculates matrix");
      85           4 :   log.printf("  printing graph for matrix calculated by action %s\n", mm->getLabel().c_str() );
      86             : 
      87             :   // Retrieve the adjacency matrix of interest
      88           4 :   for(unsigned i=0; i<mm->getNumberOfVessels(); ++i) {
      89           4 :     mymatrix = dynamic_cast<AdjacencyMatrixVessel*>( mm->getPntrToVessel(i) );
      90           4 :     if( mymatrix ) break ;
      91             :   }
      92           4 :   if( !mymatrix ) error( mm->getLabel() + " does not calculate an adjacency matrix");
      93           4 :   if( !mymatrix->isSymmetric() ) error("input contact matrix must be symmetric");
      94           4 :   if( maxconnections==0 ) maxconnections=mymatrix->getNumberOfRows();
      95           4 :   parse("FILE",filename);
      96           4 :   log.printf("  printing graph to file named %s \n",filename.c_str() );
      97           4 :   checkRead();
      98           4 : }
      99             : 
     100           4 : void DumpGraph::update() {
     101           4 :   OFile ofile; ofile.link(*this); ofile.setBackupString("graph");
     102           4 :   ofile.open( filename ); ofile.printf("graph G { \n");
     103             :   // Print all nodes
     104        1336 :   for(unsigned i=0; i<mymatrix->getNumberOfRows(); ++i) ofile.printf("%u [label=\"%u\"];\n",i,i);
     105             :   // Now retrieve connectivitives
     106           4 :   unsigned nedge; std::vector<std::pair<unsigned,unsigned> > edge_list( mymatrix->getNumberOfRows()*maxconnections );
     107           4 :   mymatrix->retrieveEdgeList( nedge, edge_list );
     108        6388 :   for(unsigned i=0; i<nedge; ++i) ofile.printf("%u -- %u \n", edge_list[i].first, edge_list[i].second );
     109           4 :   ofile.printf("} \n");
     110           4 : }
     111             : 
     112             : }
     113             : }
     114             : 

Generated by: LCOV version 1.15