ALMaSS Hare ODDox  1.1
The hare model description following ODdox protocol
YoungForest.cpp
Go to the documentation of this file.
1 //
2 // YoungForest.cpp
3 //
4 /*
5 *******************************************************************************************************
6 Copyright (c) 2011, Christopher John Topping, Aarhus University
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/YoungForest.h"
30 
31 
32 bool YoungForestCrop::Do( Farm *a_farm, LE *a_field, FarmEvent *a_ev )
33 {
34  m_farm = a_farm;
35  m_field = a_field;
36  m_ev = a_ev;
37  bool done = false;
38  int d1;
39 
40  switch ( m_ev->m_todo ) {
41  case yf_start:
42  {
43  // Set up the date management stuff
44  m_last_date=g_date->DayInYear(1,9);
45  // Start and stop dates for all events after harvest
46  int noDates= 1;
47  m_field->SetMDates(0,0,g_date->DayInYear(1,9));
48  // Determined by harvest date - used to see if at all possible
49  m_field->SetMDates(1,0,g_date->DayInYear(1,9));
50  // Check the next crop for early start, unless it is a spring crop
51  // in which case we ASSUME that no checking is necessary!!!!F
52  // So DO NOT implement a crop that runs over the year boundary
53 
54  //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)
55  if(!(m_farm->GetType() == tof_OptimisingFarm && g_date->GetYearNumber()>0)){
56 
57  if (m_ev->m_startday>g_date->DayInYear(1,7))
58  {
59  if (m_field->GetMDates(0,0) >=m_ev->m_startday)
60  {
61  g_msg->Warn( WARN_BUG, "YoungForestCrop::Do(): "
62  "Harvest too late for the next crop to start!!!", "" );
63  exit( 1 );
64  }
65  // Now fix any late finishing problems
66  for (int i=0; i<noDates; i++) {
67  if(m_field->GetMDates(0,i)>=m_ev->m_startday) {
68  m_field->SetMDates(0,i,m_ev->m_startday-1); //move the starting date
69  }
70  if(m_field->GetMDates(1,i)>=m_ev->m_startday){
71  m_field->SetMConstants(i,0);
72  m_field->SetMDates(1,i,m_ev->m_startday-1); //move the finishing date
73  }
74  }
75  }
76  // Now no operations can be timed after the start of the next crop.
77 
78  int today=g_date->Date();
79  d1 = g_date->OldDays() + m_first_date+365; // Add 365 for spring crop
80  if (today > d1)
81  {
82  // Yes too late - should not happen - raise an error
83  g_msg->Warn( WARN_BUG, " YoungForestCrop::Do(): "
84  "Crop start attempt after last possible start date", "" );
85  exit( 1 );
86  }
87  }//if
88 
89  // Reinit d1 to first possible starting date.
90  d1 = g_date->OldDays()+m_first_date;;
91  if ( ! m_ev->m_first_year ) d1+=365; // Add 365 for spring crop (not 1st yr)
92  if ( g_date->Date() > d1 )
93  {
94  d1 = g_date->Date();
95  }
96  // OK, let's go.
97  SimpleEvent( d1, yf_do_nothing_start, false );
98  }
99  break;
100 
101  case yf_do_nothing_start:
102  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1, 9 ),
103  yf_do_nothing_stop, false );
104  break;
105 
106  case yf_do_nothing_stop:
107 
108  ChooseNextCrop (1);
109 
110  done = true;
111  break;
112 
113  default:
114  g_msg->Warn( WARN_BUG, "YoungForestCrop::Do(): "
115  "Unknown event type! ", "" );
116  exit( 1 );
117  }
118 
119  return done;
120 }
121 
122 
YoungForestCrop::Do
bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
Definition: YoungForest.cpp:32
FarmEvent
A struct to hold the information required to trigger a farm event.
Definition: farm.h:463
FarmEvent::m_first_year
bool m_first_year
Definition: farm.h:467
tof_OptimisingFarm
Definition: farm.h:273
yf_do_nothing_stop
Definition: YoungForest.h:45
Farm::GetType
TTypesOfFarm GetType(void)
Definition: farm.h:901
Crop::m_first_date
int m_first_date
Definition: farm.h:540
FarmEvent::m_startday
int m_startday
Definition: farm.h:466
Crop::SimpleEvent
void SimpleEvent(long a_date, int a_todo, bool a_lock)
Adds an event to this crop management.
Definition: farm.cpp:307
yf_start
Definition: YoungForest.h:43
yf_do_nothing_start
Definition: YoungForest.h:44
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
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