merge successful with staging
[biocity.git] / src / main.cpp
bloba67ad0ca2d84639c32152047d24cb935a04745f2
1 /* Copyright 2008 Jason Kim Chong Polak
3 Please respect the following:
5 This file is part of bioCity. bioCity is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <iostream>
20 #include <fstream>
21 #include <iomanip>
22 #include <string>
23 #include <sstream>
24 #include <list>
26 #include "World.hh"
27 #include "ConfReader.hh"
29 #include <g2.h>
30 #include <g2_X11.h>
31 #include <g2_PS.h>
32 using namespace std;
34 int translate_data( string, string[], int );
35 void print_default( string );
37 int main()
39 cout<<"#bioCity: Welcome to bioCity Matrix 0.4 (staging branch)"<<endl;
40 cout<<"#bioCity: The current byte-size of the world class is "<<sizeof(World)<<endl;
41 int k0;
43 * This part initializes environment variables
44 * and sets defaults if the configuration file
45 * cannot be read.
47 //START Environment variables
48 int psize = 4;
49 double dim_x = 500, dim_y = 500;
50 int time = 100;
51 int graphics = 1;
52 int scenario_num = 0;
53 string scenario="scen_singl_pop";
55 //array of scenario functions defined in World.cpp
56 typedef void (World::*ptrUpdate)( );
57 ptrUpdate update_func[][2] =
59 { &World::scen_single_pop, &World::scen_single_pop_init}
61 string update_func_names[] =
63 "scen_single_pop"
65 int update_func_len = 1;
67 //END environment variables
69 ConfReader conf( "bioc.conf" );
71 string currline;
72 if ( !conf.success() )
74 cout<<"#Configuration file not found. Using defaults.";
76 else
78 if (!conf.field_int( "population", &psize) )
79 print_default( "population" );
80 if (!conf.field_double( "dimx", &dim_x) )
81 print_default("dimx");
82 if (!conf.field_double( "dimy", &dim_y) )
83 print_default("dimy");
84 if (!conf.field_int( "time", &time ) )
85 print_default("time");
86 if (!conf.field_int( "graphics", &graphics) )
87 print_default("graphics");
88 if (!conf.field_string( "scenario", &scenario) )
89 print_default( "scenario" );
91 int spacing = 21;
92 cout<<"#bioCity: Current World Parameters"<<endl;
93 cout<<setw(spacing)<<std::left<<"#Population Size:"<<std::right<<psize<<endl;
94 cout<<setw(spacing)<<std::left<<"#Dimension X:"<<std::right<<dim_x<<endl;
95 cout<<setw(spacing)<<std::left<<"#Dimension Y:"<<std::right<<dim_y<<endl;
96 cout<<setw(spacing)<<std::left<<"#Time:"<<std::right<<time<<endl;
97 cout<<setw(spacing)<<std::left<<"#Graphics:"<<std::right<<graphics<<endl;
98 cout<<setw(spacing)<<std::left<<"#Scenario:"<<std::right<<scenario<<endl;
101 scenario_num = translate_data( scenario, update_func_names, update_func_len) - 1;
104 * This is where the magic happens :)
106 int i = 0;
107 World W1( psize, dim_x, dim_y);
108 (W1.*update_func[scenario_num][1])();
110 //Let the main know about all the functions
111 //that can be use so that the user can
112 //select them.
114 int id,id0;
115 if (graphics)
117 id = g2_open_vd();
118 id0 = g2_open_X11( (int) dim_x , (int) dim_y );
119 //int id1 = g2_open_EPSF_CLIP( "locationd.eps", (int) dim_x, (int) dim_y );
120 g2_attach(id, id0);
121 //g2_attach(id, id1);
122 W1.print_graph( id );
124 // g2_detach(id, id1);
125 cout<<"Timestep\tPopulation\t First\t Second"<<endl;
126 int k;
127 cout<<i<<"\t";
128 W1.print_stats();
129 for ( i = 0; i < time; i++ )
131 (W1.*update_func[scenario_num][0])();
132 W1.move_next();
133 if (i%20 == 0 && i > 0)
135 cout<<i<<"\t";
136 W1.print_stats( );
137 if (graphics) { W1.print_graph( id ); }
139 /* if (i == 500)
141 g2_attach( id, id1);
142 W1.print_graph( id );
143 g2_detach( id, id1);
146 if (graphics) { W1.print_graph( id ); }
147 cout<<i<<"\t";
148 W1.print_stats( );
149 //W1.complete_stats( );
150 cout<<"#The world has ended! Input anything to quit."<<endl;
151 //cin>>k0;
152 return 0;
155 int translate_data(string varName, string database[], int length)
157 int numOptions = length;
159 int loc = -1;
160 for (int i = 0; i < numOptions; i++)
162 if (database[i] == varName)
164 loc = i+1;
167 return loc;
170 void print_default(string field)
172 cout<<"# WARNING: \""<<field<<"\" not found, using default!"<<endl;