Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2012-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 "Vessel.h"
23 : #include "ActionWithVessel.h"
24 : #include "tools/Exception.h"
25 : #include "tools/Communicator.h"
26 : #include "tools/Log.h"
27 :
28 : namespace PLMD {
29 : namespace vesselbase {
30 :
31 1613 : Keywords VesselOptions::emptyKeys;
32 :
33 534 : VesselOptions::VesselOptions(const std::string& thisname, const std::string& thislab, const unsigned& nlab, const std::string& params, ActionWithVessel* aa ):
34 : myname(thisname),
35 : mylabel(thislab),
36 534 : numlab(nlab),
37 : action(aa),
38 : keywords(emptyKeys),
39 1602 : parameters(params)
40 : {
41 534 : }
42 :
43 398 : VesselOptions::VesselOptions(const VesselOptions& da, const Keywords& keys ):
44 : myname(da.myname),
45 : mylabel(da.mylabel),
46 398 : numlab(da.numlab),
47 398 : action(da.action),
48 : keywords(keys),
49 1592 : parameters(da.parameters)
50 : {
51 398 : }
52 :
53 398 : void Vessel::registerKeywords( Keywords& keys ) {
54 398 : plumed_assert( keys.size()==0 );
55 1592 : keys.add("optional","LABEL","the label used to reference this particular quantity");
56 398 : }
57 :
58 725 : std::string Vessel::transformName( const std::string& name ) {
59 : std::string tlabel=name;
60 : // Convert to lower case
61 725 : std::transform( tlabel.begin(), tlabel.end(), tlabel.begin(), tolower );
62 : // Remove any underscore characters (as these are reserved)
63 : for(unsigned i=0;; ++i) {
64 : std::size_t num=tlabel.find_first_of("_");
65 895 : if( num==std::string::npos ) break;
66 170 : tlabel.erase( tlabel.begin() + num, tlabel.begin() + num + 1 );
67 170 : }
68 725 : return tlabel;
69 : }
70 :
71 534 : Vessel::Vessel( const VesselOptions& da ):
72 : myname(da.myname),
73 534 : numlab(da.numlab),
74 534 : action(da.action),
75 : line(Tools::getWords( da.parameters )),
76 534 : keywords(da.keywords),
77 : finished_read(false),
78 534 : comm(da.action->comm),
79 3204 : log((da.action)->log)
80 : {
81 534 : if( da.mylabel.length()>0) {
82 0 : mylabel=da.mylabel;
83 : } else {
84 1427 : if( keywords.exists("LABEL") ) parse("LABEL",mylabel);
85 534 : if( mylabel.length()==0 && numlab>=0 ) {
86 844 : mylabel=transformName( myname ); std::string nn;
87 518 : if(numlab>0) { Tools::convert( numlab, nn ); mylabel = mylabel + "-" + nn; }
88 : }
89 : }
90 534 : }
91 :
92 78 : std::string Vessel::getName() const {
93 78 : return myname;
94 : }
95 :
96 1096 : std::string Vessel::getLabel() const {
97 1096 : return mylabel;
98 : }
99 :
100 162 : std::string Vessel::getAllInput() {
101 : std::string fullstring;
102 1887 : for(unsigned i=0; i<line.size(); ++i) {
103 1563 : fullstring = fullstring + " " + line[i];
104 : }
105 162 : line.clear(); line.resize(0);
106 162 : return fullstring;
107 : }
108 :
109 115 : void Vessel::parseFlag(const std::string&key, bool & t) {
110 : // Check keyword has been registered
111 115 : plumed_massert(keywords.exists(key), "keyword " + key + " has not been registered");
112 : // Check keyword is a flag
113 230 : if(!keywords.style(key,"nohtml")) {
114 230 : plumed_massert(keywords.style(key,"flag"), "keyword " + key + " is not a flag");
115 : }
116 :
117 : // Read in the flag otherwise get the default value from the keywords object
118 115 : if(!Tools::parseFlag(line,key,t)) {
119 188 : if( keywords.style(key,"nohtml") ) {
120 0 : t=false;
121 282 : } else if ( !keywords.getLogicalDefault(key,t) ) {
122 0 : plumed_merror("there is a flag with no logical default in a vessel - weird");
123 : }
124 : }
125 115 : }
126 :
127 478 : void Vessel::checkRead() {
128 478 : if(!line.empty()) {
129 0 : std::string msg="cannot understand the following words from input : ";
130 0 : for(unsigned i=0; i<line.size(); i++) {
131 0 : if(i>0) msg = msg + ", ";
132 0 : msg = msg + line[i];
133 : }
134 0 : error(msg);
135 : }
136 478 : finished_read=true;
137 478 : std::string describe=description();
138 830 : if( describe.length()>0 ) log.printf(" %s\n", describe.c_str() );
139 478 : }
140 :
141 0 : void Vessel::error( const std::string& msg ) {
142 0 : action->log.printf("ERROR for keyword %s in action %s with label %s : %s \n \n",myname.c_str(), (action->getName()).c_str(), (action->getLabel()).c_str(), msg.c_str() );
143 0 : if(finished_read) keywords.print( log );
144 0 : plumed_merror("ERROR for keyword " + myname + " in action " + action->getName() + " with label " + action->getLabel() + " : " + msg );
145 : }
146 :
147 : }
148 4839 : }
|