ContextImpl.cpp 21.5 KB
Newer Older
1
2
3
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
Evan Pretti's avatar
Evan Pretti committed
4
5
 * This is part of the OpenMM molecular simulation toolkit.                   *
 * See https://openmm.org/development.                                        *
6
 *                                                                            *
7
 * Portions copyright (c) 2008-2022 Stanford University and the Authors.      *
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software"), *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included in *
 * all copies or substantial portions of the Software.                        *
 *                                                                            *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
 * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
 * -------------------------------------------------------------------------- */

30
31
32
33
34
35
#include "openmm/Force.h"
#include "openmm/Integrator.h"
#include "openmm/OpenMMException.h"
#include "openmm/System.h"
#include "openmm/kernels.h"
#include "openmm/internal/ForceImpl.h"
36
#include "openmm/internal/ContextImpl.h"
37
#include "openmm/State.h"
38
#include "openmm/VirtualSite.h"
Peter Eastman's avatar
Peter Eastman committed
39
#include "openmm/Context.h"
40
#include <algorithm>
41
#include <cmath>
42
#include <cstdlib>
Peter Eastman's avatar
Peter Eastman committed
43
#include <iostream>
44
#include <map>
45
#include <utility>
46
#include <vector>
Robert McGibbon's avatar
Robert McGibbon committed
47
#include <string.h>
48
49

using namespace OpenMM;
Peter Eastman's avatar
Peter Eastman committed
50
using namespace std;
Robert McGibbon's avatar
Robert McGibbon committed
51
const static char CHECKPOINT_MAGIC_BYTES[] = "OpenMM Binary Checkpoint\n";
Robert McGibbon's avatar
Robert McGibbon committed
52

53

54
ContextImpl::ContextImpl(Context& owner, const System& system, Integrator& integrator, Platform* platform, const map<string, string>& properties, ContextImpl* originalContext) :
55
56
        owner(owner), system(system), integrator(integrator), hasInitializedForces(false), hasSetPositions(false), integratorIsDeleted(false),
        lastForceGroups(-1), platform(platform), platformData(NULL) {
57
58
    int numParticles = system.getNumParticles();
    if (numParticles == 0)
59
        throw OpenMMException("Cannot create a Context for a System with no particles");
60
61
62
    
    // Check for errors in virtual sites and massless particles.
    
63
64
65
    for (int i = 0; i < numParticles; i++)
        if (system.isVirtualSite(i) && system.getParticleMass(i) != 0.0)
            throw OpenMMException("Virtual site has nonzero mass");
66
    set<pair<int, int> > constraintAtoms;
67
68
69
70
    for (int i = 0; i < system.getNumConstraints(); i++) {
        int particle1, particle2;
        double distance;
        system.getConstraintParameters(i, particle1, particle2, distance);
71
72
73
74
        if (particle1 == particle2)
            throw OpenMMException("A constraint cannot connect a particle to itself");
        if (particle1 < 0 || particle2 < 0 || particle1 >= numParticles || particle2 >= numParticles)
            throw OpenMMException("Illegal particle index in constraint");
75
76
77
        double mass1 = system.getParticleMass(particle1);
        double mass2 = system.getParticleMass(particle2);
        if ((mass1 == 0.0 && mass2 != 0.0) || (mass2 == 0.0 && mass1 != 0.0))
78
            throw OpenMMException("A constraint cannot involve a massless particle");
79
80
81
82
        pair<int, int> atoms = make_pair(min(particle1, particle2), max(particle1, particle2));
        if (constraintAtoms.find(atoms) != constraintAtoms.end())
            throw OpenMMException("The System has two constraints between the same atoms.  This will produce a singular constraint matrix.");
        constraintAtoms.insert(atoms);
83
84
    }
    
85
86
    // Validate the list of properties.

87
    map<string, string> validatedProperties;
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    if (platform != NULL) {
        const vector<string>& platformProperties = platform->getPropertyNames();
        for (auto& prop : properties) {
            string property = prop.first;
            if (platform->deprecatedPropertyReplacements.find(property) != platform->deprecatedPropertyReplacements.end())
                property = platform->deprecatedPropertyReplacements[property];
            bool valid = false;
            for (auto& p : platformProperties)
                if (p == property) {
                    valid = true;
                    break;
                }
            if (!valid)
                throw OpenMMException("Illegal property name: "+prop.first);
            validatedProperties[property] = prop.second;
        }
104
    }
105
106
107
108
    else {
        // There can't be any platform-specific properties if there's no platform
    }

109
110
    // Find the list of kernels required.
    
111
    vector<string> kernelNames;
112
    kernelNames.push_back(CalcForcesAndEnergyKernel::Name());
113
    kernelNames.push_back(UpdateStateDataKernel::Name());
114
115
    kernelNames.push_back(ApplyConstraintsKernel::Name());
    kernelNames.push_back(VirtualSitesKernel::Name());
116
    for (int i = 0; i < system.getNumForces(); ++i) {
117
        forceImpls.push_back(system.getForce(i).createImpl());
118
119
120
        vector<string> forceKernels = forceImpls[forceImpls.size()-1]->getKernelNames();
        kernelNames.insert(kernelNames.begin(), forceKernels.begin(), forceKernels.end());
    }
121
    hasInitializedForces = true;
122
123
    vector<string> integratorKernels = integrator.getKernelNames();
    kernelNames.insert(kernelNames.begin(), integratorKernels.begin(), integratorKernels.end());
124
125
126
127
    
    // Select a platform to use.
    
    vector<pair<double, Platform*> > candidatePlatforms;
128
129
130
131
132
    if (platform == NULL) {
        char* defaultPlatform = getenv("OPENMM_DEFAULT_PLATFORM");
        if (defaultPlatform != NULL)
            platform = &Platform::getPlatformByName(string(defaultPlatform));
    }
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    if (platform == NULL) {
        for (int i = 0; i < Platform::getNumPlatforms(); i++) {
            Platform& p = Platform::getPlatform(i);
            if (p.supportsKernels(kernelNames))
                candidatePlatforms.push_back(make_pair(p.getSpeed(), &p));
        }
        if (candidatePlatforms.size() == 0)
            throw OpenMMException("No Platform supports all the requested kernels");
        sort(candidatePlatforms.begin(), candidatePlatforms.end());
    }
    else {
        if (!platform->supportsKernels(kernelNames))
            throw OpenMMException("Specified a Platform for a Context which does not support all required kernels");
        candidatePlatforms.push_back(make_pair(platform->getSpeed(), platform));
    }
    for (int i = candidatePlatforms.size()-1; i >= 0; i--) {
        try {
            this->platform = platform = candidatePlatforms[i].second;
151
152
153
154
            if (originalContext == NULL)
                platform->contextCreated(*this, validatedProperties);
            else
                platform->linkedContextCreated(*this, *originalContext);
155
156
157
158
159
160
161
162
            break;
        }
        catch (...) {
            if (i > 0)
                continue;
            throw;
        }
    }
163
164
165
}

void ContextImpl::initialize() {
166
167
    // Create and initialize kernels and other objects.
    
168
    initializeForcesKernel = platform->createKernel(CalcForcesAndEnergyKernel::Name(), *this);
169
    initializeForcesKernel.getAs<CalcForcesAndEnergyKernel>().initialize(system);
170
    updateStateDataKernel = platform->createKernel(UpdateStateDataKernel::Name(), *this);
171
    updateStateDataKernel.getAs<UpdateStateDataKernel>().initialize(system);
172
    applyConstraintsKernel = platform->createKernel(ApplyConstraintsKernel::Name(), *this);
173
    applyConstraintsKernel.getAs<ApplyConstraintsKernel>().initialize(system);
174
    virtualSitesKernel = platform->createKernel(VirtualSitesKernel::Name(), *this);
175
    virtualSitesKernel.getAs<VirtualSitesKernel>().initialize(system);
176
177
    Vec3 periodicBoxVectors[3];
    system.getDefaultPeriodicBoxVectors(periodicBoxVectors[0], periodicBoxVectors[1], periodicBoxVectors[2]);
178
    updateStateDataKernel.getAs<UpdateStateDataKernel>().setPeriodicBoxVectors(*this, periodicBoxVectors[0], periodicBoxVectors[1], periodicBoxVectors[2]);
179
    for (size_t i = 0; i < forceImpls.size(); ++i) {
180
        forceImpls[i]->initialize(*this);
181
        map<string, double> forceParameters = forceImpls[i]->getDefaultParameters();
182
183
184
        for (auto param : forceParameters)
            if (parameters.find(param.first) != parameters.end() && parameters[param.first] != forceParameters[param.first])
                throw OpenMMException("Two Forces define different default values for the parameter '"+param.first+"'");
185
186
        parameters.insert(forceParameters.begin(), forceParameters.end());
    }
187
    integrator.initialize(*this);
188
    updateStateDataKernel.getAs<UpdateStateDataKernel>().setVelocities(*this, vector<Vec3>(system.getNumParticles()));
189
190
}

191
ContextImpl::~ContextImpl() {
peastman's avatar
peastman committed
192
193
    for (auto force : forceImpls)
        delete force;
194
195
196
197
198
199
200
    
    // Make sure all kernels get properly deleted before contextDestroyed() is called.
    
    initializeForcesKernel = Kernel();
    updateStateDataKernel = Kernel();
    applyConstraintsKernel = Kernel();
    virtualSitesKernel = Kernel();
201
202
203
204
205
206
    if (!integratorIsDeleted) {
        // The Context is being deleted before the Integrator, so call cleanup() on it now.
        
        integrator.cleanup();
        integrator.context = NULL;
    }
207
    platform->contextDestroyed(*this);
208
209
}

210
double ContextImpl::getTime() const {
211
    return updateStateDataKernel.getAs<const UpdateStateDataKernel>().getTime(*this);
212
213
}

214
void ContextImpl::setTime(double t) {
215
    updateStateDataKernel.getAs<UpdateStateDataKernel>().setTime(*this, t);
216
217
}

218
219
220
221
222
223
224
225
long long ContextImpl::getStepCount() const {
    return updateStateDataKernel.getAs<const UpdateStateDataKernel>().getStepCount(*this);
}

void ContextImpl::setStepCount(long long count) {
    updateStateDataKernel.getAs<UpdateStateDataKernel>().setStepCount(*this, count);
}

Peter Eastman's avatar
Peter Eastman committed
226
227
void ContextImpl::getPositions(std::vector<Vec3>& positions, bool allowPeriodic) {
    updateStateDataKernel.getAs<UpdateStateDataKernel>().getPositions(*this, positions, allowPeriodic);
228
229
230
}

void ContextImpl::setPositions(const std::vector<Vec3>& positions) {
231
    hasSetPositions = true;
232
    updateStateDataKernel.getAs<UpdateStateDataKernel>().setPositions(*this, positions);
233
    integrator.stateChanged(State::Positions);
234
235
236
}

void ContextImpl::getVelocities(std::vector<Vec3>& velocities) {
237
    updateStateDataKernel.getAs<UpdateStateDataKernel>().getVelocities(*this, velocities);
238
239
240
}

void ContextImpl::setVelocities(const std::vector<Vec3>& velocities) {
241
    updateStateDataKernel.getAs<UpdateStateDataKernel>().setVelocities(*this, velocities);
242
    integrator.stateChanged(State::Velocities);
243
244
245
}

void ContextImpl::getForces(std::vector<Vec3>& forces) {
246
    updateStateDataKernel.getAs<UpdateStateDataKernel>().getForces(*this, forces);
247
248
}

249
250
251
252
const std::map<std::string, double>& ContextImpl::getParameters() const {
    return parameters;
}

253
double ContextImpl::getParameter(std::string name) {
254
    if (parameters.find(name) == parameters.end())
255
        throw OpenMMException("Called getParameter() with invalid parameter name: "+name);
256
257
258
    return parameters[name];
}

259
void ContextImpl::setParameter(std::string name, double value) {
260
    if (parameters.find(name) == parameters.end())
261
        throw OpenMMException("Called setParameter() with invalid parameter name: "+name);
262
    parameters[name] = value;
263
    integrator.stateChanged(State::Parameters);
264
265
}

266
267
268
269
void ContextImpl::getEnergyParameterDerivatives(std::map<std::string, double>& derivs) {
    updateStateDataKernel.getAs<UpdateStateDataKernel>().getEnergyParameterDerivatives(*this, derivs);
}

270
void ContextImpl::getPeriodicBoxVectors(Vec3& a, Vec3& b, Vec3& c) {
271
    updateStateDataKernel.getAs<UpdateStateDataKernel>().getPeriodicBoxVectors(*this, a, b, c);
272
273
274
275
276
}

void ContextImpl::setPeriodicBoxVectors(const Vec3& a, const Vec3& b, const Vec3& c) {
    if (a[1] != 0.0 || a[2] != 0.0)
        throw OpenMMException("First periodic box vector must be parallel to x.");
277
278
279
280
    if (b[2] != 0.0)
        throw OpenMMException("Second periodic box vector must be in the x-y plane.");
    if (a[0] <= 0.0 || b[1] <= 0.0 || c[2] <= 0.0 || a[0] < 2*fabs(b[0]) || a[0] < 2*fabs(c[0]) || b[1] < 2*fabs(c[1]))
        throw OpenMMException("Periodic box vectors must be in reduced form.");
281
    updateStateDataKernel.getAs<UpdateStateDataKernel>().setPeriodicBoxVectors(*this, a, b, c);
282
283
}

284
void ContextImpl::applyConstraints(double tol) {
285
286
    if (!hasSetPositions)
        throw OpenMMException("Particle positions have not been set");
287
    applyConstraintsKernel.getAs<ApplyConstraintsKernel>().apply(*this, tol);
288
289
}

290
void ContextImpl::applyVelocityConstraints(double tol) {
291
292
    if (!hasSetPositions)
        throw OpenMMException("Particle positions have not been set");
293
294
295
    applyConstraintsKernel.getAs<ApplyConstraintsKernel>().applyToVelocities(*this, tol);
}

296
void ContextImpl::computeVirtualSites() {
297
    virtualSitesKernel.getAs<VirtualSitesKernel>().computePositions(*this);
298
299
}

300
double ContextImpl::calcForcesAndEnergy(bool includeForces, bool includeEnergy, int groups) {
301
302
    if (!hasSetPositions)
        throw OpenMMException("Particle positions have not been set");
303
    lastForceGroups = groups;
304
    CalcForcesAndEnergyKernel& kernel = initializeForcesKernel.getAs<CalcForcesAndEnergyKernel>();
305
306
307
    while (true) {
        double energy = 0.0;
        kernel.beginComputation(*this, includeForces, includeEnergy, groups);
peastman's avatar
peastman committed
308
309
        for (auto force : forceImpls)
            energy += force->calcForcesAndEnergy(*this, includeForces, includeEnergy, groups);
310
311
312
313
314
        bool valid = true;
        energy += kernel.finishComputation(*this, includeForces, includeEnergy, groups, valid);
        if (valid)
            return energy;
    }
315
316
}

Peter Eastman's avatar
Peter Eastman committed
317
int& ContextImpl::getLastForceGroups() {
318
319
320
    return lastForceGroups;
}

321
double ContextImpl::calcKineticEnergy() {
322
    return integrator.computeKineticEnergy();
323
324
}

325
326
327
328
void ContextImpl::computeShiftedVelocities(double timeShift, std::vector<Vec3>& velocities) {
    updateStateDataKernel.getAs<UpdateStateDataKernel>().computeShiftedVelocities(*this, timeShift, velocities);
}

329
330
bool ContextImpl::updateContextState() {
    bool forcesInvalid = false;
peastman's avatar
peastman committed
331
    for (auto force : forceImpls)
332
333
        force->updateContextState(*this, forcesInvalid);
    return forcesInvalid;
334
335
}

336
337
338
339
const vector<ForceImpl*>& ContextImpl::getForceImpls() const {
    return forceImpls;
}

340
341
342
343
vector<ForceImpl*>& ContextImpl::getForceImpls() {
    return forceImpls;
}

344
void* ContextImpl::getPlatformData() {
345
346
347
    return platformData;
}

348
349
350
351
const void* ContextImpl::getPlatformData() const {
    return platformData;
}

352
void ContextImpl::setPlatformData(void* data) {
353
354
    platformData = data;
}
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370

const vector<vector<int> >& ContextImpl::getMolecules() const {
    if (!hasInitializedForces)
        throw OpenMMException("ContextImpl: getMolecules() cannot be called until all ForceImpls have been initialized");
    if (molecules.size() > 0 || system.getNumParticles() == 0)
        return molecules;

    // First make a list of bonds and constraints.

    vector<pair<int, int> > bonds;
    for (int i = 0; i < system.getNumConstraints(); i++) {
        int particle1, particle2;
        double distance;
        system.getConstraintParameters(i, particle1, particle2, distance);
        bonds.push_back(std::make_pair(particle1, particle2));
    }
peastman's avatar
peastman committed
371
372
    for (auto force : forceImpls) {
        vector<pair<int, int> > forceBonds = force->getBondedParticles();
373
374
        bonds.insert(bonds.end(), forceBonds.begin(), forceBonds.end());
    }
375
376
377
378
379
380
381
    for (int i = 0; i < system.getNumParticles(); i++) {
        if (system.isVirtualSite(i)) {
            const VirtualSite& site = system.getVirtualSite(i);
            for (int j = 0; j < site.getNumParticles(); j++)
                bonds.push_back(std::make_pair(i, site.getParticle(j)));
        }
    }
382
383
384
385
386

    // Make a list of every other particle to which each particle is connected

    int numParticles = system.getNumParticles();
    vector<vector<int> > particleBonds(numParticles);
peastman's avatar
peastman committed
387
388
389
    for (auto& bond : bonds) {
        particleBonds[bond.first].push_back(bond.second);
        particleBonds[bond.second].push_back(bond.first);
390
391
    }

392
    // Now identify particles by which molecule they belong to.
393

394
395
396
397
398
399
400
401
402
    molecules = findMolecules(numParticles, particleBonds);
    return molecules;
}

vector<vector<int> > ContextImpl::findMolecules(int numParticles, vector<vector<int> >& particleBonds) {
    // This is essentially a recursive algorithm, but it is reformulated as a loop to avoid
    // stack overflows.  It selects a particle, marks it as a new molecule, then recursively
    // marks every particle bonded to it as also being in that molecule.
    
403
404
405
    vector<int> particleMolecule(numParticles, -1);
    int numMolecules = 0;
    for (int i = 0; i < numParticles; i++)
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
        if (particleMolecule[i] == -1) {
            // Start a new molecule.
            
            vector<int> particleStack;
            vector<int> neighborStack;
            particleStack.push_back(i);
            neighborStack.push_back(0);
            int molecule = numMolecules++;
            
            // Recursively tag all the bonded particles.
            
            while (particleStack.size() > 0) {
                int particle = particleStack.back();
                particleMolecule[particle] = molecule;
                int& neighbor = neighborStack.back();
                while (neighbor < particleBonds[particle].size() && particleMolecule[particleBonds[particle][neighbor]] != -1)
                    neighbor++;
                if (neighbor < particleBonds[particle].size()) {
                    particleStack.push_back(particleBonds[particle][neighbor]);
                    neighborStack.push_back(0);
                }
                else {
                    particleStack.pop_back();
                    neighborStack.pop_back();
                }
            }
        }
    
    // Build the final output vector.
    
    vector<vector<int> > molecules(numMolecules);
437
438
439
440
441
    for (int i = 0; i < numParticles; i++)
        molecules[particleMolecule[i]].push_back(i);
    return molecules;
}

Peter Eastman's avatar
Peter Eastman committed
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
static void writeString(ostream& stream, string str) {
    int length = str.size();
    stream.write((char*) &length, sizeof(int));
    stream.write((char*) &str[0], length);
}

static string readString(istream& stream) {
    int length;
    stream.read((char*) &length, sizeof(int));
    string str(length, ' ');
    stream.read((char*) &str[0], length);
    return str;
}

void ContextImpl::createCheckpoint(ostream& stream) {
Robert McGibbon's avatar
Robert McGibbon committed
457
    stream.write(CHECKPOINT_MAGIC_BYTES, sizeof(CHECKPOINT_MAGIC_BYTES)/sizeof(CHECKPOINT_MAGIC_BYTES[0]));
Peter Eastman's avatar
Peter Eastman committed
458
459
460
461
462
    writeString(stream, getPlatform().getName());
    int numParticles = getSystem().getNumParticles();
    stream.write((char*) &numParticles, sizeof(int));
    int numParameters = parameters.size();
    stream.write((char*) &numParameters, sizeof(int));
peastman's avatar
peastman committed
463
464
465
    for (auto& param : parameters) {
        writeString(stream, param.first);
        stream.write((char*) &param.second, sizeof(double));
Peter Eastman's avatar
Peter Eastman committed
466
    }
467
    updateStateDataKernel.getAs<UpdateStateDataKernel>().createCheckpoint(*this, stream);
468
    integrator.createCheckpoint(stream);
Peter Eastman's avatar
Peter Eastman committed
469
470
471
472
    stream.flush();
}

void ContextImpl::loadCheckpoint(istream& stream) {
Robert McGibbon's avatar
Robert McGibbon committed
473
474
475
476
    static const int magiclength = sizeof(CHECKPOINT_MAGIC_BYTES)/sizeof(CHECKPOINT_MAGIC_BYTES[0]);
    char magicbytes[magiclength];
    stream.read(magicbytes, magiclength);
    if (memcmp(magicbytes, CHECKPOINT_MAGIC_BYTES, magiclength) != 0)
Robert McGibbon's avatar
Robert McGibbon committed
477
478
        throw OpenMMException("loadCheckpoint: Checkpoint header was not correct");

Peter Eastman's avatar
Peter Eastman committed
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
    string platformName = readString(stream);
    if (platformName != getPlatform().getName())
        throw OpenMMException("loadCheckpoint: Checkpoint was created with a different Platform: "+platformName);
    int numParticles;
    stream.read((char*) &numParticles, sizeof(int));
    if (numParticles != getSystem().getNumParticles())
        throw OpenMMException("loadCheckpoint: Checkpoint contains the wrong number of particles");
    int numParameters;
    stream.read((char*) &numParameters, sizeof(int));
    for (int i = 0; i < numParameters; i++) {
        string name = readString(stream);
        double value;
        stream.read((char*) &value, sizeof(double));
        parameters[name] = value;
    }
494
    updateStateDataKernel.getAs<UpdateStateDataKernel>().loadCheckpoint(*this, stream);
495
    integrator.loadCheckpoint(stream);
496
    hasSetPositions = true;
497
498
499
500
    integrator.stateChanged(State::Positions);
    integrator.stateChanged(State::Velocities);
    integrator.stateChanged(State::Parameters);
    integrator.stateChanged(State::Energy);
Peter Eastman's avatar
Peter Eastman committed
501
}
502
503
504
505

void ContextImpl::systemChanged() {
    integrator.stateChanged(State::Energy);
}
506
507
508
509

Context* ContextImpl::createLinkedContext(const System& system, Integrator& integrator) {
    return new Context(system, integrator, *this);
}