ALMaSS Rabbit ODdox  1.1
The rabbit model description following ODdox protocol
pesticide.h
Go to the documentation of this file.
1 //
2 // pesticide.h
3 //
4 /*
5 *******************************************************************************************************
6 Copyright (c) 2003, Christopher John Topping, EcoSol
7 All rights reserved.
8 
9 Redistribution and use in source and binary forms, with or without modification, are permitted provided
10 that the following conditions are met:
11 
12 Redistributions of source code must retain the above copyright notice, this list of conditions and the
13 following disclaimer.
14 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
15 the following disclaimer in the documentation and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
20 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 ********************************************************************************************************
26 */
27 
28 #ifndef PESTICIDE_H
29 #define PESTICIDE_H
30 
31 // Side length of a square in the pesticide map is given by this parameter
32 // as a power of two. Ie. 3 will give a side length of 8 main map units
33 // as 2^3 equals 8, which must also be set below
34 //
35 // Unfortunately it would be a huge performance penalty to have to make
36 // this a runtime configuration variable, it has to be compiled in.
37 // Standard rough settings here:
38 
39 #define PEST_GRIDSIZE_POW2 0
40 #define PEST_GRIDSIZE 1
41 #define PEST_GRIDAREA 1 // i.e. PEST_GRIDSIZE^2
42 /*
43 #define PEST_GRIDSIZE_POW2 2
44 #define PEST_GRIDSIZE 4
45 #define PEST_GRIDAREA 16 // i.e. PEST_GRIDSIZE^2
46 */
47 
48 
49 // Consider undef for production code, no speed penalty if defined.
50 #define PEST_DEBUG
51 
52 // Check for water bodies when asking for pesticide concentration
53 // at main map coords x,y? Speed vs. reality tradeoff. You
54 // probably want this defined.
55 //#define PEST_WATER_CHECK
56 
57 
58 extern class Pesticide *g_pest;
59 
60 
62 {
67  double m_amount;
69 
70  PesticideEvent(LE* a_sprayed_elem, double a_amount, PlantProtectionProducts a_ppp) {
71  m_sprayed_elem = a_sprayed_elem;
72  m_amount = a_amount;
73  m_ppp = a_ppp;
74  }
75 };
76 
77 class Diffusor
78 {
84  int m_dx;
85  int m_dy;
86  double m_fraction;
87 
88 public:
89  Diffusor( int a_dx, int a_dy, double a_fraction ) {
90  m_dx = a_dx;
91  m_dy = a_dy;
92  m_fraction = a_fraction;
93  }
94 
95  void SetFraction(double a_frac) { m_fraction = a_frac; }
96  double GetFraction() { return m_fraction; }
97  int Getdx() { return m_dx; }
98  int Getdy() { return m_dy; }
99 
100 };
101 
103 typedef vector <Diffusor*> Diffusion_mask;
104 
105 
107 {
108  // Method call stack and ordering:
109  //
110  // Pesticide()
111  // DiffusionMaskInit()
112  // DiffusionFunction()
113  //
114  // Tick()
115  // MainMapDecay()
116  // DailyQueueProcess()
117  // TwinMapClear()
118  // TwinMapSpray()
119  // TwinMapSprayPixel()
120  // TwinMapSprayCorrectBorders()
121  // TwinMapCopyToMask()
122  // TwinMapDiffusion()
123  // DiffusionSprayPixel()
124  // ElementIsWater()
125  // TwinMapAddToMain()
126  // DailyQueueClear()
127  //
128  // SupplyPesticide()
129  // #ifdef PEST_WATER_CHECK
130  // ElementIsWater()
131  // #endif
132 
133  protected:
147 
149  int m_NoPPPs;
150 
151  // Main map coordinates beyond which we have to use special
152  // proportional constants when adding new amounts of pesticide
153  // to the pesticide map.
156  double m_prop;
157  double m_corr_x;
158  double m_corr_y;
159  int m_wind;
160 
161  // Daily decay fraction, precalculated from the pesticide
162  // half life parameter, for general, vegetation and soil
166 
167  // Local copy of pointer to the main map object.
169 
170  // Local copy of pointer to the main landscape object.
172 
173  // The pesticide map itself, and its twins used when adding newly
174  // sprayed squares to the main map.
175 #ifdef __DETAILED_PESTICIDE_FATE
176  vector<vector <double> > m_pest_map_vegcanopy;
177  vector<vector <double> > m_pest_map_soil;
178 #else
179  vector<vector <double> > m_pest_map_main;
180 #endif
181 
184  unsigned int m_pest_map_size;
186  unsigned int m_pest_map_width;
188  unsigned int m_pest_map_height;
189 
191  double m_RainWashoffFactor[10000];
194 
198 
200  vector<vector <PesticideEvent*>> m_daily_spray_queue;
201 
204 
205  bool ElementIsWater( int a_x, int a_y );
206 
208 
209  void TwinMapClear( int a_minx, int a_miny, int a_maxx, int a_maxy );
210  void TwinMapSpray( LE* a_element_spryaed, double a_amount, int a_minx, int a_miny, int a_maxx, int a_maxy );
211  void TwinMapSprayPixel( int a_large_map_x,
212  int a_large_map_y,
213  double a_fractional_amount );
214  void TwinMapSprayCorrectBorders( void );
215  //void TwinMapCopyToMask( int a_minx, int a_miny, int a_maxx, int a_maxy );
216  //void TwinMapAddToMain( void );
217  void TwinMapDiffusion( int a_minx, int a_miny, int a_maxx, int a_maxy, double a_cover, PlantProtectionProducts a_ppp);
218 
219  void DiffusionMaskInit( void );
220  double DiffusionFunction( double a_dist_meters );
221  void DiffusionSprayPixel( int a_x, int a_limit_x, int a_y, int a_limit_y, double a_amount, double a_cover, PlantProtectionProducts a_ppp );
223  void CalcRainWashOffFactors();
224 
225 public:
226  void Tick( void );
227  void DailyQueueAdd( LE* a_element_sprayed, double a_amount, PlantProtectionProducts a_ppp);
229  // Doesn't make sense for the pesticide map, as one cannot guarantee
230  // that all the area covered by a particular polygon has the
231  // same concentration of pesticide, but as an approximation we use the
232  // grid cell covering ValidX & ValidY for that polygon.
233  //
234 #ifdef __DETAILED_PESTICIDE_FATE
235  double SupplyPesticideS(int a_x, int a_y, PlantProtectionProducts a_ppp);
236  double SupplyPesticideP(int a_x, int a_y, PlantProtectionProducts a_ppp);
237  double SupplyPesticideS(int a_polyref, PlantProtectionProducts a_ppp);
238  double SupplyPesticideP(int a_polyref, PlantProtectionProducts a_ppp);
239 #else
240  double SupplyPesticide( int a_x, int a_y, PlantProtectionProducts a_ppp);
241  double SupplyPesticide(int a_polyref, PlantProtectionProducts a_ppp);
242 #endif
243 
244  Pesticide( RasterMap *a_land, Landscape *a_map );
245  virtual ~Pesticide( void );
246 
247 #ifdef PEST_DEBUG
248  /* *** For testing of the pesticide engine. */
249 
250  bool SavePPM( double *a_map,
251  int a_beginx, int a_width,
252  int a_beginy, int a_height,
253  char* a_filename );
254 
255  void DiffusionMaskInitTest( void );
256  //void Test( Landscape *a_map );
257 #endif
258 
259 };
260 
261 
262 
263 inline bool Pesticide::ElementIsWater( int a_x, int a_y )
264 {
265  TTypesOfLandscapeElement l_eletype =
266  m_map->SupplyElementType( a_x, a_y );
267 
268  if ( l_eletype == tole_Freshwater ||
269  l_eletype == tole_River ||
270  l_eletype == tole_Pond ||
271  l_eletype == tole_FishFarm ||
272  l_eletype == tole_Saltwater
273  )
274  return true;
275 
276  return false;
277 }
278 
279 
280 #ifdef __DETAILED_PESTICIDE_FATE
281 
282 inline double Pesticide::SupplyPesticideS(int a_x, int a_y, PlantProtectionProducts a_ppp)
283 {
289  int l_x = a_x >> PEST_GRIDSIZE_POW2;
290  int l_y = a_y >> PEST_GRIDSIZE_POW2;
291 
292  return m_pest_map_soil[a_ppp][l_y * m_pest_map_width + l_x];
293 }
294 
295 inline double Pesticide::SupplyPesticideP(int a_x, int a_y, PlantProtectionProducts a_ppp)
296 {
302  int l_x = a_x >> PEST_GRIDSIZE_POW2;
303  int l_y = a_y >> PEST_GRIDSIZE_POW2;
304 
305  return m_pest_map_vegcanopy[a_ppp][l_y * m_pest_map_width + l_x];
306 }
307 
308 inline double Pesticide::SupplyPesticideS(int a_ele, PlantProtectionProducts a_ppp)
309 {
314  int l_c = m_map->SupplyPesticideCell(a_ele);
315  return m_pest_map_soil[a_ppp][l_c];
316 }
317 
318 inline double Pesticide::SupplyPesticideP(int a_ele, PlantProtectionProducts a_ppp)
319 {
324  int l_c = m_map->SupplyPesticideCell(a_ele);
325  return m_pest_map_vegcanopy[a_ppp][l_c];
326 }
327 
328 #else
329 inline double Pesticide::SupplyPesticide(int a_x, int a_y, PlantProtectionProducts a_ppp)
330 {
336 #ifdef PEST_WATER_CHECK
337  if (ElementIsWater(a_x, a_y))
338  return 0.0;
339 #endif
340 
341  int l_x = a_x >> PEST_GRIDSIZE_POW2;
342  int l_y = a_y >> PEST_GRIDSIZE_POW2;
343 
344  return m_pest_map_main[a_ppp][l_y * m_pest_map_width + l_x];
345 }
346 
348 {
349  // This is an approximation because we have no idea about the actual variation
350  // in pesticide concentrations within the polygon.
351  int l_c = m_map->SupplyPesticideCell(a_ele);
352  return m_pest_map_main[a_ppp][l_c];
353 }
354 
355 #endif
356 
357 inline void Pesticide::TwinMapSprayPixel(int a_large_map_x,
358  int a_large_map_y,
359  double a_fractional_amount)
360 {
361  int l_x = a_large_map_x >> PEST_GRIDSIZE_POW2;
362  int l_y = a_large_map_y >> PEST_GRIDSIZE_POW2;
363 
364  m_pest_map_twin[l_y * m_pest_map_width + l_x] +=
365  a_fractional_amount;
366 }
367 
368 
370 {
375 protected:
377  vector<double> * m_pmap_insecticides;
379  vector<double> * m_pmap_fungicides;
381  vector<double> * m_pmap_herbicides;
398 
399 public:
400  PesticideMap(int a_startyear, int a_noyears, int a_cellsize, Landscape* a_landscape, RasterMap* a_land, bool a_typeofmap);
401  ~PesticideMap();
402  bool DumpPMap(vector<double>* a_map);
406  void Spray(LE * a_element_sprayed, TTypesOfPesticideCategory a_type);
407 
408 };
409 #endif // PESTICIDE_H
Pesticide::TwinMapSprayPixel
void TwinMapSprayPixel(int a_large_map_x, int a_large_map_y, double a_fractional_amount)
Definition: pesticide.h:357
PesticideEvent::m_ppp
PlantProtectionProducts m_ppp
Definition: pesticide.h:68
Pesticide::DailyQueueClear
void DailyQueueClear(PlantProtectionProducts a_ppp)
Definition: pesticide.cpp:131
PesticideMap::m_cellsize
int m_cellsize
the size of the cell for pesticide data in m
Definition: pesticide.h:387
Diffusor
Definition: pesticide.h:77
tole_Saltwater
Definition: tole_declaration.h:66
Pesticide::TwinMapDiffusion
void TwinMapDiffusion(int a_minx, int a_miny, int a_maxx, int a_maxy, double a_cover, PlantProtectionProducts a_ppp)
Definition: pesticide.cpp:276
PesticideEvent::PesticideEvent
PesticideEvent(LE *a_sprayed_elem, double a_amount, PlantProtectionProducts a_ppp)
Definition: pesticide.h:70
PlantProtectionProducts
PlantProtectionProducts
A list PPP names for tracking by the Pesticide class.
Definition: farm.h:420
ppp_foobar
Definition: farm.h:432
Pesticide::m_rainfallcategory
unsigned m_rainfallcategory
Daily rainfall saved here * 100 to use as an index to the Pesticide::m_RainWashoffFactor array - an o...
Definition: pesticide.h:193
tole_Freshwater
Definition: tole_declaration.h:64
Pesticide::m_x_excess
int m_x_excess
Definition: pesticide.h:154
PesticideEvent::m_sprayed_elem
LE * m_sprayed_elem
Definition: pesticide.h:66
Landscape::SupplyElementType
TTypesOfLandscapeElement SupplyElementType(int a_polyref)
Definition: landscape.h:1110
PesticideMap::m_endyear
int m_endyear
last year of data to record
Definition: pesticide.h:385
tole_FishFarm
Definition: tole_declaration.h:102
Diffusor::GetFraction
double GetFraction()
Definition: pesticide.h:96
Pesticide::SupplyPesticide
double SupplyPesticide(int a_x, int a_y, PlantProtectionProducts a_ppp)
Definition: pesticide.h:329
Pesticide::m_y_excess
int m_y_excess
Definition: pesticide.h:155
Pesticide::ElementIsWater
bool ElementIsWater(int a_x, int a_y)
Definition: pesticide.h:263
PesticideMap::m_pmap_width
int m_pmap_width
based on cellsize the width of the map
Definition: pesticide.h:389
Pesticide
Definition: pesticide.h:106
PEST_GRIDSIZE_POW2
#define PEST_GRIDSIZE_POW2
Definition: pesticide.h:39
Pesticide::m_diffusion_mask
Diffusion_mask m_diffusion_mask[4]
Pre-calculated square diffusion map, assuming wind directions (4) Used after spraying an element in...
Definition: pesticide.h:197
Pesticide::m_pest_map_size
unsigned int m_pest_map_size
The total size of one map in cellsize resolution.
Definition: pesticide.h:184
PesticideMap::PesticideMap
PesticideMap(int a_startyear, int a_noyears, int a_cellsize, Landscape *a_landscape, RasterMap *a_land, bool a_typeofmap)
Definition: pesticide.cpp:769
Pesticide::DiffusionSprayPixel
void DiffusionSprayPixel(int a_x, int a_limit_x, int a_y, int a_limit_y, double a_amount, double a_cover, PlantProtectionProducts a_ppp)
Definition: pesticide.cpp:301
PesticideMap::DumpPMapF
bool DumpPMapF()
Definition: pesticide.h:405
Pesticide::~Pesticide
virtual ~Pesticide(void)
Definition: pesticide.cpp:621
PesticideMap::m_pmap_fungicides
vector< double > * m_pmap_fungicides
herbicide map data
Definition: pesticide.h:379
Pesticide::Tick
void Tick(void)
Definition: pesticide.cpp:97
PesticideMap::Spray
void Spray(LE *a_element_sprayed, TTypesOfPesticideCategory a_type)
Definition: pesticide.cpp:854
PesticideEvent
Definition: pesticide.h:61
Pesticide::m_daily_spray_queue
vector< vector< PesticideEvent * > > m_daily_spray_queue
List lists of landscape elements, which was sprayed on a given day. One for each PPP we track.
Definition: pesticide.h:200
Diffusor::m_fraction
double m_fraction
Definition: pesticide.h:86
Diffusor::Getdy
int Getdy()
Definition: pesticide.h:98
PesticideMap::~PesticideMap
~PesticideMap()
Definition: pesticide.cpp:812
PesticideMap::m_startyear
int m_startyear
first simultion year to record
Definition: pesticide.h:383
Diffusor::Getdx
int Getdx()
Definition: pesticide.h:97
Pesticide::m_pest_map_main
vector< vector< double > > m_pest_map_main
Definition: pesticide.h:179
Landscape
The landscape class containing all environmental and topographical data.
Definition: landscape.h:112
Pesticide::GetAnythingToDecay
bool GetAnythingToDecay(PlantProtectionProducts a_ppp)
Definition: pesticide.h:228
PesticideMap::m_Rastermap
RasterMap * m_Rastermap
pointer to the landscape map
Definition: pesticide.h:397
PesticideMap::DumpPMapI
bool DumpPMapI()
Definition: pesticide.h:403
PesticideMap::DumpPMapH
bool DumpPMapH()
Definition: pesticide.h:404
tole_Pond
Definition: tole_declaration.h:101
Diffusor::m_dy
int m_dy
Definition: pesticide.h:85
Pesticide::m_something_to_decay
bool m_something_to_decay[ppp_foobar]
Speed hack. Only actually run the daily decay routine on the pesticide map if and only if we are sure...
Definition: pesticide.h:146
Pesticide::TwinMapClear
void TwinMapClear(int a_minx, int a_miny, int a_maxx, int a_maxy)
Definition: pesticide.cpp:181
Diffusor::SetFraction
void SetFraction(double a_frac)
Definition: pesticide.h:95
Diffusion_mask
vector< Diffusor * > Diffusion_mask
The grid of diffusors for each point in a polygon that is sprayed.
Definition: pesticide.h:103
Pesticide::m_NoPPPs
int m_NoPPPs
The number of active PPPs to track.
Definition: pesticide.h:149
Pesticide::DiffusionMaskInit
void DiffusionMaskInit(void)
Definition: pesticide.cpp:325
Pesticide::DailyQueueAdd
void DailyQueueAdd(LE *a_element_sprayed, double a_amount, PlantProtectionProducts a_ppp)
Definition: pesticide.cpp:140
PesticideMap::m_OurLandscape
Landscape * m_OurLandscape
pointer to the landscape
Definition: pesticide.h:395
Pesticide::MainMapDecay
void MainMapDecay(PlantProtectionProducts a_ppp)
Definition: pesticide.cpp:469
TTypesOfPesticideCategory
TTypesOfPesticideCategory
Definition: landscape.h:63
Pesticide::m_pest_daily_decay_frac
double m_pest_daily_decay_frac
Definition: pesticide.h:163
Pesticide::m_land
RasterMap * m_land
Definition: pesticide.h:168
Pesticide::DailyQueueProcess
void DailyQueueProcess(PlantProtectionProducts a_ppp)
Definition: pesticide.cpp:147
tole_River
Definition: tole_declaration.h:65
PesticideMap::m_pmap_insecticides
vector< double > * m_pmap_insecticides
insecticide map data
Definition: pesticide.h:377
LE
Definition: elements.h:81
Pesticide::DiffusionFunction
double DiffusionFunction(double a_dist_meters)
Definition: pesticide.cpp:442
Pesticide::m_prop
double m_prop
Definition: pesticide.h:156
Pesticide::m_pest_map_width
unsigned int m_pest_map_width
The width of one map in cellsize resolution.
Definition: pesticide.h:186
PesticideEvent::m_amount
double m_amount
Definition: pesticide.h:67
PesticideMap::m_typeofmap
bool m_typeofmap
true if using test pesticide, false for general pesticides
Definition: pesticide.h:393
Landscape::SupplyPesticideCell
int SupplyPesticideCell(int a_polyref)
Definition: landscape.h:1468
Pesticide::Pesticide
Pesticide(RasterMap *a_land, Landscape *a_map)
Definition: pesticide.cpp:526
TTypesOfLandscapeElement
TTypesOfLandscapeElement
Definition: tole_declaration.h:36
Pesticide::m_corr_y
double m_corr_y
Definition: pesticide.h:158
Pesticide::m_RainWashoffFactor
double m_RainWashoffFactor[10000]
a structure to hold pre-calculated pesticide rain wash off factor (Rw)
Definition: pesticide.h:191
Pesticide::m_corr_x
double m_corr_x
Definition: pesticide.h:157
Pesticide::m_pest_map_twin
double * m_pest_map_twin
The complete pesticide map.
Definition: pesticide.h:182
Pesticide::m_wind
int m_wind
Definition: pesticide.h:159
PesticideMap
Definition: pesticide.h:369
RasterMap
Definition: rastermap.h:40
Pesticide::m_pest_daily_decay_frac_Soil
double m_pest_daily_decay_frac_Soil
Definition: pesticide.h:165
Diffusor::m_dx
int m_dx
Definition: pesticide.h:84
Pesticide::TwinMapSprayCorrectBorders
void TwinMapSprayCorrectBorders(void)
Definition: pesticide.cpp:230
Pesticide::m_pest_daily_decay_frac_Veg
double m_pest_daily_decay_frac_Veg
Definition: pesticide.h:164
Diffusor::Diffusor
Diffusor(int a_dx, int a_dy, double a_fraction)
Definition: pesticide.h:89
PesticideMap::DumpPMap
bool DumpPMap(vector< double > *a_map)
Definition: pesticide.cpp:824
Pesticide::DiffusionMaskInitTest
void DiffusionMaskInitTest(void)
Definition: pesticide.cpp:640
Pesticide::m_map
Landscape * m_map
Definition: pesticide.h:171
Pesticide::CalcRainWashOffFactors
void CalcRainWashOffFactors()
Pre-calculates the constants required for rain wash off with increasing rainfall and stores this in m...
Definition: pesticide.cpp:599
Pesticide::m_pest_map_height
unsigned int m_pest_map_height
The height of one map in cellsize resolution.
Definition: pesticide.h:188
Pesticide::TwinMapSpray
void TwinMapSpray(LE *a_element_spryaed, double a_amount, int a_minx, int a_miny, int a_maxx, int a_maxy)
Definition: pesticide.cpp:194
PesticideMap::m_pmap_height
int m_pmap_height
based on cellsize the height of the map
Definition: pesticide.h:391
PesticideMap::m_pmap_herbicides
vector< double > * m_pmap_herbicides
fungicide map data
Definition: pesticide.h:381
g_pest
class Pesticide * g_pest
Definition: pesticide.cpp:92
Pesticide::SavePPM
bool SavePPM(double *a_map, int a_beginx, int a_width, int a_beginy, int a_height, char *a_filename)
Definition: pesticide.cpp:673