Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

RungeKutta4Solver.cpp

00001 /*************************************************************************** 00002 * This file is part of OpenCAL: Open Computer Animation Library * 00003 * I created OpenCAL as my master's thesis Computer Science (multimedia) * 00004 * at the tUL university in Diepenbeek, Belgium * 00005 * * 00006 * Copyright (C) 2003-2004 by Jeroen Dierckx * 00007 * jeroen.dierckx@student.luc.ac.be * 00008 * * 00009 ***************************************************************************/ 00010 00011 // Includes 00012 #include "RungeKutta4Solver.h" 00013 #include <OpenCAL/ODESource.h> 00014 using namespace OpenCAL; 00015 00016 using namespace std; 00017 00018 00019 /****************************** 00020 * Constructors and destructor * 00021 ******************************/ 00022 00023 RungeKutta4Solver::RungeKutta4Solver() 00024 { 00025 } 00026 00027 RungeKutta4Solver::~RungeKutta4Solver() 00028 { 00029 } 00030 00031 00032 /************************ 00033 * Get and set functions * 00034 ************************/ 00035 00036 00037 /****************** 00038 * Other functions * 00039 ******************/ 00040 00054 void RungeKutta4Solver::solve(float stepSize, ODESource *source) 00055 { 00062 const float oneThird = 1.0f / 3.0f; 00063 const float oneSixth = 1.0f / 6.0f; 00064 00065 source->fillState(); 00066 source->fillDerivative(); 00067 00068 PhaseSpace &state = *(source->getState()); 00069 PhaseSpace &deriv = *(source->getDerivative()); 00070 00071 // Copy the original state 00072 PhaseSpace x0 = state; 00073 00074 00075 // Calculate k1 00076 PhaseSpace k1 = stepSize * deriv; 00077 00078 // Calculate k2 00079 state.add(k1, 0.5f); 00080 source->fromState(); 00081 source->fillDerivative(); 00082 PhaseSpace k2 = stepSize * deriv; 00083 00084 // Calculate k3 00085 state = x0; 00086 state.add(k2, 0.5f); 00087 source->fromState(); 00088 source->fillDerivative(); 00089 PhaseSpace k3 = stepSize * deriv; 00090 00091 // Calculate k4 00092 state = x0; 00093 state += k3; 00094 source->fromState(); 00095 source->fillDerivative(); 00096 PhaseSpace k4 = stepSize * deriv; 00097 00098 00099 // Now calculate the new state 00100 00101 // This works, but requires 9 copies of a phase space 00102 //state = x0 + oneSixth * k1 + oneThird * k2 + oneThird * k3 + oneSixth * k4; 00103 00104 // This requires only one copy 00105 state = x0; 00106 state.add(k1, oneSixth); 00107 state.add(k2, oneThird); 00108 state.add(k3, oneThird); 00109 state.add(k4, oneSixth); 00110 00111 00112 source->fromState(); 00113 }

Generated on Sun Aug 15 19:19:23 2004 for OpenCAL: Open Computer Animation Library by doxygen 1.3.8