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/agebandmatrixmemberfunctions.cc
[mareframe] / trunk / gadget / agebandmatrixmemberfunctions.cc Repository:
ViewVC logotype

Annotation of /trunk/gadget/agebandmatrixmemberfunctions.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "agebandmatrix.h"
2 :     #include "agebandmatrixptrvector.h"
3 :     #include "mathfunc.h"
4 :     #include "doublevector.h"
5 :     #include "conversionindex.h"
6 :     #include "popinfovector.h"
7 :     #include "errorhandler.h"
8 :     #include "gadget.h"
9 :     #include "global.h"
10 :    
11 :     void AgeBandMatrix::Add(const AgeBandMatrix& Addition, const ConversionIndex &CI, double ratio) {
12 :    
13 :     PopInfo pop;
14 :     int minaddage = max(this->minAge(), Addition.minAge());
15 :     int maxaddage = min(this->maxAge(), Addition.maxAge());
16 :     int age, l, minl, maxl;
17 :    
18 :     if ((maxaddage < minaddage) || (isZero(ratio)))
19 :     return;
20 :    
21 :     if (CI.isSameDl()) {
22 :     int offset = CI.getOffset();
23 :     for (age = minaddage; age <= maxaddage; age++) {
24 :     minl = max(this->minLength(age), Addition.minLength(age) + offset);
25 :     maxl = min(this->maxLength(age), Addition.maxLength(age) + offset);
26 :     for (l = minl; l < maxl; l++) {
27 :     pop = Addition[age][l - offset];
28 :     pop *= ratio;
29 :     (*v[age - minage])[l] += pop;
30 :     }
31 :     }
32 :    
33 :     } else {
34 :     if (CI.isFiner()) {
35 :     for (age = minaddage; age <= maxaddage; age++) {
36 :     minl = max(this->minLength(age), CI.minPos(Addition.minLength(age)));
37 :     maxl = min(this->maxLength(age), CI.maxPos(Addition.maxLength(age) - 1) + 1);
38 :     for (l = minl; l < maxl; l++) {
39 :     pop = Addition[age][CI.getPos(l)];
40 :     pop *= ratio;
41 :     pop.N /= CI.getNumPos(l); //JMB CI.getNumPos() should never be zero
42 :     (*v[age - minage])[l] += pop;
43 :     }
44 :     }
45 :    
46 :     } else {
47 :     for (age = minaddage; age <= maxaddage; age++) {
48 :     minl = max(CI.minPos(this->minLength(age)), Addition.minLength(age));
49 :     maxl = min(CI.maxPos(this->maxLength(age) - 1) + 1, Addition.maxLength(age));
50 :     if (maxl > minl && CI.getPos(maxl - 1) < this->maxLength(age)
51 :     && CI.getPos(minl) >= this->minLength(age)) {
52 :     for (l = minl; l < maxl; l++) {
53 :     pop = Addition[age][l];
54 :     pop *= ratio;
55 :     (*v[age - minage])[CI.getPos(l)] += pop;
56 :     }
57 :     }
58 :     }
59 :     }
60 :     }
61 :     }
62 :    
63 :     void AgeBandMatrix::Subtract(const DoubleVector& Ratio, const ConversionIndex& CI) {
64 :     int i, j, j1, j2;
65 :     if (CI.isSameDl()) {
66 :     int offset = CI.getOffset();
67 :     for (i = 0; i < nrow; i++) {
68 :     j1 = max(v[i]->minCol(), CI.minLength());
69 :     j2 = min(v[i]->maxCol(), CI.maxLength());
70 :     for (j = j1; j < j2; j++)
71 :     (*v[i])[j] *= Ratio[j - offset];
72 :     }
73 :     } else {
74 :     for (i = 0; i < nrow; i++) {
75 :     j1 = max(v[i]->minCol(), CI.minLength());
76 :     j2 = min(v[i]->maxCol(), CI.maxLength());
77 :     for (j = j1; j < j2; j++)
78 :     (*v[i])[j] *= Ratio[CI.getPos(j)];
79 :     }
80 :     }
81 :     }
82 :    
83 :     void AgeBandMatrix::Multiply(const DoubleVector& Ratio) {
84 :     int i, j;
85 :     for (i = 0; i < nrow; i++)
86 :     for (j = v[i]->minCol(); j < v[i]->maxCol(); j++)
87 :     (*v[i])[j] *= Ratio[i];
88 :     }
89 :    
90 :     void AgeBandMatrix::sumColumns(PopInfoVector& Result) const {
91 :     int i, j;
92 :     for (i = 0; i < Result.Size(); i++)
93 :     Result[i].setToZero();
94 :     for (i = 0; i < nrow; i++)
95 :     for (j = v[i]->minCol(); j < v[i]->maxCol(); j++)
96 :     Result[j] += (*v[i])[j];
97 :     }
98 :    
99 :     void AgeBandMatrix::IncrementAge() {
100 :     int i, j;
101 :    
102 :     if (nrow <= 1)
103 :     return; //only one age
104 :    
105 :     //for the oldest age group
106 :     i = nrow - 1;
107 :     for (j = v[i]->minCol(); j < v[i]->maxCol(); j++)
108 :     (*v[i])[j] += (*v[i - 1])[j];
109 :    
110 :     //for the other age groups
111 :     for (i = nrow - 2; i > 0; i--)
112 :     for (j = v[i]->minCol(); j < v[i]->maxCol(); j++)
113 :     (*v[i])[j] = (*v[i - 1])[j];
114 :    
115 :     //for the youngest age group
116 :     for (j = v[0]->minCol(); j < v[0]->maxCol(); j++)
117 :     (*v[0])[j].setToZero();
118 :     }
119 :    
120 :     void AgeBandMatrix::setToZero() {
121 :     int i, j;
122 :     for (i = 0; i < nrow; i++)
123 :     for (j = v[i]->minCol(); j < v[i]->maxCol(); j++)
124 :     (*v[i])[j].setToZero();
125 :     }
126 :    
127 :     void AgeBandMatrix::printNumbers(ofstream& outfile) const {
128 :     int i, j;
129 :     int maxcol = 0;
130 :     for (i = 0; i < nrow; i++)
131 :     if (v[i]->maxCol() > maxcol)
132 :     maxcol = v[i]->maxCol();
133 :    
134 :     for (i = 0; i < nrow; i++) {
135 :     outfile << TAB;
136 :     for (j = 0; j < v[i]->minCol(); j++)
137 :     outfile << setw(smallwidth) << 0.0 << sep;
138 :     for (j = v[i]->minCol(); j < v[i]->maxCol(); j++)
139 :     outfile << setw(smallwidth) << setprecision(smallprecision) << (*v[i])[j].N << sep;
140 :     for (j = v[i]->maxCol(); j < maxcol; j++)
141 :     outfile << setw(smallwidth) << 0.0 << sep;
142 :     outfile << endl;
143 :     }
144 :     }
145 :    
146 :     void AgeBandMatrix::printWeights(ofstream& outfile) const {
147 :     int i, j;
148 :     int maxcol = 0;
149 :     for (i = 0; i < nrow; i++)
150 :     if (v[i]->maxCol() > maxcol)
151 :     maxcol = v[i]->maxCol();
152 :    
153 :     for (i = 0; i < nrow; i++) {
154 :     outfile << TAB;
155 :     for (j = 0; j < v[i]->minCol(); j++)
156 :     outfile << setw(smallwidth) << 0.0 << sep;
157 :     for (j = v[i]->minCol(); j < v[i]->maxCol(); j++)
158 :     outfile << setw(smallwidth) << setprecision(smallprecision) << (*v[i])[j].W << sep;
159 :     for (j = v[i]->maxCol(); j < maxcol; j++)
160 :     outfile << setw(smallwidth) << 0.0 << sep;
161 :     outfile << endl;
162 :     }
163 :     }
164 :    
165 :     void AgeBandMatrixPtrVector::Migrate(const DoubleMatrix& MI, PopInfoVector& tmp) {
166 :     int i, j, age, length;
167 :     for (age = v[0]->minAge(); age <= v[0]->maxAge(); age++) {
168 :     for (length = v[0]->minLength(age); length < v[0]->maxLength(age); length++) {
169 :     for (j = 0; j < size; j++)
170 :     tmp[j].setToZero();
171 :    
172 :     //let tmp[j] keep the population of agelength group on area j after the migration
173 :     for (j = 0; j < size; j++)
174 :     for (i = 0; i < size; i++)
175 :     tmp[j] += (*v[i])[age][length] * MI[j][i];
176 :    
177 :     for (j = 0; j < size; j++)
178 :     (*v[j])[age][length] = tmp[j];
179 :     }
180 :     }
181 :     }

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

Powered By FusionForge