Repository navigation
Expand file tree
/
Copy pathActionList.cpp
More file actions
180 lines (169 loc) · 6.37 KB
/
Copy pathActionList.cpp
File metadata and controls
180 lines (169 loc) · 6.37 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
// ActionList
#include "ActionList.h"
#include "CpptrajStdio.h"
// CONSTRUCTOR
ActionList::ActionList() : debug_(0), actionsAreSilent_(false) {}
// DESTRUCTOR
ActionList::~ActionList() { Clear(); }
void ActionList::Clear() {
// No need to cast back to whatever action was allocd since Action destructor is virtual
for (Aarray::const_iterator act = actionList_.begin(); act != actionList_.end(); ++act)
delete act->ptr_;
actionList_.clear();
}
// ActionList::AddAction()
int ActionList::AddAction(Action* actIn, ArgList& argIn, ActionInit& init)
{
if (actIn == 0) {
mprinterr("Internal Error: AddAction() called with null Action.\n");
return 1;
}
int err = 0;
if (actionsAreSilent_) SetWorldSilent( true );
ActHolder act;
act.ptr_ = actIn;
act.args_ = argIn;
// Attempt to initialize action
if ( act.ptr_->Init( argIn, init, debug_ ) != Action::OK ) {
mprinterr("Error: Could not initialize action [%s]\n", argIn.Command());
delete act.ptr_;
err = 1;
} else {
act.status_ = INIT;
actionList_.push_back( act );
if (argIn.CheckForMoreArgs()) err = 1;
}
if (actionsAreSilent_) SetWorldSilent( false );
return err;
}
// ActionList::SetupActions()
/** Attempt to set up all actions in the action list with the given parm
* If an action cannot be set up skip it.
*/
int ActionList::SetupActions(ActionSetup& setup, bool exitOnError) {
if (actionList_.empty()) return 0;
ActionSetup OriginalSetup = setup;
mprintf(".....................................................\n");
mprintf("ACTION SETUP FOR PARM '%s' (%zu actions):\n", setup.Top().c_str(), actionList_.size());
if (actionsAreSilent_) SetWorldSilent( true );
for (Aarray::iterator act = actionList_.begin(); act != actionList_.end(); ++act)
{
// Only attempt to set up action if active
if (act->status_ != INACTIVE) {
mprintf(" %li: [%s]\n", act - actionList_.begin(), act->args_.ArgLine());
act->status_ = SETUP;
Action::RetType err = act->ptr_->Setup(setup);
if (err == Action::ERR) {
mprinterr("Error: Setup failed for [%s]\n", act->args_.Command());
if (exitOnError) return 1;
act->status_ = INIT;
} else if (err == Action::SKIP) {
mprintf("Warning: Setup incomplete for [%s]: Skipping\n", act->args_.Command());
// Reset action status to INIT (pre-setup)
act->status_ = INIT;
} else if (err == Action::USE_ORIGINAL_FRAME) {
setup = OriginalSetup;
}
}
}
if (actionsAreSilent_) SetWorldSilent( false );
return 0;
}
// ActionList::DoActions()
/** Perform actions in the action list on the given Frame. Skip actions not
* initialized or not setup.
* \param frameNumIn The current frame number.
* \param frm Contains current Frame.
* \return true if coordinate output should be suppressed, false if coordinate
* output should be performed.
*/
bool ActionList::DoActions(int frameNumIn, ActionFrame& frm) {
ActionFrame OriginalFrame = frm;
//fprintf(stdout,"DEBUG: Performing %i actions on frame %i.\n",Naction,frameNumIn);
for (Aarray::iterator act = actionList_.begin(); act != actionList_.end(); ++act)
{
// Only do actions which were properly set up
if (act->status_ == SETUP) {
// Perform action on frame
Action::RetType err = act->ptr_->DoAction(frameNumIn, frm);
// Check for action special conditions/errors
if (err == Action::USE_ORIGINAL_FRAME) {
// Return to original frame
frm = OriginalFrame;
} else if (err == Action::SUPPRESS_COORD_OUTPUT) {
// Skip the rest of the actions and suppress output. Necessary when
// e.g. performing a running average over coords.
return true;
} else if (err == Action::ERR) {
// If here return type is ACTION_ERR.
// Treat actions that fail as if they could not be set up
mprintf("Warning: Action [%s] failed, frame %i.\n", act->args_.Command(), frameNumIn);
act->status_ = INIT;
}
}
}
return false;
}
// ActionList::Print()
void ActionList::PrintActions() {
for (Aarray::const_iterator act = actionList_.begin(); act != actionList_.end(); ++act)
{ // Skip deactivated actions
if (act->status_ != INACTIVE)
act->ptr_->Print();
}
}
#ifdef MPI
int ActionList::NumPreviousFramesReqd() const {
int nrequired = 0;
for (Aarray::const_iterator act = actionList_.begin(); act != actionList_.end(); ++act)
{ // Skip deactivated actions
if (act->status_ != INACTIVE)
nrequired = std::max( nrequired, act->ptr_->ParallelPreviousFramesRequired() );
}
if (debug_ > 0) rprintf("DEBUG: Action(s) require %i previous frames.\n", nrequired);
return nrequired;
}
/** Should not be called by master. */
int ActionList::ParallelProcessPreload(Action::FArray const& preload_frames) {
int err = 0;
for (Aarray::iterator act = actionList_.begin(); act != actionList_.end(); ++act)
{ // Skip deactivated actions
if (act->status_ != INACTIVE) {
//rprintf("DEBUG: Calling ParallelPreloadFrames() for '%s'\n", act->args_.Command());
if (act->ptr_->ParallelPreloadFrames( preload_frames )) {
rprintf("Warning: Parallel Preload failed for Action '%s'\n", act->args_.Command());
act->status_ = INACTIVE;
err++;
}
}
}
return err;
}
void ActionList::SyncActions() {
for (Aarray::const_iterator act = actionList_.begin(); act != actionList_.end(); ++act)
{ // Skip deactivated actions
if (act->status_ != INACTIVE) {
//rprintf("DEBUG: Calling SyncAction() for '%s'\n", act->args_.Command());
if (act->ptr_->SyncAction())
rprintf("Warning: Sync failed for Action '%s'\n", act->args_.Command());
}
}
}
void ActionList::ParallelPostCalcs() {
for (Aarray::const_iterator act = actionList_.begin(); act != actionList_.end(); ++act)
{ // Skip deactivated actions
if (act->status_ != INACTIVE) {
//rprintf("DEBUG: Calling ParallelPostCalc() for '%s'\n", act->args_.Command());
if (act->ptr_->ParallelPostCalc())
rprintf("Warning: ParallelPostCalc failed for Action '%s'\n", act->args_.Command());
}
}
}
#endif /* MPI */
void ActionList::List() const {
if (!actionList_.empty()) {
mprintf("\nACTIONS (%zu total):\n", actionList_.size());
for (Aarray::const_iterator act = actionList_.begin(); act != actionList_.end(); ++act)
mprintf(" %li: [%s]\n", act - actionList_.begin(), act->args_.ArgLine());
}
}