Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2019-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 "CLTool.h"
23 : #include "core/CLToolRegister.h"
24 : #include "tools/Tools.h"
25 : #include "config/Config.h"
26 : #include "core/ActionRegister.h"
27 : #include "core/ActionWithValue.h"
28 : #include "core/ActionWithVirtualAtom.h"
29 : #include "core/ActionShortcut.h"
30 : #include "core/ActionSet.h"
31 : #include "core/PlumedMain.h"
32 : #include "tools/Communicator.h"
33 : #include "tools/IFile.h"
34 : #include <cstdio>
35 : #include <string>
36 : #include <vector>
37 : #include <iostream>
38 : #include <fstream>
39 :
40 : namespace PLMD {
41 : namespace cltools {
42 :
43 : //+PLUMEDOC TOOLS gen_example
44 : /*
45 : gen_example is a tool that you can use to construct an example for the manual that users can interact with to understand
46 :
47 : The example constructed by this action is in html. In all probability you will never need to use this
48 : tool. However, it is used within the scripts that generate the html manual for PLUMED. If you need to use this
49 : tool outside those scripts the input is specified using the following command line arguments.
50 :
51 : THIS COMMAND WILL LIKELY BE DELETED SOON AS IT HAS BEEN SUPERCEDED BY PLUMEDTOHTML.
52 :
53 : ## Examples
54 :
55 : The following generates an example based on the contents of the plumed file plumed.dat
56 :
57 : ```plumed
58 : plumed gen_example --plumed plumed.dat --status working
59 : ```
60 :
61 :
62 : */
63 : //+ENDPLUMEDOC
64 :
65 : class GenExample:
66 : public CLTool {
67 : private:
68 : int multi;
69 : std::string status, version;
70 : Communicator intracomm;
71 : Communicator intercomm;
72 : public:
73 : static void registerKeywords( Keywords& keys );
74 : explicit GenExample(const CLToolOptions& co );
75 : int main(FILE* in, FILE*out,Communicator& pc) override;
76 5 : std::string description()const override {
77 5 : return "construct an example for the manual that users can interact with";
78 : }
79 : void printExampleInput( const std::vector<std::vector<std::string> >& input, const std::string& egname, const std::string& divname, std::ofstream& ofile );
80 : std::vector<std::vector<std::string> > createLongInput( const std::vector<std::vector<std::string> >& input );
81 : };
82 :
83 16259 : PLUMED_REGISTER_CLTOOL(GenExample,"gen_example")
84 :
85 5418 : void GenExample::registerKeywords( Keywords& keys ) {
86 5418 : CLTool::registerKeywords( keys );
87 5418 : keys.add("compulsory","--plumed","plumed.dat","convert the input in this file to the html manual");
88 5418 : keys.add("compulsory","--out","example.html","the file on which to output the example in html");
89 5418 : keys.add("compulsory","--name","ppp","the name to use for this particular input");
90 5418 : keys.add("compulsory","--status","nobadge","whether or not the input file works");
91 5418 : keys.add("compulsory","--multi","0","set number of replicas for multi environment (needs MPI)");
92 5418 : }
93 :
94 5 : GenExample::GenExample(const CLToolOptions& co ):
95 : CLTool(co),
96 5 : multi(0),
97 10 : status("nobadge"),
98 5 : version("master") {
99 5 : inputdata=commandline;
100 5 : }
101 :
102 0 : int GenExample::main(FILE* in, FILE*out,Communicator& pc) {
103 :
104 : // set up for multi replica driver:
105 0 : parse("--multi",multi);
106 0 : if(multi) {
107 0 : int ntot=pc.Get_size();
108 0 : int nintra=ntot/multi;
109 0 : if(multi*nintra!=ntot) {
110 0 : error("invalid number of processes for multi environment");
111 : }
112 0 : pc.Split(pc.Get_rank()/nintra,pc.Get_rank(),intracomm);
113 0 : pc.Split(pc.Get_rank()%nintra,pc.Get_rank(),intercomm);
114 : } else {
115 0 : intracomm.Set_comm(pc.Get_comm());
116 : }
117 :
118 0 : if( config::getVersionLong().find("dev")==std::string::npos ) {
119 0 : version="v"+config::getVersion();
120 : }
121 : std::string fname, egname, outfile;
122 0 : parse("--plumed",fname);
123 0 : parse("--name",egname);
124 0 : parse("--out",outfile);
125 0 : parse("--status",status);
126 :
127 0 : int r=0;
128 0 : if(intracomm.Get_rank()==0) {
129 0 : r=intercomm.Get_rank();
130 : }
131 0 : intracomm.Bcast(r,0);
132 0 : if(r>0) {
133 : outfile="/dev/null";
134 : }
135 :
136 0 : IFile ifile;
137 0 : ifile.open(fname);
138 0 : ifile.allowNoEOL();
139 0 : std::ofstream ofile;
140 0 : ofile.open(outfile);
141 : std::vector<bool> shortcuts;
142 : bool hasshortcuts=false, endplumed=false;
143 : std::vector<std::vector<std::string> > input;
144 : std::vector<std::string> words;
145 0 : while( Tools::getParsedLine(ifile, words, false) ) {
146 0 : input.push_back( words );
147 0 : shortcuts.push_back( false );
148 0 : if( words.empty() || words[0].find("#")!=std::string::npos || endplumed ) {
149 0 : continue;
150 : }
151 0 : std::vector<std::string> interpreted( words );
152 0 : Tools::interpretLabel(interpreted);
153 0 : if( interpreted[0]=="ENDPLUMED" ) {
154 : endplumed=true;
155 : continue;
156 : }
157 0 : Keywords keys;
158 0 : actionRegister().getKeywords( interpreted[0], keys );
159 0 : if( status=="working" && keys.exists("IS_SHORTCUT") ) {
160 0 : hasshortcuts=shortcuts[shortcuts.size()-1]=true;
161 : }
162 0 : }
163 0 : ifile.close();
164 0 : if( hasshortcuts ) {
165 0 : ofile<<"<div style=\"width: 80%; float:left\" id=\"value_details_"<<egname<<"\"> Click on the labels of the actions for more information on what each action computes </div>\n";
166 0 : ofile<<"<div style=\"width: 10%; float:left\"><button type=\"button\" id=\""<<egname<<"_button\" onclick=\'swapInput(\""<<egname<<"\")\'>contract shortcuts</button></div>";
167 : } else {
168 0 : ofile<<"<div style=\"width: 90%; float:left\" id=\"value_details_"<<egname<<"\"> Click on the labels of the actions for more information on what each action computes </div>\n";
169 : }
170 0 : ofile<<"<div style=\"width: 10%; float:left\">";
171 0 : ofile<<"<img src=\"https://img.shields.io/badge/";
172 0 : if(status=="working") {
173 0 : ofile<<version<<"-passing-green";
174 0 : } else if(status=="broken") {
175 0 : ofile<<version<<"-failed-red";
176 0 : } else if(status=="loads") {
177 0 : ofile<<"with-LOAD-yellow";
178 0 : } else if(status=="incomplete") {
179 0 : ofile<<version<<"-incomplete-yellow";
180 0 : } else if ( status=="nobadge" ) {
181 0 : ofile<<"nobadge-36454F";
182 : } else {
183 0 : error("unknown status: "+status);
184 : }
185 0 : ofile<<".svg\" alt=\"tested on "<<version<<"\" /></div>";
186 0 : ofile.flush();
187 0 : if( hasshortcuts ) {
188 : // Write out the short version of the input
189 0 : ofile<<"<div style=\"width: 100%; float:left\" id=\"input_"<<egname<<"\"></div>"<<std::endl;
190 : // Write an extra pre to make sure the html after the example is put in the right place on the page
191 0 : ofile<<"<pre style=\"width: 97%;\" class=\"fragment\"></pre>"<<std::endl;
192 0 : ofile<<"<script type=\"text/javascript\">"<<std::endl;
193 0 : ofile<<"if (window.addEventListener) { // Mozilla, Netscape, Firefox"<<std::endl;
194 0 : ofile<<" window.addEventListener('load', "<<egname<<"Load, false);"<<std::endl;
195 0 : ofile<<"} else if (window.attachEvent) { // IE"<<std::endl;
196 0 : ofile<<" window.attachEvent('onload', "<<egname<<"Load);"<<std::endl;
197 0 : ofile<<"}"<<std::endl;
198 0 : ofile<<"function "<<egname<<"Load(event) {"<<std::endl;
199 0 : ofile<<" swapInput(\""<<egname<<"\");"<<std::endl;
200 0 : ofile<<"}"<<std::endl;
201 0 : ofile<<"</script>"<<std::endl;
202 0 : ofile<<"<div style=\"display:none;\" id=\""<<egname<<"short\">"<<std::endl;
203 0 : printExampleInput( input, egname + "short", egname, ofile );
204 0 : ofile<<"</div>"<<std::endl;
205 : // Write out long version of the input
206 0 : ofile<<"<div style=\"display:none;\" id=\""<<egname<<"long\">";
207 0 : std::vector<std::vector<std::string> > long_input = createLongInput( input );
208 0 : printExampleInput( long_input, egname + "long", egname, ofile );
209 0 : ofile<<"</div>"<<std::endl;
210 0 : } else {
211 0 : printExampleInput( input, egname, egname, ofile );
212 : }
213 0 : ofile.close();
214 0 : return 0;
215 0 : }
216 :
217 0 : std::vector<std::vector<std::string> > GenExample::createLongInput( const std::vector<std::vector<std::string> >& input ) {
218 : std::vector<std::vector<std::string> > long_input;
219 0 : PlumedMain myplumed;
220 0 : int rr=sizeof(double), natoms=10000000;
221 0 : double kt=2.49;
222 0 : myplumed.cmd("setRealPrecision",&rr);
223 0 : if(Communicator::initialized()) {
224 0 : if(multi) {
225 0 : if(intracomm.Get_rank()==0) {
226 0 : myplumed.cmd("GREX setMPIIntercomm",&intercomm.Get_comm());
227 : }
228 0 : myplumed.cmd("GREX setMPIIntracomm",&intracomm.Get_comm());
229 0 : myplumed.cmd("GREX init");
230 : }
231 0 : myplumed.cmd("setMPIComm",&intracomm.Get_comm());
232 : }
233 : bool endplumed=false;
234 0 : myplumed.cmd("setNatoms",&natoms);
235 0 : myplumed.cmd("setKbT",&kt);
236 0 : myplumed.cmd("init");
237 0 : for(unsigned ll=0; ll<input.size(); ++ll) {
238 0 : if( input[ll].empty() || endplumed ) {
239 0 : long_input.push_back( input[ll] );
240 0 : continue;
241 : }
242 0 : if( input[ll][0].find("#")!=std::string::npos ) {
243 0 : long_input.push_back( input[ll] );
244 0 : continue;
245 : }
246 0 : std::vector<std::string> interpreted( input[ll] );
247 0 : Tools::interpretLabel(interpreted);
248 0 : if( interpreted[0]=="ENDPLUMED" ) {
249 : endplumed=true;
250 0 : long_input.push_back( input[ll] );
251 : continue;
252 : }
253 0 : Keywords keys;
254 0 : plumed_assert( actionRegister().check( interpreted[0] ) );
255 0 : actionRegister().getKeywords( interpreted[0], keys );
256 : std::string lab, myinputline;
257 0 : if( Tools::parse(interpreted, "LABEL", lab ) ) {
258 0 : myinputline = lab + ": ";
259 : }
260 0 : myinputline += interpreted[0] + " ";
261 : bool trailingcomment=false;
262 0 : for(unsigned i=1; i<interpreted.size(); ++i) {
263 0 : if( trailingcomment && interpreted[i]=="@newline") {
264 : trailingcomment=false;
265 0 : continue;
266 : }
267 0 : if( interpreted[i].find("#")!=std::string::npos ) {
268 : trailingcomment=true;
269 0 : continue;
270 : }
271 0 : if( interpreted[i]=="@newline" || interpreted[i]=="..." ) {
272 0 : continue;
273 : }
274 : std::size_t pos = 0;
275 0 : while ((pos = interpreted[i].find("@newline",pos)) != std::string::npos) {
276 0 : interpreted[i].replace(pos, 8, "\n");
277 0 : pos++;
278 : }
279 0 : myinputline += interpreted[i] + " ";
280 : }
281 0 : if( status=="working" && keys.exists("IS_SHORTCUT") ) {
282 0 : myplumed.readInputLine( myinputline );
283 0 : ActionShortcut* as=dynamic_cast<ActionShortcut*>( myplumed.getActionSet()[myplumed.getActionSet().size()-1].get() );
284 0 : plumed_assert( as );
285 0 : std::vector<std::string> shortcut_commands = as->getSavedInputLines();
286 0 : for(unsigned i=0; i<shortcut_commands.size(); ++i) {
287 0 : std::vector<std::string> words = Tools::getWords( shortcut_commands[i] );
288 0 : long_input.push_back( words );
289 0 : }
290 0 : } else {
291 0 : long_input.push_back( input[ll] );
292 0 : myplumed.readInputLine( myinputline );
293 : }
294 0 : }
295 0 : return long_input;
296 0 : }
297 :
298 0 : void GenExample::printExampleInput( const std::vector<std::vector<std::string> >& input, const std::string& egname, const std::string& divname, std::ofstream& ofile ) {
299 0 : PlumedMain myplumed;
300 0 : int rr=sizeof(double), natoms=10000000;
301 0 : double kt=2.49;
302 0 : myplumed.cmd("setRealPrecision",&rr);
303 0 : if(Communicator::initialized()) {
304 0 : if(multi) {
305 0 : if(intracomm.Get_rank()==0) {
306 0 : myplumed.cmd("GREX setMPIIntercomm",&intercomm.Get_comm());
307 : }
308 0 : myplumed.cmd("GREX setMPIIntracomm",&intracomm.Get_comm());
309 0 : myplumed.cmd("GREX init");
310 : }
311 0 : myplumed.cmd("setMPIComm",&intracomm.Get_comm());
312 : }
313 0 : myplumed.cmd("setNatoms",&natoms);
314 0 : myplumed.cmd("setKbT",&kt);
315 0 : myplumed.cmd("init");
316 : std::vector<std::string> labellist;
317 : bool endplumed=false;
318 0 : ofile<<"<pre style=\"width: 97%;\" class=\"fragment\">"<<std::endl;
319 0 : for(unsigned ll=0; ll<input.size(); ++ll) {
320 0 : if( input[ll].empty() ) {
321 : ofile<<std::endl;
322 0 : continue;
323 : }
324 0 : if( input[ll][0].find("#")!=std::string::npos || endplumed ) {
325 0 : ofile<<"<span style=\"color:blue\">"<<input[ll][0];
326 0 : for(unsigned i=1; i<input[ll].size(); ++i) {
327 0 : ofile<<" "<<input[ll][i];
328 : }
329 0 : ofile<<"</span>"<<std::endl;;
330 : } else {
331 : // Interpret the label if this needs to be done
332 0 : std::vector<std::string> interpreted( input[ll] );
333 0 : Tools::interpretLabel(interpreted);
334 : std::string lab, myinputline;
335 : // Now read in the label
336 0 : if( Tools::parse(interpreted,"LABEL",lab) ) {
337 0 : ofile<<"<b name=\""<<egname<<lab<<"\" onclick=\'showPath(\""<<divname<<"\",\""<<egname<<lab<<"\")\'>"<<lab<<": </b>";
338 0 : labellist.push_back(lab);
339 0 : myinputline = lab + ": ";
340 : }
341 : // Print the keyword in use in the action
342 0 : std::string action = interpreted[0];
343 0 : myinputline += interpreted[0] + " ";
344 0 : if( action=="ENDPLUMED" ) {
345 : endplumed=true;
346 : }
347 0 : Keywords keys;
348 0 : actionRegister().getKeywords( interpreted[0], keys );
349 : // Handle conversion of action names to links
350 0 : std::transform(action.begin(), action.end(), action.begin(), [](unsigned char c) {
351 0 : return std::tolower(c);
352 : });
353 0 : ofile<<"<a href=\"https://www.plumed.org/doc-"<<version<<"/user-doc/html/";
354 : for(unsigned n=0;; ++n) {
355 0 : std::size_t und=action.find_first_of("_");
356 0 : if( und==std::string::npos ) {
357 : break;
358 : }
359 0 : std::string first=action.substr(0,und);
360 0 : for(auto c : first ) {
361 0 : if( isdigit(c) ) {
362 0 : ofile<<c;
363 : } else {
364 0 : ofile<<"_"<<c;
365 : }
366 : }
367 0 : ofile<<"_";
368 0 : action=action.substr(und+1);
369 0 : }
370 0 : for(auto c : action ) {
371 0 : if( isdigit(c) ) {
372 0 : ofile<<c;
373 : } else {
374 0 : ofile<<"_"<<c;
375 : }
376 : }
377 0 : ofile<<".html\" style=\"color:green\">"<<interpreted[0]<<"</a> ";
378 : // And write out everything else in the input line
379 : bool trailingcomment=false;
380 0 : for(unsigned i=1; i<interpreted.size(); ++i) {
381 0 : if( interpreted[i]=="@newline" && i==1 ) {
382 0 : ofile<<"..."<<std::endl<<" ";
383 0 : continue;
384 0 : } else if( interpreted[i]=="@newline" ) {
385 0 : if( trailingcomment ) {
386 0 : ofile<<"</span>";
387 : trailingcomment=false;
388 : }
389 0 : if( interpreted[i+1]=="..." ) {
390 : ofile<<std::endl;
391 : } else {
392 0 : ofile<<std::endl<<" ";
393 : }
394 0 : continue;
395 0 : } else if( interpreted[i]=="__FILL__" ) {
396 0 : if( status!="incomplete" ) {
397 0 : error("found __FILL__ statement but status is " + status);
398 : }
399 0 : ofile<<"<span style=\"background-color:yellow\">__FILL__</span>";
400 0 : continue;
401 0 : } else if( interpreted[i]==action ) {
402 0 : continue;
403 : }
404 0 : if( interpreted[i].find("#")!=std::string::npos ) {
405 : trailingcomment=true;
406 0 : ofile<<"<span style=\"color:blue\">";
407 : }
408 :
409 0 : if( !trailingcomment ) {
410 0 : std::size_t eq=interpreted[i].find_first_of("=");
411 0 : if( eq!=std::string::npos ) {
412 0 : std::string keyword=interpreted[i].substr(0,eq), rest=interpreted[i].substr(eq+1);
413 0 : ofile<<"<span class=\"tooltip\">"<<keyword<<"<span class=\"right\">"<<keys.getTooltip(keyword)<<"<i></i></span></span>";
414 0 : if( rest=="__FILL__" ) {
415 0 : if( status!="incomplete" ) {
416 0 : error("found __FILL__ statement but status is " + status);
417 : }
418 0 : ofile<<"=<span style=\"background-color:yellow\">__FILL__</span>";
419 0 : } else if( rest.find_first_of("{")!=std::string::npos ) {
420 : std::size_t pos = 0;
421 0 : while ((pos = rest.find("@newline",pos)) != std::string::npos) {
422 0 : rest.replace(pos, 8, "\n");
423 0 : pos++;
424 : }
425 0 : ofile<<"="<<rest<<" ";
426 0 : myinputline += keyword + "=" + rest + " ";
427 : } else {
428 0 : std::vector<std::string> args=Tools::getWords(rest,"\t\n ,");
429 0 : ofile<<"=";
430 0 : for(unsigned i=0; i<args.size(); ++i) {
431 : bool islabel=false;
432 : std::string thislab;
433 0 : for(unsigned j=0; j<labellist.size(); ++j) {
434 0 : std::size_t dot=args[i].find_first_of(".");
435 0 : std::string lll=args[i].substr(0,dot);
436 0 : if( lll==labellist[j] ) {
437 : islabel=true;
438 : thislab=labellist[j];
439 : break;
440 : }
441 : }
442 : if( islabel ) {
443 0 : ofile<<"<b name=\""<<egname<<thislab<<"\">"<<args[i]<<"</b>";
444 : } else {
445 : ofile<<args[i];
446 : }
447 0 : if( i!=args.size()-1 ) {
448 0 : ofile<<",";
449 : }
450 : }
451 0 : myinputline += interpreted[i] + " ";
452 0 : }
453 0 : ofile<<" ";
454 0 : } else if( interpreted[i]!="@newline" && interpreted[i]!="..." ) {
455 0 : myinputline += interpreted[i] + " ";
456 0 : ofile<<"<span class=\"tooltip\">"<<interpreted[i]<<"<span class=\"right\">"<<keys.getTooltip(interpreted[i])<<"<i></i></span></span> ";
457 0 : } else if( interpreted[i]=="..." ) {
458 0 : ofile<<"...";
459 : }
460 : } else {
461 0 : ofile<<interpreted[i]<<" ";
462 : }
463 : }
464 0 : if( trailingcomment ) {
465 0 : ofile<<"</span>";
466 : }
467 : // This builds the hidden content that tells the user about what is calculated
468 0 : if( status=="working" ) {
469 0 : ofile<<"<span style=\"display:none;\" id=\""<<egname<<lab<<"\">";
470 0 : ofile<<"The "<<interpreted[0]<<" action with label <b>"<<lab<<"</b>";
471 0 : myplumed.readInputLine( myinputline );
472 0 : ActionWithValue* av=myplumed.getActionSet().selectWithLabel<ActionWithValue*>(lab);
473 0 : if( av ) {
474 0 : if( av->getNumberOfComponents()==1 ) {
475 0 : ofile<<" calculates a single scalar value";
476 0 : } else if( av->getNumberOfComponents()>0 ) {
477 0 : ofile<<" calculates the following quantities:"<<std::endl;
478 0 : ofile<<"<table align=\"center\" frame=\"void\" width=\"95%%\" cellpadding=\"5%%\">"<<std::endl;
479 0 : ofile<<"<tr><td width=\"5%%\"><b> Quantity </b> </td><td><b> Description </b> </td></tr>"<<std::endl;
480 0 : unsigned ncomp = av->getNumberOfComponents();
481 0 : for(unsigned k=0; k<ncomp; ++k ) {
482 0 : std::string myname = av->copyOutput(k)->getName();
483 0 : std::size_t dot=myname.find_first_of(".");
484 0 : std::string tname=myname.substr(dot+1);
485 0 : ofile<<"<tr><td width=\"5%%\">"<<myname<<"</td><td>"<<av->getOutputComponentDescription(tname,keys)<<"</td></tr>"<<std::endl;
486 : }
487 0 : ofile<<"</table>"<<std::endl;
488 : }
489 : } else {
490 0 : ActionWithVirtualAtom* avv=myplumed.getActionSet().selectWithLabel<ActionWithVirtualAtom*>(lab);
491 0 : if( avv ) {
492 0 : ofile<<" calculates the position of a virtual atom";
493 0 : } else if( interpreted[0]=="GROUP" ) {
494 0 : ofile<<" defines a group of atoms so that they can be referred to later in the input";
495 : }
496 : }
497 0 : ofile<<"</span>"<<std::endl;
498 : } else {
499 0 : ofile<<"<span style=\"display:none;\" id=\""<<egname<<lab<<"\"> You cannot view the components that are calculated by each action for this input file. Sorry </span>"<<std::endl;
500 : }
501 0 : }
502 0 : ofile.flush();
503 : }
504 0 : ofile<<"</pre>"<<std::endl;
505 0 : }
506 :
507 : } // End of namespace
508 : }
|