Log In | Get Help   
Home My Page Projects Code Snippets Project Openings Mareframe
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files
[mareframe] Annotation of /trunk/gadget/ecosystem.cc
[mareframe] / trunk / gadget / ecosystem.cc Repository:
ViewVC logotype

Annotation of /trunk/gadget/ecosystem.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "ecosystem.h"
2 :     #include "runid.h"
3 :     #include "global.h"
4 :    
5 :     Ecosystem::Ecosystem(const MainInfo& main) : printinfo(main.getPI()) {
6 :    
7 :     funceval = 0;
8 :     interrupted = 0;
9 :     likelihood = 0.0;
10 :     keeper = new Keeper;
11 :    
12 :     // initialise counter used when printing output files
13 :     printcount = printinfo.getPrintIteration() - 1;
14 :    
15 :     // read the model specification from the main file
16 :     char* filename = main.getMainGadgetFile();
17 :     ifstream infile;
18 :     infile.open(filename, ios::in);
19 :     CommentStream commin(infile);
20 :     handle.checkIfFailure(infile, filename);
21 :     handle.Open(filename);
22 :     this->readMain(commin, main);
23 :     handle.Close();
24 :     infile.close();
25 :     infile.clear();
26 :    
27 :     // if this is an optimising run then read the optimisation parameters from file
28 :     if (main.runOptimise()) {
29 :     handle.logMessage(LOGMESSAGE, ""); //write blank line to log file
30 :     if (main.getOptInfoGiven()) {
31 :     filename = main.getOptInfoFile();
32 :     infile.open(filename, ios::in);
33 :     handle.checkIfFailure(infile, filename);
34 :     handle.Open(filename);
35 :     this->readOptimisation(commin);
36 :     handle.Close();
37 :     infile.close();
38 :     infile.clear();
39 :     } else {
40 :     handle.logMessage(LOGINFO, "Warning - no optimisation file specified, using default values");
41 :     optvec.resize(new OptInfoHooke());
42 :     }
43 :     }
44 :    
45 :     if (main.runOptimise())
46 :     handle.logMessage(LOGINFO, "\nFinished reading model data files, starting to run optimisation");
47 :     else
48 :     handle.logMessage(LOGINFO, "\nFinished reading model data files, starting to run simulation");
49 :     handle.logMessage(LOGMESSAGE, ""); //write blank line to log file
50 :     }
51 :    
52 :     Ecosystem::~Ecosystem() {
53 :     int i;
54 :     for (i = 0; i < optvec.Size(); i++)
55 :     delete optvec[i];
56 :     for (i = 0; i < printvec.Size(); i++)
57 :     delete printvec[i];
58 :     for (i = 0; i < likevec.Size(); i++)
59 :     delete likevec[i];
60 :     for (i = 0; i < tagvec.Size(); i++)
61 :     delete tagvec[i];
62 :     for (i = 0; i < basevec.Size(); i++)
63 :     delete basevec[i];
64 :    
65 :     delete Area;
66 :     delete TimeInfo;
67 :     delete keeper;
68 :     }
69 :    
70 :     void Ecosystem::writeStatus(const char* filename) const {
71 :     ofstream outfile;
72 :     outfile.open(filename, ios::out);
73 :     handle.checkIfFailure(outfile, filename);
74 :     handle.Open(filename);
75 :     RUNID.Print(outfile);
76 :     outfile << "The current simulation time is " << TimeInfo->getYear()
77 :     << ", step " << TimeInfo->getStep() << endl;
78 :    
79 :     int i;
80 :     for (i = 0; i < basevec.Size(); i++)
81 :     basevec[i]->Print(outfile);
82 :     for (i = 0; i < likevec.Size(); i++)
83 :     likevec[i]->Print(outfile);
84 :     for (i = 0; i < tagvec.Size(); i++)
85 :     tagvec[i]->Print(outfile);
86 :    
87 :     handle.Close();
88 :     outfile.close();
89 :     outfile.clear();
90 :     }
91 :    
92 :     void Ecosystem::Reset() {
93 :     int i;
94 :     TimeInfo->Reset();
95 :     for (i = 0; i < likevec.Size(); i++)
96 :     likevec[i]->Reset(keeper);
97 :     for (i = 0; i < basevec.Size(); i++)
98 :     basevec[i]->Reset(TimeInfo);
99 :     }
100 :    
101 :     void Ecosystem::Optimise() {
102 :     int i;
103 :     for (i = 0; i < optvec.Size(); i++) {
104 :     optvec[i]->OptimiseLikelihood();
105 :     this->writeOptValues();
106 :     }
107 :     }
108 :    
109 :     double Ecosystem::SimulateAndUpdate(const DoubleVector& x) {
110 :     int i, j;
111 :    
112 :     if (funceval == 0) {
113 :     // JMB - only need to create these vectors once
114 :     initialval.resize(keeper->numVariables(), 0.0);
115 :     currentval.resize(keeper->numVariables(), 0.0);
116 :     optflag.resize(keeper->numVariables(), 0);
117 :     keeper->getOptFlags(optflag);
118 :     }
119 :    
120 :     j = 0;
121 :     keeper->getCurrentValues(currentval);
122 :     keeper->getInitialValues(initialval);
123 :     for (i = 0; i < currentval.Size(); i++) {
124 :     if (optflag[i]) {
125 :     currentval[i] = x[j] * initialval[i];
126 :     j++;
127 :     }
128 :     }
129 :    
130 :     keeper->Update(currentval);
131 :     this->Simulate(0); //dont print whilst optimising
132 :    
133 :     if (printinfo.getPrint()) {
134 :     printcount++;
135 :     if (printcount == printinfo.getPrintIteration()) {
136 :     keeper->writeValues(likevec, printinfo.getPrecision());
137 :     printcount = 0;
138 :     }
139 :     }
140 :    
141 :     funceval++;
142 :     return likelihood;
143 :     }
144 :    
145 :     void Ecosystem::writeOptValues() {
146 :     int i;
147 :     DoubleVector tmpvec(likevec.Size(), 0.0);
148 :     for (i = 0; i < likevec.Size(); i++)
149 :     tmpvec[i] = likevec[i]->getUnweightedLikelihood();
150 :    
151 :     handle.logMessage(LOGINFO, "\nAfter a total of", funceval, "function evaluations the best point found is");
152 :     keeper->writeBestValues();
153 :     handle.logMessage(LOGINFO, "\nThe scores from each likelihood component are");
154 :     handle.logMessage(LOGINFO, tmpvec);
155 :     if (!isZero(keeper->getBestLikelihoodScore())) // no better point has been found
156 :     handle.logMessage(LOGINFO, "\nThe overall likelihood score is", keeper->getBestLikelihoodScore());
157 :     else
158 :     handle.logMessage(LOGINFO, "\nThe overall likelihood score is", this->getLikelihood());
159 :     }
160 :    
161 :     void Ecosystem::writeInitialInformation(const char* const filename) {
162 :     keeper->openPrintFile(filename);
163 :     keeper->writeInitialInformation(likevec);
164 :     }
165 :    
166 :     void Ecosystem::writeValues() {
167 :     keeper->writeValues(likevec, printinfo.getPrecision());
168 :     }
169 :    
170 :     void Ecosystem::writeParams(const char* const filename, int prec) const {
171 :     if ((funceval > 0) && (interrupted == 0)) {
172 :     //JMB - print the final values to any output files specified
173 :     //in case they have been missed by the -print value
174 :     if (printinfo.getPrint())
175 :     keeper->writeValues(likevec, printinfo.getPrecision());
176 :     }
177 :     keeper->writeParams(optvec, filename, prec, interrupted);
178 :     }

root@forge.cesga.es
ViewVC Help
Powered by ViewVC 1.0.0  

Powered By FusionForge