-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathcolvarmodule.cpp
More file actions
2794 lines (2289 loc) · 78.7 KB
/
colvarmodule.cpp
File metadata and controls
2794 lines (2289 loc) · 78.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- c++ -*-
// This file is part of the Collective Variables module (Colvars).
// The original version of Colvars and its updates are located at:
// https://github.com/Colvars/colvars
// Please update all Colvars source files before making any changes.
// If you wish to distribute your changes, please submit them to the
// Colvars repository at GitHub.
#include <iomanip>
#include <iostream>
#include <memory>
#include <vector>
#include "colvarmodule.h"
#include "colvar_gpu_support.h"
#include "colvarparse.h"
#include "colvarproxy.h"
#include "colvar.h"
#include "colvarbias.h"
#include "colvarbias_abf.h"
#include "colvarbias_abmd.h"
#include "colvarbias_alb.h"
#include "colvarbias_histogram.h"
#include "colvarbias_histogram_reweight_amd.h"
#include "colvarbias_meta.h"
#include "colvarbias_restraint.h"
#include "colvarbias_opes.h"
#include "colvarscript.h"
#include "colvaratoms.h"
#include "colvarcomp.h"
#include "colvars_memstream.h"
#include "colvars_version.h"
std::string colvarmodule::version() const
{
return std::string(COLVARS_VERSION);
}
int colvarmodule::version_number() const
{
return version_int;
}
int colvarmodule::patch_version_number() const
{
return COLVARS_PATCH_VERSION;
}
/// Track usage of Colvars features
class colvarmodule::usage {
public:
/// Constructor
usage();
/// Increment usage count for the given feature; return error if not found
int cite_feature(std::string const &feature);
/// Increment usage count for the given paper; return error if not found
int cite_paper(std::string const &paper);
/// Generate a report for used features (0 = URL, 1 = BibTeX)
std::string report(int flag);
protected:
/// Usage count for each feature
std::map<std::string, int> feature_count_;
/// Usage count for each cited paper
std::map<std::string, int> paper_count_;
/// URL for each paper
std::map<std::string, std::string> paper_url_;
/// BibTeX entry for each paper
std::map<std::string, std::string> paper_bibtex_;
/// Map code features to the relevant papers
std::map<std::string, std::string> feature_paper_map_;
};
namespace {
constexpr uint32_t colvars_magic_number = 2013813594;
}
colvarmodule::colvarmodule()
{
depth_s = 0;
log_level_ = 10;
xyz_reader_use_count = 0;
num_biases_types_used_ =
reinterpret_cast<void *>(new std::map<std::string, int>());
restart_version_str.clear();
restart_version_int = 0;
usage_ = new usage();
usage_->cite_feature("Colvars module");
}
void colvarmodule::init(colvarproxy *proxy_in)
{
if (proxy) {
// TODO relax this error to handle multiple molecules in VMD
// once the module is not static anymore
cvm::error("Error: trying to allocate the collective "
"variable module twice.\n", COLVARS_BUG_ERROR);
return;
}
proxy = proxy_in; // Pointer to the proxy object
parse = new colvarparse(); // Parsing object for global options
version_int = proxy->get_version_from_string(COLVARS_VERSION);
cvm::log(cvm::line_marker);
cvm::log(
"Initializing the collective variables module, version " + version() +
(patch_version_number() ? (" (patch " + cvm::to_str(patch_version_number()) + ")") : "") +
".\n");
cvm::log("Please cite Fiorin et al, Mol Phys 2013:\n"
" https://doi.org/10.1080/00268976.2013.813594\n"
"as well as all other papers listed below for individual features used.\n");
cvm::log("Summary of compile-time features available in this build:\n");
std::string cxx_lang_msg(" - C++ language version: " + cvm::to_str(__cplusplus));
#if defined(_WIN32) && !defined(__CYGWIN__)
cxx_lang_msg += std::string(" (warning: may not be accurate for this build)");
#endif
cxx_lang_msg += std::string("\n");
cvm::log(cxx_lang_msg);
if (proxy->check_replicas_enabled() == COLVARS_NOT_IMPLEMENTED) {
cvm::log(" - Multiple replicas: not available\n");
} else {
if (proxy->check_replicas_enabled() == COLVARS_OK) {
cvm::log(" - Multiple replicas: enabled (replica number " +
to_str(proxy->replica_index() + 1) + " of " + to_str(proxy->num_replicas()) + ")\n");
} else {
cvm::log(" - Multiple replicas: available, but not (yet) enabled\n");
}
}
#if defined(LEPTON)
cvm::log(" - Lepton custom functions: available\n");
#else
cvm::log(" - Lepton custom functions: not available\n");
#endif
#if defined(COLVARS_TCL)
cvm::log(" - Tcl interpreter: available\n");
#else
cvm::log(" - Tcl interpreter: not available\n");
#endif
// set initial default values
binary_restart = false;
char const *env_var = getenv("COLVARS_BINARY_RESTART");
if (env_var && atoi(env_var)) {
binary_restart = true;
}
// "it_restart" will be set by the input state file, if any;
// "it" should be updated by the proxy
colvarmodule::it = colvarmodule::it_restart = 0;
use_scripted_forces = false;
scripting_after_biases = false;
colvarmodule::debug_gradients_step_size = 1.0e-05;
colvarmodule::rotation::monitor_crossings = false;
colvarmodule::rotation::crossing_threshold = 1.0e-02;
cv_traj_freq = 100;
restart_out_freq = proxy->default_restart_frequency();
cv_traj_write_labels = true;
// Removes the need for proxy specializations to create this
proxy->script = new colvarscript(proxy, this);
#if defined (COLVARS_CUDA) || defined (COLVARS_HIP)
gpu_calc = nullptr;
#endif
}
colvarmodule * colvarmodule::main()
{
return proxy ? proxy->colvars : NULL;
}
std::vector<colvar *> *colvarmodule::variables()
{
return &colvars;
}
std::vector<colvar *> *colvarmodule::variables_active()
{
return &colvars_active;
}
std::vector<colvar *> *colvarmodule::variables_active_smp()
{
return &colvars_smp;
}
std::vector<int> *colvarmodule::variables_active_smp_items()
{
return &colvars_smp_items;
}
int colvarmodule::calc_component_smp(int i)
{
colvar *x = (*(variables_active_smp()))[i];
int x_item = (*(variables_active_smp_items()))[i];
if (cvm::debug()) {
cvm::log("Thread "+cvm::to_str(proxy->smp_thread_id())+"/"+
cvm::to_str(proxy->smp_num_threads())+
": calc_component_smp(), i = "+cvm::to_str(i)+", cv = "+
x->name+", cvc = "+cvm::to_str(x_item)+"\n");
}
return x->calc_cvcs(x_item, 1);
}
std::vector<colvarbias *> *colvarmodule::biases_active()
{
return &(biases_active_);
}
size_t colvarmodule::size() const
{
return colvars.size() + biases.size();
}
void colvarmodule::set_initial_step(step_number it_in)
{
cvm::log("Setting initial step number from MD engine: " + cvm::to_str(it_in) + "\n");
it = it_restart = it_in;
}
int colvarmodule::read_config_file(char const *config_filename)
{
cvm::log(cvm::line_marker);
cvm::log("Reading new configuration from file \""+
std::string(config_filename)+"\":\n");
// open the configfile
std::istream &config_s = proxy->input_stream(config_filename,
"configuration file/string");
if (!config_s) {
return cvm::error("Error: in opening configuration file \""+
std::string(config_filename)+"\".\n",
COLVARS_FILE_ERROR);
}
// read the config file into a string
std::string conf = "";
std::string line;
while (parse->read_config_line(config_s, line)) {
// Delete lines that contain only white space after removing comments
if (line.find_first_not_of(colvarparse::white_space) != std::string::npos)
conf.append(line+"\n");
}
proxy->close_input_stream(config_filename);
return parse_config(conf);
}
int colvarmodule::read_config_string(std::string const &config_str)
{
cvm::log(cvm::line_marker);
cvm::log("Reading new configuration:\n");
std::istringstream new_config_s(config_str);
// strip the comments away
std::string conf = "";
std::string line;
while (parse->read_config_line(new_config_s, line)) {
// Delete lines that contain only white space after removing comments
if (line.find_first_not_of(colvarparse::white_space) != std::string::npos)
conf.append(line+"\n");
}
return parse_config(conf);
}
std::istream & colvarmodule::getline(std::istream &is, std::string &line)
{
std::string l;
if (std::getline(is, l)) {
size_t const sz = l.size();
if (sz > 0) {
if (l[sz-1] == '\r' ) {
// Replace Windows newlines with Unix newlines
line = l.substr(0, sz-1);
} else {
line = l;
}
} else {
line.clear();
}
}
return is;
}
int colvarmodule::parse_config(std::string &conf)
{
// Auto-generated additional configuration
extra_conf.clear();
// Check that the input has matching braces
if (colvarparse::check_braces(conf, 0) != COLVARS_OK) {
return cvm::error("Error: unmatched curly braces in configuration.\n",
COLVARS_INPUT_ERROR);
}
// Check that the input has only ASCII characters, and warn otherwise
colvarparse::check_ascii(conf);
// Parse global options
if (catch_input_errors(parse_global_params(conf))) {
return get_error();
}
// Parse the options for collective variables
if (catch_input_errors(parse_colvars(conf))) {
return get_error();
}
// Parse the options for biases
if (catch_input_errors(parse_biases(conf))) {
return get_error();
}
// Done parsing known keywords, check that all keywords found were valid ones
if (catch_input_errors(parse->check_keywords(conf, "colvarmodule"))) {
return get_error();
}
// Parse auto-generated configuration (e.g. for back-compatibility)
if (extra_conf.size()) {
catch_input_errors(parse_global_params(extra_conf));
catch_input_errors(parse_colvars(extra_conf));
catch_input_errors(parse_biases(extra_conf));
parse->check_keywords(extra_conf, "colvarmodule");
extra_conf.clear();
if (get_error() != COLVARS_OK) return get_error();
}
cvm::log(cvm::line_marker);
cvm::log("Collective variables module (re)initialized.\n");
cvm::log(cvm::line_marker);
if (source_Tcl_script.size() > 0) {
run_tcl_script(source_Tcl_script);
}
return get_error();
}
std::string const & colvarmodule::get_config() const
{
return parse->get_config();
}
int colvarmodule::append_new_config(std::string const &new_conf)
{
extra_conf += new_conf;
return COLVARS_OK;
}
void colvarmodule::config_changed()
{
cv_traj_write_labels = true;
}
int colvarmodule::parse_global_params(std::string const &conf)
{
int error_code = COLVARS_OK;
// TODO document and then echo this keyword
parse->get_keyval(conf, "logLevel", log_level_, log_level_,
colvarparse::parse_silent);
{
std::string units;
if (parse->get_keyval(conf, "units", units)) {
units = colvarparse::to_lower_cppstr(units);
error_code |= proxy->set_unit_system(units, (colvars.size() != 0));
}
}
{
std::string index_file_name;
size_t pos = 0;
while (parse->key_lookup(conf, "indexFile", &index_file_name, &pos)) {
cvm::log("# indexFile = \""+index_file_name+"\"\n");
error_code |= read_index_file(index_file_name.c_str());
index_file_name.clear();
}
}
std::string smp;
if (parse->get_keyval(conf, "smp", smp)) {
if (smp == "cvcs" || smp == "on" || smp == "yes") {
if (proxy->set_smp_mode(colvarproxy_smp::smp_mode_t::cvcs) != COLVARS_OK) {
cvm::error("Colvars component-based parallelism is not implemented.\n");
return COLVARS_INPUT_ERROR;
}
} else if (smp == "inner_loop") {
if (proxy->set_smp_mode(colvarproxy_smp::smp_mode_t::inner_loop) != COLVARS_OK) {
cvm::error("SMP parallelism inside the calculation of Colvars components is not implemented.\n");
return COLVARS_INPUT_ERROR;
}
} else if (smp == "gpu") {
if (proxy->set_smp_mode(colvarproxy_smp::smp_mode_t::gpu) != COLVARS_OK) {
cvm::error("GPU parallelism is not implemented.\n");
return COLVARS_INPUT_ERROR;
} else {
#if defined (COLVARS_CUDA) || defined (COLVARS_HIP)
gpu_calc = std::unique_ptr<colvars_gpu::colvarmodule_gpu_calc>(
new colvars_gpu::colvarmodule_gpu_calc);
gpu_calc->init();
cvm::log("EXPERIMENTAL GPU parallelism will be applied inside:\n");
cvm::log(" - atom groups\n");
#endif
}
} else {
proxy->set_smp_mode(colvarproxy_smp::smp_mode_t::none);
cvm::log("SMP parallelism has been disabled.\n");
}
} else {
cvm::log("SMP parallelism is not set.\n");
cvm::log("Available SMP parallelism modes of this proxy are:\n");
const auto available_smp_modes = proxy->get_available_smp_modes();
for (size_t i = 0; i < available_smp_modes.size(); ++i) {
switch (available_smp_modes[i]) {
case colvarproxy_smp::smp_mode_t::cvcs: {
cvm::log(" - cvcs\n");
break;
}
case colvarproxy_smp::smp_mode_t::inner_loop: {
cvm::log(" - inner_loop\n");
break;
}
case colvarproxy_smp::smp_mode_t::gpu: {
cvm::log(" - gpu\n");
break;
}
case colvarproxy_smp::smp_mode_t::none: {
cvm::log(" - none\n");
break;
}
}
}
cvm::log("Set SMP parallelism to the preferred (default) mode to the proxy.\n");
// Find the proxy's preferred SMP mode if SMP is not defined
colvarproxy_smp::smp_mode_t preferred_smp_mode = proxy->get_preferred_smp_mode();
proxy->set_smp_mode(preferred_smp_mode);
switch (preferred_smp_mode) {
case colvarproxy_smp::smp_mode_t::cvcs: {
cvm::log("SMP parallelism will be applied to Colvars components.\n");
cvm::log(" - SMP parallelism: enabled (num. threads = " + to_str(proxy->smp_num_threads()) + ")\n");
break;
}
case colvarproxy_smp::smp_mode_t::inner_loop: {
cvm::log("SMP parallelism will be applied inside the Colvars components.\n");
cvm::log(" - SMP parallelism: enabled (num. threads = " + to_str(proxy->smp_num_threads()) + ")\n");
break;
}
case colvarproxy_smp::smp_mode_t::gpu: {
#if defined (COLVARS_CUDA) || defined (COLVARS_HIP)
gpu_calc = std::unique_ptr<colvars_gpu::colvarmodule_gpu_calc>(
new colvars_gpu::colvarmodule_gpu_calc);
gpu_calc->init();
#endif
cvm::log("EXPERIMENTAL GPU parallelism will be applied inside:\n");
cvm::log(" - atom groups\n");
break;
}
case colvarproxy_smp::smp_mode_t::none: {
cvm::log("SMP parallelism is disabled by default.\n");
break;
}
}
}
bool b_analysis = true;
if (parse->get_keyval(conf, "analysis", b_analysis, true, colvarparse::parse_silent)) {
cvm::log("Warning: keyword \"analysis\" is deprecated: it is now always set "
"to true; individual analyses are performed only if requested.");
}
parse->get_keyval(conf, "debugGradientsStepSize", debug_gradients_step_size,
debug_gradients_step_size,
colvarparse::parse_silent);
parse->get_keyval(conf, "monitorEigenvalueCrossing",
colvarmodule::rotation::monitor_crossings,
colvarmodule::rotation::monitor_crossings,
colvarparse::parse_silent);
parse->get_keyval(conf, "eigenvalueCrossingThreshold",
colvarmodule::rotation::crossing_threshold,
colvarmodule::rotation::crossing_threshold,
colvarparse::parse_silent);
parse->get_keyval(conf, "colvarsTrajFrequency", cv_traj_freq, cv_traj_freq);
if (cv_traj_freq % cvm::proxy->time_step_factor() != 0) {
cvm::error("colvarsTrajFrequency (currently " + cvm::to_str(cv_traj_freq)
+ ") must be a multiple of the global Colvars timestep multiplier ("
+ cvm::to_str(cvm::proxy->time_step_factor()) + ").\n", COLVARS_INPUT_ERROR);
}
parse->get_keyval(conf, "colvarsRestartFrequency",
restart_out_freq, restart_out_freq);
if (restart_out_freq % cvm::proxy->time_step_factor() != 0) {
cvm::error("colvarsRestartFrequency (currently " + cvm::to_str(restart_out_freq)
+ ") must be a multiple of the global Colvars timestep multiplier ("
+ cvm::to_str(cvm::proxy->time_step_factor()) + ").\n", COLVARS_INPUT_ERROR);
}
parse->get_keyval(conf, "scriptedColvarForces",
use_scripted_forces, use_scripted_forces);
parse->get_keyval(conf, "scriptingAfterBiases",
scripting_after_biases, scripting_after_biases);
#if defined(COLVARS_TCL)
parse->get_keyval(conf, "sourceTclFile", source_Tcl_script);
#endif
if (proxy->engine_name() == "GROMACS" && proxy->version_number() >= 20231003) {
parse->get_keyval(conf, "defaultInputStateFile", default_input_state_file_,
default_input_state_file_);
}
return error_code;
}
int colvarmodule::run_tcl_script(std::string const &filename) {
int result = COLVARS_OK;
#if defined(COLVARS_TCL)
result = proxy->tcl_run_file(filename);
#endif
return result;
}
int colvarmodule::parse_colvars(std::string const &conf)
{
if (cvm::debug())
cvm::log("Initializing the collective variables.\n");
std::string colvar_conf = "";
size_t pos = 0;
while (parse->key_lookup(conf, "colvar", &colvar_conf, &pos)) {
if (colvar_conf.size()) {
cvm::log(cvm::line_marker);
cvm::increase_depth();
colvars.push_back(new colvar());
if (((colvars.back())->init(colvar_conf) != COLVARS_OK) ||
((colvars.back())->check_keywords(colvar_conf, "colvar") != COLVARS_OK)) {
cvm::log("Error while constructing colvar number " +
cvm::to_str(colvars.size()) + " : deleting.");
delete colvars.back(); // the colvar destructor updates the colvars array
cvm::decrease_depth();
return COLVARS_ERROR;
}
cvm::decrease_depth();
} else {
cvm::error("Error: \"colvar\" keyword found without any configuration.\n", COLVARS_INPUT_ERROR);
return COLVARS_ERROR;
}
cvm::decrease_depth();
colvar_conf = "";
}
if (pos > 0) {
// One or more new variables were added
config_changed();
}
if (!colvars.size()) {
cvm::log("Warning: no collective variables defined.\n");
}
if (colvars.size())
cvm::log(cvm::line_marker);
cvm::log("Collective variables initialized, "+
cvm::to_str(colvars.size())+
" in total.\n");
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
}
bool colvarmodule::check_new_bias(std::string &conf, char const *key)
{
if (cvm::get_error() ||
(biases.back()->check_keywords(conf, key) != COLVARS_OK)) {
cvm::log("Error while constructing bias number " +
cvm::to_str(biases.size()) + " : deleting.\n");
delete biases.back(); // the bias destructor updates the biases array
return true;
}
return false;
}
template <class bias_type>
int colvarmodule::parse_biases_type(std::string const &conf,
char const *keyword)
{
// Allow camel case when calling, but use only lower case for parsing
std::string const &type_keyword = colvarparse::to_lower_cppstr(keyword);
// Check how many times this bias keyword was used, set default name
// accordingly
std::map<std::string, int> *num_biases_types_used =
reinterpret_cast<std::map<std::string, int> *>(num_biases_types_used_);
if (num_biases_types_used->count(type_keyword) == 0) {
(*num_biases_types_used)[type_keyword] = 0;
}
std::string bias_conf = "";
size_t conf_saved_pos = 0;
while (parse->key_lookup(conf, keyword, &bias_conf, &conf_saved_pos)) {
if (bias_conf.size()) {
cvm::log(cvm::line_marker);
cvm::increase_depth();
int &bias_count = (*num_biases_types_used)[type_keyword];
biases.push_back(new bias_type(type_keyword.c_str()));
bias_count += 1;
biases.back()->rank = bias_count;
biases.back()->init(bias_conf);
if (cvm::check_new_bias(bias_conf, keyword) != COLVARS_OK) {
return COLVARS_ERROR;
}
cvm::decrease_depth();
} else {
cvm::error("Error: keyword \""+std::string(keyword)+"\" found without configuration.\n",
COLVARS_INPUT_ERROR);
return COLVARS_ERROR;
}
bias_conf = "";
}
if (conf_saved_pos > 0) {
// One or more new biases were added
config_changed();
}
return COLVARS_OK;
}
int colvarmodule::parse_biases(std::string const &conf)
{
if (cvm::debug())
cvm::log("Initializing the collective variables biases.\n");
/// initialize ABF instances
parse_biases_type<colvarbias_abf>(conf, "abf");
/// initialize ABMD instances
parse_biases_type<colvarbias_abmd>(conf, "abmd");
/// initialize adaptive linear biases
parse_biases_type<colvarbias_alb>(conf, "ALB");
/// initialize harmonic restraints
parse_biases_type<colvarbias_restraint_harmonic>(conf, "harmonic");
/// initialize harmonic walls restraints
parse_biases_type<colvarbias_restraint_harmonic_walls>(conf, "harmonicWalls");
/// initialize histograms
parse_biases_type<colvarbias_histogram>(conf, "histogram");
/// initialize histogram restraints
parse_biases_type<colvarbias_restraint_histogram>(conf, "histogramRestraint");
/// initialize linear restraints
parse_biases_type<colvarbias_restraint_linear>(conf, "linear");
/// initialize metadynamics instances
parse_biases_type<colvarbias_meta>(conf, "metadynamics");
/// initialize reweightaMD instances
parse_biases_type<colvarbias_reweightaMD>(conf, "reweightaMD");
/// initialize OPES instances
parse_biases_type<colvarbias_opes>(conf, "opes_metad");
if (use_scripted_forces) {
cvm::log(cvm::line_marker);
cvm::increase_depth();
cvm::log("User forces script will be run at each bias update.\n");
cvm::decrease_depth();
}
std::vector<std::string> const time_biases = time_dependent_biases();
if (time_biases.size() > 1) {
cvm::log("WARNING: there are "+cvm::to_str(time_biases.size())+
" time-dependent biases with non-zero force parameters:\n"+
cvm::to_str(time_biases)+"\n"+
"Please ensure that their forces do not counteract each other.\n");
}
if (num_biases() || use_scripted_forces) {
cvm::log(cvm::line_marker);
cvm::log("Collective variables biases initialized, "+
cvm::to_str(num_biases())+" in total.\n");
} else {
if (!use_scripted_forces) {
cvm::log("No collective variables biases were defined.\n");
}
}
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
}
size_t colvarmodule::num_variables() const
{
return colvars.size();
}
size_t colvarmodule::num_variables_feature(int feature_id) const
{
size_t n = 0;
for (std::vector<colvar *>::const_iterator cvi = colvars.begin();
cvi != colvars.end();
cvi++) {
if ((*cvi)->is_enabled(feature_id)) {
n++;
}
}
return n;
}
size_t colvarmodule::num_biases() const
{
return biases.size();
}
size_t colvarmodule::num_biases_feature(int feature_id) const
{
size_t n = 0;
for (std::vector<colvarbias *>::const_iterator bi = biases.begin();
bi != biases.end();
bi++) {
if ((*bi)->is_enabled(feature_id)) {
n++;
}
}
return n;
}
size_t colvarmodule::num_biases_type(std::string const &type) const
{
size_t n = 0;
for (std::vector<colvarbias *>::const_iterator bi = biases.begin();
bi != biases.end();
bi++) {
if ((*bi)->bias_type == type) {
n++;
}
}
return n;
}
std::vector<std::string> const colvarmodule::time_dependent_biases() const
{
size_t i;
std::vector<std::string> biases_names;
for (i = 0; i < num_biases(); i++) {
if (biases[i]->is_enabled(colvardeps::f_cvb_apply_force) &&
biases[i]->is_enabled(colvardeps::f_cvb_active) &&
(biases[i]->is_enabled(colvardeps::f_cvb_history_dependent) ||
biases[i]->is_enabled(colvardeps::f_cvb_time_dependent))) {
biases_names.push_back(biases[i]->name);
}
}
return biases_names;
}
int colvarmodule::catch_input_errors(int result)
{
if (result != COLVARS_OK || get_error()) {
set_error_bits(result);
set_error_bits(COLVARS_INPUT_ERROR);
parse->clear();
return get_error();
}
return COLVARS_OK;
}
colvarbias * colvarmodule::bias_by_name(std::string const &name)
{
colvarmodule *cv = cvm::main();
for (std::vector<colvarbias *>::iterator bi = cv->biases.begin();
bi != cv->biases.end();
bi++) {
if ((*bi)->name == name) {
return (*bi);
}
}
return NULL;
}
colvar *colvarmodule::colvar_by_name(std::string const &name)
{
colvarmodule *cv = cvm::main();
for (std::vector<colvar *>::iterator cvi = cv->colvars.begin();
cvi != cv->colvars.end();
cvi++) {
if ((*cvi)->name == name) {
return (*cvi);
}
}
return NULL;
}
cvm::atom_group *colvarmodule::atom_group_soa_by_name(std::string const& name) {
colvarmodule *cv = cvm::main();
for (std::vector<cvm::atom_group *>::iterator agi = cv->named_atom_groups_soa.begin();
agi != cv->named_atom_groups_soa.end();
agi++) {
if ((*agi)->name == name) {
return (*agi);
}
}
return nullptr;
}
void colvarmodule::register_named_atom_group_soa(atom_group *ag) {
named_atom_groups_soa.push_back(ag);
}
void colvarmodule::unregister_named_atom_group_soa(atom_group *ag) {
for (std::vector<cvm::atom_group *>::iterator agi = named_atom_groups_soa.begin();
agi != named_atom_groups_soa.end();
agi++) {
if (*agi == ag) {
named_atom_groups_soa.erase(agi);
break;
}
}
}
int colvarmodule::change_configuration(std::string const &bias_name,
std::string const &conf)
{
// This is deprecated; supported strategy is to delete the bias
// and parse the new config
cvm::increase_depth();
colvarbias *b;
b = bias_by_name(bias_name);
if (b == NULL) {
cvm::error("Error: bias not found: " + bias_name);
return COLVARS_ERROR;
}
b->change_configuration(conf);
cvm::decrease_depth();
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
}
std::string colvarmodule::read_colvar(std::string const &name)
{
cvm::increase_depth();
colvar *c;
std::stringstream ss;
c = colvar_by_name(name);
if (c == NULL) {
cvm::error("Error: colvar not found: " + name);
return std::string();
}
ss << c->value();
cvm::decrease_depth();
return ss.str();
}
cvm::real colvarmodule::energy_difference(std::string const &bias_name,
std::string const &conf)
{
cvm::increase_depth();
colvarbias *b;
cvm::real energy_diff = 0.;
b = bias_by_name(bias_name);
if (b == NULL) {
cvm::error("Error: bias not found: " + bias_name);
return 0.;
}
energy_diff = b->energy_difference(conf);
cvm::decrease_depth();
return energy_diff;
}
int colvarmodule::calc()
{
int error_code = COLVARS_OK;
if (cvm::debug()) {
cvm::log(cvm::line_marker);
cvm::log("Collective variables module, step no. "+
cvm::to_str(cvm::step_absolute())+"\n");
}
error_code |= calc_colvars();
error_code |= calc_biases();
error_code |= update_colvar_forces();
error_code |= analyze();
// write trajectory files, if needed
if (cv_traj_freq && cv_traj_name.size()) {
error_code |= write_traj_files();
}
// write restart files and similar data
if (restart_out_freq && (cvm::step_relative() > 0) &&
((cvm::step_absolute() % restart_out_freq) == 0)) {
if (restart_out_name.size()) {
// Write restart file, if different from main output
error_code |= write_restart_file(restart_out_name);
} else if (output_prefix().size()) {
error_code |= write_restart_file(output_prefix() + ".colvars.state");
}
if (output_prefix().size()) {
cvm::increase_depth();
for (std::vector<colvar *>::iterator cvi = colvars.begin(); cvi != colvars.end(); cvi++) {
// TODO remove this when corrFunc becomes a bias
error_code |= (*cvi)->write_output_files();
}
for (std::vector<colvarbias *>::iterator bi = biases.begin(); bi != biases.end(); bi++) {
error_code |= (*bi)->write_state_to_replicas();
}
cvm::decrease_depth();
}
}
// Write output files for biases, at the specified frequency for each
cvm::increase_depth();
for (std::vector<colvarbias *>::iterator bi = biases.begin();
bi != biases.end();
bi++) {
if ((*bi)->output_freq > 0) {
if ((cvm::step_relative() > 0) &&
((cvm::step_absolute() % (*bi)->output_freq) == 0) ) {
error_code |= (*bi)->write_output_files();
}
}