Swarm-NG  1.1
tutorial_ensemble.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 2011 by Saleh Dindar and the Swarm-NG Development Team *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 3 of the License. *
7  * *
8  * This program is distributed in the hope that it will be useful, *
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11  * GNU General Public License for more details. *
12  * *
13  * You should have received a copy of the GNU General Public License *
14  * along with this program; if not, write to the *
15  * Free Software Foundation, Inc., *
16  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
17  ************************************************************************/
18 
24 // @page TutorialEnsemble Tutorial on using the Ensemble data structure.
25 // In this tutorial, we demonstrate how to generate an ensemble from
26 // a mathematical description.
27 //
28 // We will create an ensemble object, fill it with information of
29 // planetary systems that we synthesize. In the end we save the whole
30 // ensemble to the file. However, one can go ahead and do a number of
31 // arbitrary integrations on the ensemble object as demonstrated in
32 // @ref TutorialBeginner.
33 //
34 // We generate planetary systems where all the planets are on a parabola
35 // The star is placed at the origin. and the planets are on
36 // y = x^2/4/R - R.
37 //
38 // Let's get into the code.
39 // First some definitions and includes.
40 #include "swarm/swarm.h"
41 #include <iostream>
42 using namespace swarm;
43 using namespace std;
44 
45 // Here we define some properties to be constant, like :
46 //
47 // Number of bodies in a system
48 const int nbod = 3;
49 // Number of planetary systems
50 const int nsys = 64;
51 // Mass of the stars and the planets
52 const double mass_star = 1;
53 const double mass_planet = 0.0001;
54 // X component of the planet positions.
55 const double xpos[4] = { -2.0, +2.0, -4.0, +4.0 };
56 // mu = GM , but we have set both G and M to 1.
57 const double mu = 1;
58 // the minimum distance of the planets to the star
59 // on the parabolic orbit. or in other terms, the distance between
60 // the bottom of the parabola and the star (at origin).
61 const double R = 1;
62 //
63 // We create a convenience norm function
64 double norm(double x,double y){
65  return sqrt(x*x+y*y);
66 }
67 //
68 // Now we can start the main procedure.
69 //
70 int main(int argc, char* argv[]){
71 // our program take one ARGV parameter: the
72 // name of the output file.
73 if(argc <= 1){
74  cout << "Usage: parabolic_collision <outputfilename>" << endl;
75 }
76 const string outputfn = argv[1];
77 
78 // First we have to initialize swarm, we don't need any specific configurations
79 // since we are not performing any integrations.
80 init(config());
81 // Create an empty ensemble.
83 
84 // Now we have to populate the ensemble with data, we have
85 // to write two nesting for loops, one over systems
86 // and one over planets.
87 for(int i = 0; i < nsys ; i++){
88  ensemble::SystemRef s = ens[i];
89 
90  // Set the system parameters, the system must be set active, otherwise
91  // it will not be integrated.
92  s.id() = 0;
93  s.time() = 0;
94  s.set_active();
95 
96  // Place a stationary star at origin
97  s[0].mass() = mass_star;
98  s[0].x() = 0, s[0].y() = 0, s[0].z() = 0 ;
99  s[0].vx() = 0, s[0].vy() = 0, s[0].vz() = 0 ;
100 
101 
102  // Place the rest of the planets on the parabola
103  for(int b = 1; b < nbod; b++){
104  // x comes from the array (defined at the top). Y is calculated
105  // to be on the parabola.
106  double x = xpos[b-1], y = x*x/4/R - R ;
107  // Speed is calculated based on orbital parameters for a parabolic orbit.
108  double speed = sqrt(2*mu/norm(x,y)) ;
109  // The direction of velocity is calculated from the dy/dx but
110  // it also need to be normalized. It is also set to face the
111  // origin.
112  double velocity_direction_x = (x/abs(x))/ norm(1,x/2/R);
113  double velocity_direction_y = (x/2/R )/ norm(1,x/2/R);
114 
115  // Once the parameters are calculated, we can write them to
116  // the ensemble data structure.
117  s[b].mass() = mass_planet;
118  s[b].x() = x , s[b].vx() = -speed*velocity_direction_x ;
119  s[b].y() = y , s[b].vy() = -speed*velocity_direction_y ;
120  s[b].z() = 0 , s[b].vz() = 0 ;
121  }
122 
123  // End of the loop around systems.
124 }
125 // Now that the ensemble is created and filled, we save the results
126 // to the output file. Not that in real applications, we would want
127 // to integrate and examine the ensemble before writing it to a file
128 snapshot::save_text(ens,outputfn);
129 // On another note, we can also use snapshot::save function (with the same parameters)
130 // to write the ensemble to a binary file. Binary files are faster to read and
131 // write by C++ applications. The disadvantage is that they are not readable by
132 // Other applications. For manipulation of text and binary ensemble files
133 // see @ref SwarmExec.
134 }
135 
136 // This concludes the tutorial. For complete listing of the file see @ref src/tutorials/tutorial_ensemble.cpp