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 13793 : PLUMED_REGISTER_ACTION(DumpGraph,"DUMPGRAPH") 64 : 65 8 : void DumpGraph::registerKeywords( Keywords& keys ) { 66 8 : Action::registerKeywords( keys ); 67 8 : ActionPilot::registerKeywords( keys ); 68 16 : keys.add("compulsory","MATRIX","the action that calculates the adjacency matrix vessel we would like to analyze"); 69 16 : keys.add("compulsory","STRIDE","1","the frequency with which you would like to output the graph"); 70 16 : keys.add("compulsory","FILE","the name of the file on which to output the data"); 71 16 : keys.add("compulsory","MAXCONNECT","0","maximum number of connections that can be formed by any given node in the graph. " 72 : "By default this is set equal to zero and the number of connections is set equal to the number " 73 : "of nodes. You only really need to set this if you are working with a very large system and " 74 : "memory is at a premium"); 75 : 76 8 : } 77 : 78 4 : DumpGraph::DumpGraph( const ActionOptions& ao): 79 : Action(ao), 80 : ActionPilot(ao), 81 4 : mymatrix(NULL) { 82 8 : parse("MAXCONNECT",maxconnections); 83 : std::string mstring; 84 4 : parse("MATRIX",mstring); 85 4 : AdjacencyMatrixBase* mm = plumed.getActionSet().selectWithLabel<AdjacencyMatrixBase*>( mstring ); 86 4 : if( !mm ) { 87 0 : error("found no action in set with label " + mstring + " that calculates matrix"); 88 : } 89 4 : log.printf(" printing graph for matrix calculated by action %s\n", mm->getLabel().c_str() ); 90 : 91 : // Retrieve the adjacency matrix of interest 92 4 : for(unsigned i=0; i<mm->getNumberOfVessels(); ++i) { 93 4 : mymatrix = dynamic_cast<AdjacencyMatrixVessel*>( mm->getPntrToVessel(i) ); 94 4 : if( mymatrix ) { 95 : break ; 96 : } 97 : } 98 4 : if( !mymatrix ) { 99 0 : error( mm->getLabel() + " does not calculate an adjacency matrix"); 100 : } 101 4 : if( !mymatrix->isSymmetric() ) { 102 0 : error("input contact matrix must be symmetric"); 103 : } 104 4 : if( maxconnections==0 ) { 105 4 : maxconnections=mymatrix->getNumberOfRows(); 106 : } 107 4 : parse("FILE",filename); 108 4 : log.printf(" printing graph to file named %s \n",filename.c_str() ); 109 4 : checkRead(); 110 4 : } 111 : 112 4 : void DumpGraph::update() { 113 4 : OFile ofile; 114 4 : ofile.link(*this); 115 4 : ofile.setBackupString("graph"); 116 4 : ofile.open( filename ); 117 4 : ofile.printf("graph G { \n"); 118 : // Print all nodes 119 1336 : for(unsigned i=0; i<mymatrix->getNumberOfRows(); ++i) { 120 1332 : ofile.printf("%u [label=\"%u\"];\n",i,i); 121 : } 122 : // Now retrieve connectivitives 123 : unsigned nedge; 124 4 : std::vector<std::pair<unsigned,unsigned> > edge_list( mymatrix->getNumberOfRows()*maxconnections ); 125 4 : mymatrix->retrieveEdgeList( nedge, edge_list ); 126 6388 : for(unsigned i=0; i<nedge; ++i) { 127 6384 : ofile.printf("%u -- %u \n", edge_list[i].first, edge_list[i].second ); 128 : } 129 4 : ofile.printf("} \n"); 130 4 : } 131 : 132 : } 133 : } 134 :