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

Annotation of /trunk/gadget/predatorpreyaggregator.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "predatorpreyaggregator.h"
2 :     #include "errorhandler.h"
3 :     #include "stock.h"
4 :     #include "stockprey.h"
5 :     #include "poppredator.h"
6 :     #include "mathfunc.h"
7 :     #include "gadget.h"
8 :     #include "global.h"
9 :    
10 :     PredatorPreyAggregator::PredatorPreyAggregator(const PredatorPtrVector& Predators,
11 :     const PreyPtrVector& Preys, LengthGroupDivision* const Lgrpdiv,
12 :     const IntMatrix& Areas, const IntMatrix& Ages)
13 :     : predators(Predators), preys(Preys), LgrpDiv(Lgrpdiv), areas(Areas), ages(Ages),
14 :     doeseat(Predators.Size(), Preys.Size(), 0), suitptr(0), alptr(0) {
15 :    
16 :     int i, j;
17 :     for (i = 0; i < predators.Size(); i++)
18 :     for (j = 0; j < preys.Size(); j++)
19 :     if (predators[i]->doesEat(preys[j]->getName()))
20 :     doeseat[i][j] = 1;
21 :    
22 :     for (i = 0; i < preys.Size(); i++) {
23 :     CI.resize(new ConversionIndex(preys[i]->getLengthGroupDiv(), LgrpDiv));
24 :     if (CI[i]->Error())
25 :     handle.logMessage(LOGFAIL, "Error in predatorpreyaggregator - error when checking length structure");
26 :    
27 :     //check that the prey is a stock
28 :     if (preys[i]->getType() == LENGTHPREY)
29 :     handle.logMessage(LOGFAIL, "Error in predatorpreyaggregator - cannot aggregate prey", preys[i]->getName());
30 :     }
31 :    
32 :     //resize objects to store the agggregated information
33 :     for (i = 0; i < areas.Nrow(); i++)
34 :     mortality.resize(new DoubleMatrix(ages.Nrow(), LgrpDiv->numLengthGroups(), 0.0));
35 :     PopInfo tmppop;
36 :     tmppop.N = 1.0;
37 :     PopInfoMatrix popmatrix(ages.Nrow(), LgrpDiv->numLengthGroups(), tmppop);
38 :     total.resize(areas.Nrow(), 0, 0, popmatrix);
39 :     consume.resize(areas.Nrow(), 0, 0, popmatrix);
40 :     this->Reset();
41 :     }
42 :    
43 :     PredatorPreyAggregator::~PredatorPreyAggregator() {
44 :     int i;
45 :     for (i = 0; i < CI.Size(); i++)
46 :     delete CI[i];
47 :     for (i = 0; i < mortality.Size(); i++)
48 :     delete mortality[i];
49 :     }
50 :    
51 :     void PredatorPreyAggregator::Reset() {
52 :     int i;
53 :     for (i = 0; i < mortality.Size(); i++) {
54 :     total[i].setToZero();
55 :     consume[i].setToZero();
56 :     (*mortality[i]).setToZero();
57 :     }
58 :     }
59 :    
60 :     void PredatorPreyAggregator::Sum(const TimeClass* const TimeInfo) {
61 :    
62 :     int f, g, h, i, j, k, l, m;
63 :     double ratio;
64 :    
65 :     this->Reset();
66 :     //Sum over the appropriate predators, preys, areas, ages and length groups
67 :     //First calculate the prey population that is consumed by the predation
68 :     for (f = 0; f < predators.Size(); f++) {
69 :     for (g = 0; g < preys.Size(); g++) {
70 :     if (doeseat[f][g]) {
71 :     for (i = 0; i < areas.Nrow(); i++) {
72 :     for (j = 0; j < areas.Ncol(i); j++) {
73 :     if ((preys[g]->isPreyArea(areas[i][j])) && (predators[f]->isInArea(areas[i][j]))) {
74 :     for (k = 0; k < predators[f]->numPreys(); k++) {
75 :     if (strcasecmp(preys[g]->getName(), predators[f]->getPrey(k)->getName()) == 0) {
76 :     alptr = &((StockPrey*)preys[g])->getConsumptionALK(areas[i][j]);
77 :     for (h = 0; h < predators[f]->getLengthGroupDiv()->numLengthGroups(); h++) {
78 :     //suitptr = &predators[f]->getSuitability(k)[h];
79 :     suitptr = &((PopPredator*)predators[f])->getUseSuitability(areas[i][j], k)[h];
80 :     ratio = predators[f]->getConsumptionRatio(areas[i][j], k, h);
81 :     for (l = 0; l < ages.Nrow(); l++)
82 :     for (m = 0; m < ages.Ncol(l); m++)
83 :     if ((alptr->minAge() <= ages[l][m]) && (ages[l][m] <= alptr->maxAge()))
84 :     consume[i][l].Add((*alptr)[ages[l][m]], *CI[g], *suitptr, ratio);
85 :     }
86 :     }
87 :     }
88 :     }
89 :     }
90 :     }
91 :     }
92 :     }
93 :     }
94 :    
95 :     //Then calculate the prey population before predation
96 :     for (g = 0; g < preys.Size(); g++) {
97 :     for (i = 0; i < areas.Nrow(); i++) {
98 :     for (j = 0; j < areas.Ncol(i); j++) {
99 :     if (preys[g]->isPreyArea(areas[i][j])) {
100 :     alptr = &((StockPrey*)preys[g])->getConsumptionALK(areas[i][j]);
101 :     for (l = 0; l < ages.Nrow(); l++)
102 :     for (m = 0; m < ages.Ncol(l); m++)
103 :     if ((alptr->minAge() <= ages[l][m]) && (ages[l][m] <= alptr->maxAge()))
104 :     total[i][l].Add((*alptr)[ages[l][m]], *CI[g]);
105 :    
106 :     }
107 :     }
108 :     }
109 :     }
110 :    
111 :     //Finally calculate the mortality caused by the predation
112 :     ratio = 1.0 / TimeInfo->getTimeStepSize();
113 :     for (i = 0; i < mortality.Size(); i++)
114 :     for (j = 0; j < (*mortality[i]).Nrow(); j++)
115 :     for (k = 0; k < (*mortality[i]).Ncol(j); k++)
116 :     (*mortality[i])[j][k] = calcMortality(consume[i][j][k].N, total[i][j][k].N, ratio);
117 :     }

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

Powered By FusionForge