ALMaSS Rabbit ODdox  1.00
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 
32 //#define __DETAILED_PESTICIDE_FATE
33 
34 // Side length of a square in the pesticide map is given by this parameter
35 // as a power of two. Ie. 3 will give a side length of 8 main map units
36 // as 2^3 equals 8, which must also be set below
37 //
38 // Unfortunately it would be a huge performance penalty to have to make
39 // this a runtime configuration variable, it has to be compiled in.
40 // Standard rough settings here:
41 
42 #define PEST_GRIDSIZE_POW2 0
43 #define PEST_GRIDSIZE 1
44 #define PEST_GRIDAREA 1 // i.e. PEST_GRIDSIZE^2
45 /*
46 #define PEST_GRIDSIZE_POW2 2
47 #define PEST_GRIDSIZE 4
48 #define PEST_GRIDAREA 16 // i.e. PEST_GRIDSIZE^2
49 */
50 
51 
52 // Consider undef for production code, no speed penalty if defined.
53 #define PEST_DEBUG
54 
55 // Check for water bodies when asking for pesticide concentration
56 // at main map coords x,y? Speed vs. reality tradeoff. You
57 // probably want this defined.
58 //#define PEST_WATER_CHECK
59 
60 
61 extern class Pesticide *g_pest;
62 
63 
65 {
70  double m_amount;
71 
72  PesticideEvent( LE* a_sprayed_elem, double a_amount ) {
73  m_sprayed_elem = a_sprayed_elem;
74  m_amount = a_amount;
75  }
76 };
77 
78 class Diffusor
79 {
85  int m_dx;
86  int m_dy;
87  double m_fraction;
88 
89 public:
90  Diffusor( int a_dx, int a_dy, double a_fraction ) {
91  m_dx = a_dx;
92  m_dy = a_dy;
93  m_fraction = a_fraction;
94  }
95 
96  void SetFraction(double a_frac) { m_fraction = a_frac; }
97  double GetFraction() { return m_fraction; }
98  int Getdx() { return m_dx; }
99  int Getdy() { return m_dy; }
100 
101 };
102 
104 typedef vector <Diffusor*> Diffusion_mask;
105 
106 
108 {
109  // Method call stack and ordering:
110  //
111  // Pesticide()
112  // DiffusionMaskInit()
113  // DiffusionFunction()
114  //
115  // Tick()
116  // MainMapDecay()
117  // DailyQueueProcess()
118  // TwinMapClear()
119  // TwinMapSpray()
120  // TwinMapSprayPixel()
121  // TwinMapSprayCorrectBorders()
122  // TwinMapCopyToMask()
123  // TwinMapDiffusion()
124  // DiffusionSprayPixel()
125  // ElementIsWater()
126  // TwinMapAddToMain()
127  // DailyQueueClear()
128  //
129  // SupplyPesticide()
130  // #ifdef PEST_WATER_CHECK
131  // ElementIsWater()
132  // #endif
133 
134  protected:
135  // Speed hack. Only actually run the daily decay routine on the
136  // pesticide map if and only if we are sure that there *is*
137  // some pesticide anywhere on the map for us to process.
138  //
139  // Cleared daily by MainMapDecay().
140  //
141  // Set to true when either adding pesticide to the map, or by the
142  // MainMapDecay() method itself, when it finds remains of pesticide
143  // anywhere on the pesticide map during its run.
145 
146  // Main map coordinates beyond which we have to use special
147  // proportional constants when adding new amounts of pesticide
148  // to the pesticide map.
151  double m_prop;
152  double m_corr_x;
153  double m_corr_y;
154  int m_wind;
155 
156  // Daily decay fraction, precalculated from the pesticide
157  // half life parameter, for general, vegetation and soil
161 
162  // Local copy of pointer to the main map object.
164 
165  // Local copy of pointer to the main landscape object.
167 
168  // The pesticide map itself, and its twins used when adding newly
169  // sprayed squares to the main map.
170 #ifdef __DETAILED_PESTICIDE_FATE
171  double *m_pest_map_vegcanopy;
172  double *m_pest_map_soil;
173 #else
175 #endif
177  unsigned int m_pest_map_size;
178  unsigned int m_pest_map_width;
179  unsigned int m_pest_map_height;
180 
182  double m_RainWashoffFactor[10000];
185 
188  Diffusion_mask m_diffusion_mask[4];
189 
190  // List of landscape elements, which was sprayed on a given day.
191  vector <PesticideEvent*> m_daily_spray_queue;
192 
193  void DailyQueueClear( void );
194  void DailyQueueProcess( void );
195 
196  bool ElementIsWater( int a_x, int a_y );
197 
198  void MainMapDecay( void );
199 
200  void TwinMapClear( int a_minx, int a_miny, int a_maxx, int a_maxy );
201  void TwinMapSpray( LE* a_element_spryaed, double a_amount, int a_minx, int a_miny, int a_maxx, int a_maxy );
202  void TwinMapSprayPixel( int a_large_map_x,
203  int a_large_map_y,
204  double a_fractional_amount );
205  void TwinMapSprayCorrectBorders( void );
206  //void TwinMapCopyToMask( int a_minx, int a_miny, int a_maxx, int a_maxy );
207  //void TwinMapAddToMain( void );
208  void TwinMapDiffusion( int a_minx, int a_miny, int a_maxx, int a_maxy, double a_cover );
209 
210  void DiffusionMaskInit( void );
211  double DiffusionFunction( double a_dist_meters );
212  void DiffusionSprayPixel( int a_x, int a_limit_x, int a_y, int a_limit_y, double a_amount, double a_cover );
214  void CalcRainWashOffFactors();
215 
216 public:
217  void Tick( void );
218  void DailyQueueAdd( LE* a_element_sprayed, double a_amount );
219  bool GetAnythingToDecay(){ return (m_something_to_decay); }
220  // Doesn't make sense for the pesticide map, as one cannot guarantee
221  // that all the area covered by a particular polygon has the
222  // same concentration of pesticide, but as an approximation we use the
223  // grid cell covering ValidX & ValidY for that polygon.
224  //
225 #ifdef __DETAILED_PESTICIDE_FATE
226  double SupplyPesticideS(int a_x, int a_y);
227  double SupplyPesticideP(int a_x, int a_y);
228  double SupplyPesticideS(int a_polyref);
229  double SupplyPesticideP(int a_polyref);
230 #else
231  double SupplyPesticide( int a_x, int a_y );
232  double SupplyPesticide(int a_polyref);
233 #endif
234 
235  Pesticide( RasterMap *a_land, Landscape *a_map );
236  virtual ~Pesticide( void );
237 
238 #ifdef PEST_DEBUG
239  /* *** For testing of the pesticide engine. */
240 
241  bool SavePPM( double *a_map,
242  int a_beginx, int a_width,
243  int a_beginy, int a_height,
244  char* a_filename );
245 
246  void DiffusionMaskInitTest( void );
247  //void Test( Landscape *a_map );
248 #endif
249 
250 };
251 
252 
253 
254 inline bool Pesticide::ElementIsWater( int a_x, int a_y )
255 {
256  TTypesOfLandscapeElement l_eletype =
257  m_map->SupplyElementType( a_x, a_y );
258 
259  if ( l_eletype == tole_Freshwater ||
260  l_eletype == tole_River ||
261  l_eletype == tole_Pond ||
262  l_eletype == tole_FishFarm ||
263  l_eletype == tole_Saltwater
264  )
265  return true;
266 
267  return false;
268 }
269 
270 
271 #ifdef __DETAILED_PESTICIDE_FATE
272 
273 inline double Pesticide::SupplyPesticideS(int a_x, int a_y)
274 {
280  int l_x = a_x >> PEST_GRIDSIZE_POW2;
281  int l_y = a_y >> PEST_GRIDSIZE_POW2;
282 
283  return m_pest_map_soil[l_y * m_pest_map_width + l_x];
284 }
285 
286 inline double Pesticide::SupplyPesticideP(int a_x, int a_y)
287 {
293  int l_x = a_x >> PEST_GRIDSIZE_POW2;
294  int l_y = a_y >> PEST_GRIDSIZE_POW2;
295 
296  return m_pest_map_vegcanopy[l_y * m_pest_map_width + l_x];
297 }
298 
299 inline double Pesticide::SupplyPesticideS(int a_ele)
300 {
305  int l_c = m_map->SupplyPesticideCell(a_ele);
306  return m_pest_map_soil[l_c];
307 }
308 
309 inline double Pesticide::SupplyPesticideP(int a_ele)
310 {
315  int l_c = m_map->SupplyPesticideCell(a_ele);
316  return m_pest_map_vegcanopy[l_c];
317 }
318 
319 #else
320 inline double Pesticide::SupplyPesticide(int a_x, int a_y)
321 {
327 #ifdef PEST_WATER_CHECK
328  if (ElementIsWater(a_x, a_y))
329  return 0.0;
330 #endif
331 
332  int l_x = a_x >> PEST_GRIDSIZE_POW2;
333  int l_y = a_y >> PEST_GRIDSIZE_POW2;
334 
335  return m_pest_map_main[l_y * m_pest_map_width + l_x];
336 }
337 
338 inline double Pesticide::SupplyPesticide(int a_ele)
339 {
340  // This is an approximation because we have no idea about the actual variation
341  // in pesticide concentrations within the polygon.
342  int l_c = m_map->SupplyPesticideCell(a_ele);
343  return m_pest_map_main[l_c];
344 }
345 
346 #endif
347 
348 inline void Pesticide::TwinMapSprayPixel(int a_large_map_x,
349  int a_large_map_y,
350  double a_fractional_amount)
351 {
352  int l_x = a_large_map_x >> PEST_GRIDSIZE_POW2;
353  int l_y = a_large_map_y >> PEST_GRIDSIZE_POW2;
354 
355  m_pest_map_twin[l_y * m_pest_map_width + l_x] +=
356  a_fractional_amount;
357 }
358 
359 #endif // PESTICIDE_H
The landscape class containing all environmental and topographical data.
Definition: landscape.h:109
vector< Diffusor * > Diffusion_mask
The grid of diffusors for each point in a polygon that is sprayed.
Definition: pesticide.h:104
unsigned int m_pest_map_width
Definition: pesticide.h:178
double m_pest_daily_decay_frac_Soil
Definition: pesticide.h:160
int Getdy()
Definition: pesticide.h:99
bool GetAnythingToDecay()
Definition: pesticide.h:219
LE * m_sprayed_elem
Definition: pesticide.h:69
RasterMap * m_land
Definition: pesticide.h:163
int m_dx
Definition: pesticide.h:85
int m_x_excess
Definition: pesticide.h:149
double SupplyPesticide(int a_x, int a_y)
Definition: pesticide.h:320
#define PEST_GRIDSIZE_POW2
Turns on code for detailed pesticide fate handling.
Definition: pesticide.h:42
double m_prop
Definition: pesticide.h:151
double * m_pest_map_twin
Definition: pesticide.h:176
Definition: elements.h:81
double m_pest_daily_decay_frac_Veg
Definition: pesticide.h:159
int m_wind
Definition: pesticide.h:154
bool ElementIsWater(int a_x, int a_y)
Definition: pesticide.h:254
double m_fraction
Definition: pesticide.h:87
PesticideEvent(LE *a_sprayed_elem, double a_amount)
Definition: pesticide.h:72
void SetFraction(double a_frac)
Definition: pesticide.h:96
vector< PesticideEvent * > m_daily_spray_queue
Definition: pesticide.h:191
unsigned m_rainfallcategory
Daily rainfall saved here * 100 to use as an indext to the Pesticide::m_RainWashoffFactor array - an ...
Definition: pesticide.h:184
double m_amount
Definition: pesticide.h:70
int m_y_excess
Definition: pesticide.h:150
double m_corr_x
Definition: pesticide.h:152
int Getdx()
Definition: pesticide.h:98
void TwinMapSprayPixel(int a_large_map_x, int a_large_map_y, double a_fractional_amount)
Definition: pesticide.h:348
unsigned int m_pest_map_size
Definition: pesticide.h:177
bool m_something_to_decay
Definition: pesticide.h:144
class Pesticide * g_pest
Definition: pesticide.cpp:70
unsigned int m_pest_map_height
Definition: pesticide.h:179
Landscape * m_map
Definition: pesticide.h:166
double m_pest_daily_decay_frac
Definition: pesticide.h:158
TTypesOfLandscapeElement
double GetFraction()
Definition: pesticide.h:97
Diffusor(int a_dx, int a_dy, double a_fraction)
Definition: pesticide.h:90
double * m_pest_map_main
Definition: pesticide.h:174
int m_dy
Definition: pesticide.h:86
double m_corr_y
Definition: pesticide.h:153