File: ExampleMeasureActualStepSize.cpp

package info (click to toggle)
molmodel 3.1.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,384 kB
  • sloc: cpp: 39,830; perl: 526; ansic: 107; makefile: 41
file content (250 lines) | stat: -rwxr-xr-x 7,677 bytes parent folder | download | duplicates (4)
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
#include "SimTKmolmodel.h"
#include "SimTKsimbody_aux.h"

#include <fstream>

#include <iostream>
#include <exception>

using namespace SimTK;
using namespace std;


class WritePdbReporter : public PeriodicEventReporter {
public:
    WritePdbReporter(
        const MultibodySystem& system, 
        std::ostream& outputStream,
        Real interval) 
        : PeriodicEventReporter(interval), 
          system(system), 
          outputStream(outputStream),
		  currentModelNumber(1)
    {}

	void addCompound(const Compound& compound) {
		compoundPointers.push_back(&compound);
	}

    void handleEvent(const State& state) const {
		outputStream << "MODEL " << currentModelNumber << endl;
        system.realize(state, Stage::Position);
		for (int c = 0; c < (int)compoundPointers.size(); ++c)
			compoundPointers[c]->writePdb(state, outputStream);
		outputStream << "ENDMDL" << endl;

		++currentModelNumber;
    }

private:
	mutable int currentModelNumber;
    const MultibodySystem& system;
	std::vector<const Compound*> compoundPointers;
    std::ostream& outputStream;
};


void integrate(Real accuracy, BondMobility::Mobility mobility, int integratorIndex, std::ostream& os)
{
    cout << "accuracy = " << accuracy << endl;
	cout << "integrator index = " << integratorIndex << endl;
	cout << "mobility index = " << mobility << endl;
    
    cout << "constructing system..." << endl;
    
    CompoundSystem system; // molecule-specialized simbody System
    SimbodyMatterSubsystem matter(system);
    DecorationSubsystem decorations(system);
    TinkerDuMMForceFieldSubsystem dumm(system); // molecular force field
    
    // Two very small proteins
    Protein protein1("SPFAK");
    Protein protein2("SPFAK");
    
	protein1.setPdbChainId('A');
	protein2.setPdbChainId('B');

    // link proteins to forcefields
    dumm.loadAmber99Parameters();
    protein1.assignBiotypes();
    protein2.assignBiotypes();
    
    // Set mobility of all bonds in both structures
	// For "Torsion" mobility, leave all bonds at default values
	if (mobility != BondMobility::Torsion) {
		for (Compound::BondIndex b(0); b < protein1.getNBonds(); ++b) {
			protein1.setBondMobility(mobility, b);
		}
		for (Compound::BondIndex b(0); b < protein2.getNBonds(); ++b) {
			protein2.setBondMobility(mobility, b);
		}
	}
    
    system.adoptCompound(protein1);
    system.adoptCompound(protein2, Vec3(0.5, 0.5, 0) );
    
    // system.updDefaultSubsystem().addEventReporter(new VTKEventReporter(system, 0.010));

	ofstream pdbStream("trajectory.pdb");
	WritePdbReporter writePdbReporter(system, pdbStream, 0.010);
	writePdbReporter.addCompound(protein1);
	writePdbReporter.addCompound(protein2);
	system.updDefaultSubsystem().addEventReporter(&writePdbReporter);
    
    system.updDefaultSubsystem().addEventHandler(new VelocityRescalingThermostat(system, 293.15, 0.020));
    
    system.modelCompounds(); // finalize multibody system
    
    system.realizeTopology();
    
    State& state = system.updDefaultState();
    
    
    cout << "minimizing energy..." << endl;
        
    LocalEnergyMinimizer::minimizeEnergy(system, state, 100.0);
    
    // Choice of integrators
    Integrator* integratorPtr;
    switch (integratorIndex) {
            case 0:
                integratorPtr = new RungeKuttaMersonIntegrator(system);
                break;
            case 1:
                integratorPtr = new VerletIntegrator(system);
                break;
            case 2:
                integratorPtr = new CPodesIntegrator(system);
                break;
            case 3:
                integratorPtr = new ExplicitEulerIntegrator(system);
                break;
    }
 
    Integrator& study = *integratorPtr;

    study.setAccuracy(accuracy);
    TimeStepper timeStepper(system, study);

    timeStepper.initialize(state);

    // cause timeStepper to pause after each integration step
    timeStepper.setReportAllSignificantStates(true);
    study.setReturnEveryInternalStep(true);
    
    cout << "integrating..." << endl;
    
	long int previousForceEvaluationCount = dumm.getForceEvaluationCount();

    std::vector<Real> integratorStepSizes;
    Real endTime = 5.000; // picoseconds
    // Real endTime = 0.050; // picoseconds - fast time for debugging
    while(timeStepper.getTime() < endTime)
    {
        timeStepper.stepTo(endTime);
    
        // store integrator step size
        integratorStepSizes.push_back(study.getPreviousStepSizeTaken());
    }
       
    cout << "analyzing statistics..." << endl;

	// Dump time steps
	//ofstream stepsOut ("timeSteps.dat");
 //   for (int t = 0; t < (int)integratorStepSizes.size(); ++t) {
	//	stepsOut << integratorStepSizes[t] << endl;
	//}	
	//stepsOut.close();

    // Mean step time
    Real meanTimeStep = 0.0;
    for (int t = 0; t < (int)integratorStepSizes.size(); ++t) {
        meanTimeStep += integratorStepSizes[t];
    }
    meanTimeStep /= integratorStepSizes.size();
    
    // Standard deviation of step time
    Real timeStepDev = 0.0;
    for (int t = 0; t < (int)integratorStepSizes.size(); ++t) {
        Real diff = meanTimeStep - integratorStepSizes[t];
        timeStepDev += diff * diff;
    }
    timeStepDev /= (integratorStepSizes.size() - 1);
    timeStepDev = sqrt(timeStepDev);
    
    // Median
    std::sort(integratorStepSizes.begin(), integratorStepSizes.end());
    Real medianTimeStep = integratorStepSizes[(int)(integratorStepSizes.size() / 2)];
    
    os << study.getMethodName();
    os << "\t";
    
    switch (mobility) {
        case BondMobility::Free: os << "All atom"; break;
        case BondMobility::Torsion: os << "Torsion"; break;
        case BondMobility::Rigid: os << "Rigid"; break;
    }
    os << "\t";
    
    os << accuracy;
    os << "\t";
    os << dumm.getForceEvaluationCount() - previousForceEvaluationCount;
    os << "\t";
    os << integratorStepSizes.size();
    os << "\t";
    os << 1000.0 * meanTimeStep;
    os << "\t";
    os << "" << 1000.0 * timeStepDev;
    os << "\t";
    os << 1000.0 * medianTimeStep;
    os << std::endl;
    
    delete integratorPtr;
    
    cout << "Done." << endl;
}

int main() { try 
{
    ofstream os("integ.dat");

    // Vary accuracy to measure effect on integrator step size
    Real accuracyList[] = {1e-2, 3e-3, 1e-3, 3e-4, 1e-4, 3e-5, 1e-5, 3e-6, 1e-6};
    // Real accuracyList[] = {3e-3, 1e-3, 3e-4}; // short list
    
    // Compare different modeling strategies
	BondMobility::Mobility mobilityList[] = {BondMobility::Free, BondMobility::Torsion, BondMobility::Rigid};

    // Loop over integrators, accuracies, and mobilities
    for (int integratorIndex = 0; integratorIndex <= 1; ++integratorIndex) 
    {
		// skip CPODES, it's way too slow for molecules, taking >60 force evaluations per step
		if (integratorIndex == 2) continue;

        for (int mIx = 1; mIx <= 1; ++mIx)
        {
            BondMobility::Mobility mobility = mobilityList[mIx];
            for (int aIx = 0; aIx <= 8; ++aIx)
            {
                integrate(accuracyList[aIx], mobility, integratorIndex, os);
                // integrate(3e-3, mobility, integratorIndex, os);
            }
        }
    }

    os.close();

    return 0;

} 
catch(const std::exception& e) {
    std::cerr << "ERROR: " << e.what() << std::endl;
    return 1;
}
catch(...) {
    std::cerr << "ERROR: An unknown exception was raised" << std::endl;
    return 1;
}

}