serial_example.cc

00001 #include <iostream>
00002 #include <stdexcept>
00003 #include <cmath>
00004 #include <limits>
00005 #include "dynamic/dynamic.h"
00006 using namespace std;
00007 const double pi = 3.14159265;
00008 const double g = 9.81; //ms-2
00012 const double weight_1 = 5; //kg
00013 const double weight_2 = 2; //kg
00014 const double rigidity_1 = 100; //Nm-1
00015 const double rigidity_2 = 15; //Nm-1
00016 const double damping_1 = 10; //kgs-1
00017 const double damping_2 = 1; //kgs-1
00018 const double start_deflection_1 = 0.0; //m
00019 const double start_deflection_2 = 0.0; //m
00020 const double start_speed_1 = 0.0; //ms-1
00021 const double start_speed_2 = 0.0; //ms-1
00022 
00023 const double delta = 0.5; //s (this isn't computing delta time but only delta for output)
00024 const double time_max = 50.0;//s
00028 const double amplitude = 20; //N
00029 const double frequency = 0.05; //Hz
00030 const double shift_phase = 0; //rad
00034 double Q1(dynamic::Object* p_object)
00035 {
00036         //return amplitude*sin(2*pi*frequency*p_object->time()+shift_phase)+weight_1*g;
00037         return weight_1 * g;
00038 }
00042 double Q2(dynamic::Object* p_object)
00043 {
00044         return weight_2 * g;
00045 }
00049 int main(int argC, char* argV[])
00050 {
00051         //Output header of table
00052         cout << endl;
00053         cout << "Dynamic library by Jakub Krasa, 2006,2007" << endl;
00054         cout << "example dynamic five masses with start deflection and speed." << endl << endl;
00055         cout << "\t\tMass 1 \t\t\t\tMass 2" << endl;
00056         cout << "Time\t\t" << "defl.\t" << "speed\t" << "accel.\t\t" << "defl.\t" << "speed\t" << "accel." << endl;
00057         cout.precision(3);
00058         cout << right << fixed;
00059         //computing
00060         try
00061         {
00062                 dynamic::mdo* p_mass_1 = new dynamic::mdo(weight_1,rigidity_1,damping_1,
00063                                 start_deflection_1,start_speed_1);
00064                 dynamic::mdo* p_mass_2 = new dynamic::mdo(weight_2,rigidity_2,damping_2,
00065                                 start_deflection_2,start_speed_2);
00066                 dynamic::Serial* p_system = new dynamic::Serial();
00067                 dynamic::mdo* p_mass_3 = new dynamic::mdo(weight_1,rigidity_1,damping_1,
00068                                 start_deflection_1,start_speed_1);
00069                 dynamic::mdo* p_mass_4 = new dynamic::mdo(weight_2,rigidity_2,damping_2,
00070                                 start_deflection_2,start_speed_2);
00071                 dynamic::mdo* p_mass_5 = new dynamic::mdo(weight_2,rigidity_2,damping_2,
00072                                 start_deflection_2,start_speed_2);
00073                 dynamic::Serial* p_sub_system = new dynamic::Serial();
00074                 p_mass_1->name.set("mass 1");
00075                 p_mass_2->name.set("mass 2");
00076                 p_mass_3->name.set("mass 3");
00077                 p_mass_4->name.set("mass 4");
00078                 p_mass_5->name.set("mass 5");
00079                 p_system->name.set("system");
00080                 p_sub_system->name.set("sub_system");
00081                 p_sub_system->push_front(p_mass_3);
00082                 p_sub_system->push_front(p_mass_4);
00083                 p_system->push_front(p_mass_5);
00084                 p_system->push_front(p_mass_2);
00085                 p_system->push_front(p_sub_system);
00086                 p_system->push_front(p_mass_1);
00087                 p_mass_1->load(Q1);
00088                 p_mass_2->load(Q2);
00089                 p_mass_3->load(Q2);
00090                 p_mass_4->load(Q2);
00091                 p_mass_5->load(Q2);
00092                 double time = 0;
00093                 do
00094                 {
00095                         time += delta;
00096                         p_system->time_to(time);
00097                         cout << p_system->time.get() << "\t\t"  
00098                                 << p_mass_1->defle() << "\t" << p_mass_1->speed() << "\t" << p_mass_1->accel()
00099                                 << "\t\t"       
00100                                 << p_mass_2->defle() << "\t" << p_mass_2->speed() << "\t" << p_mass_2->accel()
00101                                 << endl;
00102                 }
00103                 while((((time + delta/2)-time_max)/time_max) <= numeric_limits<double>::epsilon());
00104                 delete p_system;
00105                 delete p_mass_1;
00106                 delete p_mass_2;
00107                 delete p_mass_3;
00108                 delete p_mass_4;
00109                 delete p_mass_5;
00110                 delete p_sub_system;
00111         }
00112         catch (exception& e)
00113         {
00114                 cout << "sds exception: " << e.what() << endl;
00115                 return EXIT_FAILURE;
00116         }
00117         return EXIT_SUCCESS;
00118 }

Generated on Sun Mar 11 15:42:40 2007 for Dynamic library by  doxygen 1.4.7