-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosimperf-runner.cpp
More file actions
186 lines (154 loc) · 5.19 KB
/
osimperf-runner.cpp
File metadata and controls
186 lines (154 loc) · 5.19 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
#include <OpenSim/Analyses/StaticOptimization.h>
#include <OpenSim/OpenSim.h>
#include <OpenSim/Simulation/StatesTrajectory.h>
#include <OpenSim/Tools/CMCTool.h>
#include <cstdio>
#include <fstream>
#include <ostream>
#include <stdexcept>
#include <string>
#include <functional>
#include <chrono>
#include <ostream>
#include <sstream>
#include <string>
#include <chrono>
#include <ostream>
#include <string>
#include <cstring>
using namespace OpenSim;
using namespace SimTK;
using PerfClock = std::chrono::steady_clock;
namespace
{
PerfClock::duration runToolIK(
const std::string& modelFile,
const std::string& toolFile,
bool visualize)
{
Model model(modelFile);
if (visualize) { model.setUseVisualizer(true); }
OpenSim::InverseKinematicsTool tool(toolFile, false);
tool.setModel(model);
const auto begin = PerfClock::now();
tool.run();
return PerfClock::now() - begin;
}
PerfClock::duration runToolCMC(
const std::string& modelFile,
const std::string& toolFile,
bool visualize)
{
Model model(modelFile);
if (visualize) { model.setUseVisualizer(true); }
CMCTool tool(toolFile, false);
tool.setModel(model);
tool.setFinalTime(0.65);
const std::string label = model.getName() + "_CMC";
auto before = PerfClock::now();
tool.run();
return PerfClock::now() - before;
}
PerfClock::duration runToolFwd(
const std::string& modelFile,
const std::string& toolFile,
bool visualize)
{
std::cout << "modelFile = " << modelFile << ", toolFile = " << toolFile << "\n";
ForwardTool tool(toolFile, true, false);
Model model(modelFile);
if (visualize) { model.setUseVisualizer(true); }
tool.setModel(model);
const std::string label = model.getName() + "_Fwd";
const auto before = PerfClock::now();
tool.run();
return PerfClock::now() - before;
}
PerfClock::duration runAndTime(
const std::string& modelFile,
bool visualize,
SimTK::Real finalTime)
{
Model model(modelFile);
model.setUseVisualizer(visualize);
SimTK::State& s = model.initSystem();
s.setTime(0.);
model.realizeReport(s);
// The choice of integrator/parameters matches what OpenSim Creator and
// OpenSim GUI do by default.
SimTK::RungeKuttaMersonIntegrator integrator(model.getSystem());
integrator.setInternalStepLimit(20000);
integrator.setMinimumStepSize(1.0e-8 );
integrator.setMaximumStepSize(1.0);
integrator.setAccuracy(1.0e-5);
integrator.setFinalTime(finalTime);
// integrator.setReturnEveryInternalStep(true); // so that cancellations/interrupts work
integrator.initialize(s);
SimTK::TimeStepper stepper{model.getSystem(), integrator};
stepper.initialize(s);
// stepper.setReportAllSignificantStates(true); // so that cancellations/interrupts work
const auto before = PerfClock::now();
stepper.stepTo(finalTime);
const auto duration = PerfClock::now() - before;
if (integrator.getTime() < finalTime) {
throw std::runtime_error("did not reach final time");
}
return duration;
}
}
int main(int argc, char* argv[]) {
std::vector<std::string> args(argv + 1, argv + argc);
// Argument options.
std::string ik;
std::string cmc;
std::string fwd;
std::string model;
std::string out;
bool run = false;
SimTK::Real simTime = SimTK::NaN;
bool visualize = false;
// Parse the arguments.
for (auto it = args.begin(); it < args.end(); it++) {
if (*it == "--model") { model = *++it; }
if (*it == "--out") { out = *++it; }
if (*it == "--cmc") { cmc = *++it; }
if (*it == "--fwd") { fwd = *++it; }
if (*it == "--ik") { ik = *++it; }
run |= *it == "--run";
if (*it == "--run") { simTime = std::stod(*++it); }
visualize |= *it == "--viz";
}
// Check parsed arguments.
SimTK_ASSERT_ALWAYS(!model.empty(), "No model selected");
std::cout << "model = " << model << "\n";
std::cout << "cmc = " << cmc << "\n";
std::cout << "fwd = " << fwd << "\n";
std::cout << "run = " << run << "\n";
std::cout << "sim = " << simTime << "\n";
std::cout << "viz = " << visualize << "\n";
#ifdef OPENSIM_REGISTER_TYPES
RegisterTypes_osimExampleComponents();
RegisterTypes_osimSimulation();
#endif
PerfClock::duration dt{};
if (run) {
dt = runAndTime(model, visualize, simTime);
}
if (!cmc.empty()) {
dt = runToolCMC(model, cmc, visualize);
}
if (!fwd.empty()) {
dt = runToolFwd(model, fwd, visualize);
}
if (!ik.empty()) {
dt = runToolIK(model, ik, visualize);
}
const double dseconds = std::chrono::duration_cast<std::chrono::duration<double>>(dt).count();
std::cout << "RESULTS:\n";
std::cout << "--> dt = " << dseconds << "\n";
if (not out.empty()) {
std::ofstream fout(out);
fout << dseconds;
}
return 0;
}