ALMaSS Hare ODDox  1.1
The hare model description following ODdox protocol
PermanentGrassTussocky.cpp
Go to the documentation of this file.
1 //
2 // PermanentGrassTussocky.cpp
3 //
4 /*
5 *******************************************************************************************************
6 Copyright (c) 2011, Christopher John Topping, University of Aarhus
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 #include "../../Landscape/ls.h"
29 #include "../../Landscape/cropprogs/PermanentGrassTussocky.h"
30 
31 
32 extern CfgFloat cfg_ins_app_prop1;
33 extern CfgFloat cfg_herbi_app_prop;
34 extern CfgFloat cfg_fungi_app_prop1;
35 extern CfgFloat cfg_greg_app_prop;
36 
37 bool PermanentGrassTussocky::Do( Farm *a_farm, LE *a_field, FarmEvent *a_ev )
38 {
39  m_farm = a_farm;
40  m_field = a_field;
41  m_ev = a_ev;
42 
43  bool done = false;
44 
45  switch ( m_ev->m_todo ) {
46  case pgt_start:
47  {
48  PGT_CUT_DATE =0;
50  // Set up the date management stuff
51  m_last_date=g_date->DayInYear(1,10);
52  // Start and stop dates for all events after harvest
53  int noDates= 1;
54  // Start and stop dates for all events after harvest
55  m_field->SetMDates(0,0,g_date->DayInYear(15,9));
56  // 0,0 determined by harvest date - used to see if at all possible
57  m_field->SetMDates( 1, 0, g_date->DayInYear(1,10) );
58  // Check the next crop for early start, unless it is a spring crop
59  // in which case we ASSUME that no checking is necessary!!!!
60  // So DO NOT implement a crop that runs over the year boundary
61 
62  //new if: do the check only for non-optimising farms and if year>0. (030713 - m_rotation used only in the hidden year, so I modified the condition from >7 to >0)
63  int d1;
64  if(!(m_farm->GetType() == tof_OptimisingFarm && g_date->GetYearNumber()>0)){
65 
66  if (m_ev->m_startday>g_date->DayInYear(1,7))
67  {
68  if (m_field->GetMDates(0,0) >=m_ev->m_startday)
69  {
70  g_msg->Warn( WARN_BUG, "PermanentGrassTussocky::Do(): "
71  "Harvest too late for the next crop to start!!!", "" );
72  exit( 1 );
73  }
74  // Now fix any late finishing problems
75  for (int i=0; i<noDates; i++) {
76  if(m_field->GetMDates(0,i)>=m_ev->m_startday) {
77  m_field->SetMDates(0,i,m_ev->m_startday-1); //move the starting date
78  }
79  if(m_field->GetMDates(1,i)>=m_ev->m_startday){
80  m_field->SetMConstants(i,0);
81  m_field->SetMDates(1,i,m_ev->m_startday-1); //move the finishing date
82  }
83  }
84  }
85  // Now no operations can be timed after the start of the next crop.
86 
87  int today=g_date->Date();
88  d1 = g_date->OldDays() + m_first_date+365; // Add 365 for spring crop
89  if (today > d1)
90  {
91  // Yes too late - should not happen - raise an error
92  g_msg->Warn( WARN_BUG, " PermanentGrassTussocky::Do(): "
93  "Crop start attempt after last possible start date", "" );
94  exit( 1 );
95  }
96  }//if
97 
98  // Reinit d1 to first possible starting date.
99  d1 = g_date->OldDays()+m_first_date;;
100  if ( ! m_ev->m_first_year ) d1+=365; // Add 365 for spring crop (not 1st yr)
101  if ( g_date->Date() > d1 ) {
102  d1 = g_date->Date();
103  }
104  // OK, let's go.
105  m_field->SetLastSownVeg( m_field->GetVegType() ); //Force last sown, needed for goose habitat classification
106 
107  ChooseNextCrop (1);
108 
109  SimpleEvent( d1, pgt_cattle_out1, false );
110  }
111  break;
112 
113  case pgt_cattle_out1:
114  if (m_field->GetMConstants(0)==0) {
115  if (!m_farm->CattleOutLowGrazing( m_field, 0.0, -1)) { //raise an error
116  g_msg->Warn( WARN_BUG, "PermanentGrassTussocky::Do(): failure in 'CattleOutLowGrazing' execution", "" );
117  exit( 1 );
118  }
119  }
120  else {
121  if (!m_farm->CattleOutLowGrazing( m_field, 0.0, m_field->GetMDates(1,0) - g_date->DayInYear())) {
122  SimpleEvent( g_date->Date() + 1, pgt_cattle_out1, true );
123  break;
124  }
125  }
126  SimpleEvent( g_date->Date() + m_field->GetMConstants(0), pgt_cattle_is_out, false );
127  break;
128 
129  case pgt_cattle_out2:
130  if (( m_ev->m_lock || m_farm->DoIt( 33 )) || (PGT_FERTI_DATE!=0)) {
131  if (m_field->GetMConstants(0)==0) {
132  if (!m_farm->CattleOutLowGrazing( m_field, 0.0, -1)) { //raise an error
133  g_msg->Warn( WARN_BUG, "PermanentGrassTussocky::Do(): failure in 'CattleOutLowGrazing' execution", "" );
134  exit( 1 );
135  }
136  }
137  else {
138  if (!m_farm->CattleOutLowGrazing( m_field, 0.0, m_field->GetMDates(1,0) - g_date->DayInYear())) {
139  SimpleEvent( g_date->Date() + 1, pgt_cattle_out2, true );
140  break;
141  }
142  }
143  // Success
144  SimpleEvent( g_date->Date() + m_field->GetMConstants(0), pgt_cattle_is_out, false );
145  break;
146  }
147  //Don't graze - but don't end too early;
148  SimpleEvent( g_date->OldDays() +g_date->DayInYear(2,7), pgt_wait, false );
149  break;
150 
151  case pgt_wait:
152  done=true;
153  break;
154 
155  case pgt_cattle_is_out:
156  if (m_field->GetMConstants(0)==0) {
157  if (!m_farm->CattleIsOutLow( m_field, 0.0, -1, m_field->GetMDates(1,0))) { //raise an error
158  //added 28.08 - issue a warning only if we fail on the last day that this can be done, i.e. MDate
159  if(g_date->Date() == m_field->GetMDates(1,0)){
160  g_msg->Warn( WARN_BUG, "PermanentGrassTussocky::Do(): failure in 'CattleIsOutLow' execution", "" );
161  exit( 1 );
162  }
163  }
164  }
165  else {
166  if (!m_farm->CattleIsOutLow( m_field, 0.0, m_field->GetMDates(1,0) - g_date->DayInYear(), m_field->GetMDates(1,0))) {
167  SimpleEvent( g_date->Date() + 1, pgt_cattle_is_out, true );
168  break;
169  }
170  }
171  // if they come in then send them out if too early
172  if (g_date->DayInYear()<g_date->DayInYear(10,9))
173  {
174  SimpleEvent( g_date->Date()+1, pgt_cattle_out2, true );
175  break;
176  }
177  // END MAIN THREAD
178  done=true;
179  break;
180 
181  default:
182  g_msg->Warn( WARN_BUG, "PermanantGrassTussocky::Do(): "
183  "Unknown event type! ", "" );
184  exit( 1 );
185  }
186 
187  return done;
188 }
189 
190 
FarmEvent::m_lock
bool m_lock
Definition: farm.h:465
FarmEvent
A struct to hold the information required to trigger a farm event.
Definition: farm.h:463
Farm::DoIt
bool DoIt(double a_probability)
Return chance out of 0 to 100.
Definition: farm.cpp:800
pgt_cattle_out2
Definition: PermanentGrassTussocky.h:38
FarmEvent::m_first_year
bool m_first_year
Definition: farm.h:467
tof_OptimisingFarm
Definition: farm.h:273
PermanentGrassTussocky::Do
bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
Definition: PermanentGrassTussocky.cpp:37
Farm::GetType
TTypesOfFarm GetType(void)
Definition: farm.h:901
cfg_fungi_app_prop1
CfgFloat cfg_fungi_app_prop1
Crop::m_first_date
int m_first_date
Definition: farm.h:540
FarmEvent::m_startday
int m_startday
Definition: farm.h:466
cfg_herbi_app_prop
CfgFloat cfg_herbi_app_prop
Farm::CattleIsOutLow
virtual bool CattleIsOutLow(LE *a_field, double a_user, int a_days, int a_max)
Generate a 'cattle_out_low' event for every day the cattle are on a_field.
Definition: farmfuncs.cpp:2018
Crop::SimpleEvent
void SimpleEvent(long a_date, int a_todo, bool a_lock)
Adds an event to this crop management.
Definition: farm.cpp:307
Farm::CattleOutLowGrazing
virtual bool CattleOutLowGrazing(LE *a_field, double a_user, int a_days)
Start a extensive grazing event on a_field today.
Definition: farmfuncs.cpp:1944
PGT_FERTI_DATE
#define PGT_FERTI_DATE
Definition: PermanentGrassTussocky.h:33
pgt_cattle_out1
Definition: PermanentGrassTussocky.h:37
pgt_wait
Definition: PermanentGrassTussocky.h:40
PGT_CUT_DATE
#define PGT_CUT_DATE
Definition: PermanentGrassTussocky.h:32
pgt_cattle_is_out
Definition: PermanentGrassTussocky.h:39
Crop::m_farm
Farm * m_farm
Definition: farm.h:537
Crop::m_field
LE * m_field
Definition: farm.h:538
FarmEvent::m_todo
int m_todo
Definition: farm.h:469
Crop::m_last_date
int m_last_date
Definition: farm.h:542
Farm
The base class for all farm types.
Definition: farm.h:767
pgt_start
Definition: PermanentGrassTussocky.h:36
cfg_greg_app_prop
CfgFloat cfg_greg_app_prop
Crop::ChooseNextCrop
void ChooseNextCrop(int a_no_dates)
Chooses the next crop to grow in a field.
Definition: farm.cpp:318
Crop::m_ev
FarmEvent * m_ev
Definition: farm.h:539
cfg_ins_app_prop1
CfgFloat cfg_ins_app_prop1