1 : |
agomez |
1 |
#include "poppredator.h" |
2 : |
|
|
#include "mathfunc.h" |
3 : |
|
|
#include "errorhandler.h" |
4 : |
|
|
#include "gadget.h" |
5 : |
|
|
#include "global.h" |
6 : |
|
|
|
7 : |
|
|
PopPredator::PopPredator(const char* givenname, const IntVector& Areas,
|
8 : |
|
|
const LengthGroupDivision* const OtherLgrpDiv, const LengthGroupDivision* const GivenLgrpDiv)
|
9 : |
|
|
: Predator(givenname, Areas) {
|
10 : |
|
|
|
11 : |
|
|
LgrpDiv = new LengthGroupDivision(*GivenLgrpDiv);
|
12 : |
|
|
if (LgrpDiv->Error())
|
13 : |
|
|
handle.logMessage(LOGFAIL, "Error in poppredator - failed to create length group");
|
14 : |
|
|
CI = new ConversionIndex(OtherLgrpDiv, LgrpDiv);
|
15 : |
|
|
if (CI->Error())
|
16 : |
|
|
handle.logMessage(LOGFAIL, "Error in poppredator - error when checking length structure");
|
17 : |
|
|
}
|
18 : |
|
|
|
19 : |
|
|
PopPredator::PopPredator(const char* givenname, const IntVector& Areas)
|
20 : |
|
|
: Predator(givenname, Areas), LgrpDiv(0), CI(0) {
|
21 : |
|
|
}
|
22 : |
|
|
|
23 : |
|
|
PopPredator::~PopPredator() {
|
24 : |
|
|
int i, j;
|
25 : |
|
|
delete LgrpDiv;
|
26 : |
|
|
delete CI;
|
27 : |
|
|
for (i = 0; i < consumption.Nrow(); i++) {
|
28 : |
|
|
for (j = 0; j < consumption[i].Size(); j++) {
|
29 : |
|
|
delete consumption[i][j];
|
30 : |
|
|
delete cons[i][j];
|
31 : |
|
|
delete usesuit[i][j];
|
32 : |
|
|
}
|
33 : |
|
|
}
|
34 : |
|
|
for (i = 0; i < predratio.Size(); i++)
|
35 : |
|
|
delete predratio[i];
|
36 : |
|
|
}
|
37 : |
|
|
|
38 : |
|
|
void PopPredator::Print(ofstream& outfile) const {
|
39 : |
|
|
Predator::Print(outfile);
|
40 : |
|
|
int i, area;
|
41 : |
|
|
|
42 : |
|
|
outfile << TAB;
|
43 : |
|
|
LgrpDiv->Print(outfile);
|
44 : |
|
|
for (area = 0; area < areas.Size(); area++) {
|
45 : |
|
|
outfile << "\tNumber of predators on internal area " << areas[area] << ":\n\t";
|
46 : |
|
|
for (i = 0; i < LgrpDiv->numLengthGroups(); i++)
|
47 : |
|
|
outfile << setw(smallwidth) << setprecision(smallprecision) << prednumber[area][i].N << sep;
|
48 : |
|
|
outfile << "\n\tWeight of predators on internal area " << areas[area] << ":\n\t";
|
49 : |
|
|
for (i = 0; i < LgrpDiv->numLengthGroups(); i++)
|
50 : |
|
|
outfile << setw(smallwidth) << setprecision(smallprecision) << prednumber[area][i].W << sep;
|
51 : |
|
|
outfile << "\n\tTotal amount eaten on internal area " << areas[area] << ":\n\t";
|
52 : |
|
|
for (i = 0; i < LgrpDiv->numLengthGroups(); i++)
|
53 : |
|
|
outfile << setw(smallwidth) << setprecision(smallprecision) << totalconsumption[area][i] << sep;
|
54 : |
|
|
outfile << "\n\tOverconsumption on internal area " << areas[area] << ":\n\t";
|
55 : |
|
|
for (i = 0; i < LgrpDiv->numLengthGroups(); i++)
|
56 : |
|
|
outfile << setw(smallwidth) << setprecision(smallprecision) << overconsumption[area][i] << sep;
|
57 : |
|
|
outfile << endl;
|
58 : |
|
|
}
|
59 : |
|
|
}
|
60 : |
|
|
|
61 : |
|
|
const DoubleMatrix& PopPredator::getConsumption(int area, const char* preyname) const {
|
62 : |
|
|
int prey;
|
63 : |
|
|
for (prey = 0; prey < this->numPreys(); prey++)
|
64 : |
|
|
if (strcasecmp(this->getPreyName(prey), preyname) == 0)
|
65 : |
|
|
return (*consumption[this->areaNum(area)][prey]);
|
66 : |
|
|
|
67 : |
|
|
handle.logMessage(LOGFAIL, "Error in poppredator - failed to match prey", preyname);
|
68 : |
|
|
exit(EXIT_FAILURE);
|
69 : |
|
|
}
|
70 : |
|
|
|
71 : |
|
|
double PopPredator::getConsumptionBiomass(int prey, int area) const {
|
72 : |
|
|
int inarea = this->areaNum(area);
|
73 : |
|
|
if (inarea == -1)
|
74 : |
|
|
return 0.0;
|
75 : |
|
|
|
76 : |
|
|
int i, j;
|
77 : |
|
|
double kilos = 0.0;
|
78 : |
|
|
for (i = 0; i < (*consumption[inarea][prey]).Nrow(); i++)
|
79 : |
|
|
for (j = 0; j < (*consumption[inarea][prey]).Ncol(i); j++)
|
80 : |
|
|
kilos += (*consumption[inarea][prey])[i][j];
|
81 : |
|
|
|
82 : |
|
|
return kilos;
|
83 : |
|
|
}
|
84 : |
|
|
|
85 : |
|
|
void PopPredator::Reset(const TimeClass* const TimeInfo) {
|
86 : |
|
|
Predator::Reset(TimeInfo);
|
87 : |
|
|
|
88 : |
|
|
int i, area;
|
89 : |
|
|
if (TimeInfo->getSubStep() == 1) {
|
90 : |
|
|
for (area = 0; area < areas.Size(); area++) {
|
91 : |
|
|
totalconsumption[area].setToZero();
|
92 : |
|
|
(*predratio[area]).setToZero();
|
93 : |
|
|
for (i = 0; i < LgrpDiv->numLengthGroups(); i++)
|
94 : |
|
|
prednumber[area][i].setToZero();
|
95 : |
|
|
for (i = 0; i < this->numPreys(); i++)
|
96 : |
|
|
(*consumption[area][i]).setToZero();
|
97 : |
|
|
|
98 : |
|
|
//JMB only reset these if they are needed ...
|
99 : |
|
|
for (i = 0; i < this->numPreys(); i++)
|
100 : |
|
|
if ((hasoverconsumption[area]) || (this->didChange(i, TimeInfo)))
|
101 : |
|
|
(*usesuit[area][i]) = this->getSuitability(i);
|
102 : |
|
|
|
103 : |
|
|
if (hasoverconsumption[area]) {
|
104 : |
|
|
hasoverconsumption[area] = 0;
|
105 : |
|
|
overconsumption[area].setToZero();
|
106 : |
|
|
}
|
107 : |
|
|
}
|
108 : |
|
|
}
|
109 : |
|
|
|
110 : |
|
|
if (handle.getLogLevel() >= LOGMESSAGE)
|
111 : |
|
|
handle.logMessage(LOGMESSAGE, "Reset predatation data for predator", this->getName());
|
112 : |
|
|
}
|
113 : |
|
|
|
114 : |
|
|
void PopPredator::setPrey(PreyPtrVector& preyvec, Keeper* const keeper) {
|
115 : |
|
|
Predator::setPrey(preyvec, keeper);
|
116 : |
|
|
|
117 : |
|
|
int i, j, numlen, numarea, preylen;
|
118 : |
|
|
if (LgrpDiv == 0) {
|
119 : |
|
|
//need to construct length group based on the min/max lengths of the preys
|
120 : |
|
|
double minl, maxl;
|
121 : |
|
|
minl = 9999.0;
|
122 : |
|
|
maxl = 0.0;
|
123 : |
|
|
for (i = 0; i < this->numPreys(); i++) {
|
124 : |
|
|
minl = min(this->getPrey(i)->getLengthGroupDiv()->minLength(), minl);
|
125 : |
|
|
maxl = max(this->getPrey(i)->getLengthGroupDiv()->maxLength(), maxl);
|
126 : |
|
|
}
|
127 : |
|
|
LgrpDiv = new LengthGroupDivision(minl, maxl, maxl - minl);
|
128 : |
|
|
if (LgrpDiv->Error())
|
129 : |
|
|
handle.logMessage(LOGFAIL, "Error in poppredator - failed to create length group");
|
130 : |
|
|
CI = new ConversionIndex(LgrpDiv, LgrpDiv);
|
131 : |
|
|
if (CI->Error())
|
132 : |
|
|
handle.logMessage(LOGFAIL, "Error in poppredator - error when checking length structure");
|
133 : |
|
|
}
|
134 : |
|
|
|
135 : |
|
|
//now we need to initialise things
|
136 : |
|
|
Predator::Initialise();
|
137 : |
|
|
PopInfo nullpop;
|
138 : |
|
|
numlen = LgrpDiv->numLengthGroups();
|
139 : |
|
|
numarea = areas.Size();
|
140 : |
|
|
for (i = 0; i < numarea; i++) {
|
141 : |
|
|
cons.resize();
|
142 : |
|
|
consumption.resize();
|
143 : |
|
|
usesuit.resize();
|
144 : |
|
|
predratio.resize(new DoubleMatrix(this->numPreys(), numlen, 0.0));
|
145 : |
|
|
for (j = 0; j < this->numPreys(); j++) {
|
146 : |
|
|
preylen = this->getPrey(j)->getLengthGroupDiv()->numLengthGroups();
|
147 : |
|
|
cons[i].resize(new DoubleMatrix(numlen, preylen, 0.0));
|
148 : |
|
|
consumption[i].resize(new DoubleMatrix(numlen, preylen, 0.0));
|
149 : |
|
|
usesuit[i].resize(new DoubleMatrix(numlen, preylen, 0.0));
|
150 : |
|
|
}
|
151 : |
|
|
}
|
152 : |
|
|
|
153 : |
|
|
hasoverconsumption.resize(numarea, 0);
|
154 : |
|
|
totalcons.AddRows(numarea, numlen, 0.0);
|
155 : |
|
|
overcons.AddRows(numarea, numlen, 0.0);
|
156 : |
|
|
totalconsumption.AddRows(numarea, numlen, 0.0);
|
157 : |
|
|
overconsumption.AddRows(numarea, numlen, 0.0);
|
158 : |
|
|
prednumber.AddRows(numarea, numlen, nullpop);
|
159 : |
|
|
}
|
160 : |
|
|
|
161 : |
|
|
double PopPredator::getTotalOverConsumption(int area) const {
|
162 : |
|
|
int inarea = this->areaNum(area);
|
163 : |
|
|
if (inarea == -1)
|
164 : |
|
|
return 0.0;
|
165 : |
|
|
|
166 : |
|
|
int i;
|
167 : |
|
|
double total = 0.0;
|
168 : |
|
|
for (i = 0; i < LgrpDiv->numLengthGroups(); i++)
|
169 : |
|
|
total += overconsumption[inarea][i];
|
170 : |
|
|
return total;
|
171 : |
|
|
}
|
172 : |
|
|
|
173 : |
|
|
const PopInfoVector& PopPredator::getConsumptionPopInfo(int area, const char* preyname) const {
|
174 : |
|
|
int prey;
|
175 : |
|
|
for (prey = 0; prey < this->numPreys(); prey++)
|
176 : |
|
|
if (strcasecmp(this->getPreyName(prey), preyname) == 0)
|
177 : |
|
|
return this->getPrey(prey)->getConsumptionPopInfo(area);
|
178 : |
|
|
|
179 : |
|
|
handle.logMessage(LOGFAIL, "Error in poppredator - failed to match prey", preyname);
|
180 : |
|
|
exit(EXIT_FAILURE);
|
181 : |
|
|
}
|