File: RemoveMassCenterDrift.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 (207 lines) | stat: -rw-r--r-- 7,527 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
#include "Molmodel.h"

#include <iostream>
#include <exception>
#include <fstream>
#include <ctime>
#include <cmath>

#define SIMDURATION 1000        // ps

using namespace SimTK;
using std::cout; using std::endl;

// See below.
static std::clock_t start = 0; 

// This is a reporter so we can get some output during the simulation.
// Watch the COM velocity.
class SaySomething : public PeriodicEventReporter {
public:
	SaySomething(const CompoundSystem& system, const VelocityRescalingThermostat& rescaler, Real reportInterval) 
        :   PeriodicEventReporter(reportInterval), system(system), rescaler(rescaler) {}

    void handleEvent(const State& state) const {
		const SimbodyMatterSubsystem& matter = system.getMatterSubsystem();

        // This realize() is here to make sure we can reference 
        // velocity-related quantities.
        system.realize(state, Stage::Velocity);
        const Real T  = rescaler.calcCurrentTemperature(state);
        const Vec3 com = system.calcSystemMassCenterLocation(state);
        const Vec3 Vcom = matter.calcSystemMassCenterVelocityInGround(state);

		std::cout << "TIME = " << state.getTime() << " TEMP=" << T
                  << " Elapsed(s)=" << double(std::clock()-start)/CLOCKS_PER_SEC
                  << std::endl;
        std::cout << " COM pos=" << com << " |vel|=" << Vcom.norm() << endl;
    }
private:
    const CompoundSystem&               system;
    const VelocityRescalingThermostat&  rescaler;
};

class SaveState : public PeriodicEventReporter {
public:
	SaveState(Real reportInterval) 
    :   PeriodicEventReporter(reportInterval) 
    {saved.reserve(5000);}

    void clear() {saved.clear(); saved.reserve(5000);}

    void handleEvent(const State& state) const {
		saved.push_back(state);
    }

    const std::vector<State>& getStates() const {return saved;}
private:
    mutable std::vector<State> saved;
};

// Where we want the molecule's COM to stay.
static Vec3 WantCOM(1,2,3);
int main() { 
	try {
		// Load the PDB file and construct the system ... and specify the forcefield
		CompoundSystem system;
		SimbodyMatterSubsystem matter(system);
        GeneralForceSubsystem forces(system);
		DecorationSubsystem decorations(system);
		DuMMForceFieldSubsystem forceField(system);
		forceField.loadAmber99Parameters();
        //forceField.setAllGlobalScaleFactors(0);
		//PDBReader pdb("20ala.min.pdb");
		//pdb.createCompounds(system);
        //Protein pdb("AEEEA");
        //pdb.setTopLevelTransform (Transform(Rotation(Pi/3, Vec3(1,2,3)), Vec3(10,5,6)));
        //system.adoptCompound(pdb);
        //Protein pdb2("AAAAA");
        //pdb2.setTopLevelTransform(Transform(Rotation(Pi/6, Vec3(.1,2,.3)), Vec3(8,5,6)));
        //system.adoptCompound(pdb2);
        //Protein pdb3("AAAAA");
        //pdb3.setTopLevelTransform(Transform(Rotation(Pi/4, Vec3(1,.2,3)), Vec3(8,6,6)));
        //system.adoptCompound(pdb3);
        Protein pdb4("KKK");
        pdb4.setTopLevelTransform(Transform(Rotation(Pi/5, Vec3(1,2,.03)), Vec3(10,4,6)));
        system.adoptCompound(pdb4);
		system.modelCompounds();

        matter.updGround().addBodyDecoration(WantCOM, DecorativeFrame().setColor(Red));

        //forceField.setUseOpenMMAcceleration(true);
        //forceField.setTraceOpenMM(true);

	    const Real temp = 300;

        MassCenterMotionRemover& remover = 
            *new MassCenterMotionRemover(system, 10);
 		system.addEventHandler(&remover);
        remover.setDesiredMassCenterLocation(WantCOM);
        remover.setMassCenterLocationTolerance(1e-3);
        //remover.disableAngularMomentumRemoval(true);
        remover.enableMassCenterCorrection(true);

   	
        VelocityRescalingThermostat& rescaler = 
            *new VelocityRescalingThermostat(system, temp, 0.1, 6);
		system.addEventHandler(&rescaler);

        //NoseHooverThermostat noseHoover(forces,matter, temp, 1, 6);

        // Show me a movie
        Visualizer viz(system);
        system.addEventReporter( new Visualizer::Reporter(viz, 0.050) );

        SaveState* saveState = new SaveState(.05);
        //system.addEventReporter(saveState);

        system.addEventReporter(new SaySomething(system,rescaler,1.));
		
		// write output to pdb
		std::ofstream pdbfile;
        pdbfile.open("c:/temp/output.pdb");
		//system.addEventReporter(new PeriodicPdbWriter(system, pdbfile, 0.1)); 
		system.realizeTopology();
		
		// Create an initial state for the simulation.
		State state = system.getDefaultState();
		//pdb.createState(system, state);
		LocalEnergyMinimizer::minimizeEnergy(system, state, 15.0);

        system.realize(state, Stage::Velocity);

        cout << "Num thermal dofs=" << rescaler.calcNumThermalDofs(state)
             << " after excluding " << rescaler.getNumExcludedDofs() 
             << " rigid body dofs\n";

        cout << "BEFORE u=" << state.getU()
             << "\nTemp=" << rescaler.calcCurrentTemperature(state) 
             << " |mom|=" << matter.calcSystemCentralMomentum(state).norm() << endl;

        rescaler.rescale(state); // randomizes initially
        system.realize(state, Stage::Velocity);

        cout << "RESCALED u=" << state.getU() 
             << "\nTemp=" << rescaler.calcCurrentTemperature(state) 
             << " |mom|=" << matter.calcSystemCentralMomentum(state).norm() << endl;

        SpatialVec sysCMOM = matter.calcSystemCentralMomentum(state);
        cout << "Central mom=" << sysCMOM << endl;

        // Kill any rigid body linear or angular momentum.
        remover.removeSystemMomentum(state);
        system.realize(state, Stage::Velocity);

        sysCMOM = matter.calcSystemCentralMomentum(state);
        cout << "Now central mom=" << sysCMOM << endl;

        cout << "NO MOMENTUM u=" << state.getU()
             << "\nTemp=" << rescaler.calcCurrentTemperature(state) 
             << " |mom|=" << matter.calcSystemCentralMomentum(state).norm() << endl;

        rescaler.rescale(state);
        system.realize(state, Stage::Velocity);

        cout << "FINAL RESCALED u=" << state.getU()
             << "\nTemp=" << rescaler.calcCurrentTemperature(state) 
             << " |mom|=" << matter.calcSystemCentralMomentum(state).norm() << endl;


		
        start = std::clock();   // start the wallclock timer

		// Choose an integrator.

        // Note: Verlet at low accuracy is a very poor integrator.
        // You get about the same amount of drift with RK4 at 1e-2
        // accuracy as Verlet at 1e-4. But we want lots of drift here to
        // show the COM mover working!

		VerletIntegrator integ(system); integ.setAccuracy(1e-1);
        //RungeKuttaMersonIntegrator integ(system); integ.setAccuracy(1e-2);

        // Simulate.
		TimeStepper ts(system, integ);
		ts.initialize(state);
		ts.stepTo(SIMDURATION);

        std::cout << "Done. " << SIMDURATION << " ps in elapsed(s)=" 
                  << double(std::clock()-start)/CLOCKS_PER_SEC
                  << endl;
		pdbfile.close();

        for (unsigned frame=0; frame < saveState->getStates().size(); ++frame)
            viz.report(saveState->getStates()[frame]);
		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;
	}
}