Skip to content

Commit 1c17cbb

Browse files
add moisture analysis
1 parent 2d79e54 commit 1c17cbb

File tree

10 files changed

+99
-15
lines changed

10 files changed

+99
-15
lines changed

data/moisture.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
80,1.830057

include/Cell.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class cell
1010
cellState current_state_;
1111
std::shared_ptr<const Wind> wind_state_;
1212
std::unique_ptr<Fire> fire_state_;
13+
int biomass_;
1314
double altitude_;
1415

1516
public:
@@ -39,6 +40,9 @@ class cell
3940
int getAltitude() const;
4041
~cell();
4142

43+
void setBiomass(const int biomass);
44+
int getBiomass() const;
45+
4246
const Fire* getFireInCell() const;
4347
};
4448
/// @brief this function make a proper assignment wind to cell. Also it follows invariants in wind

include/Math.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,22 @@ class Math1 final : public Math
8383
mutable std::unordered_map<SlopeMetaData, double> slope_result_;
8484
mutable std::map<double, int> result_wind_;
8585
mutable std::map<int, int> result_overall;
86-
mutable clock_t slope_timer;
86+
mutable clock_t slope_timer;
8787
mutable int slope_counter;
8888

8989
mutable clock_t wind_timer_;
9090
mutable int wind_counter_;
9191
mutable std::unordered_map<WindMetaData, double> wind_result_;
92+
93+
mutable clock_t moisture_timer_;
94+
mutable int moisture_counter_;
95+
mutable std::unordered_map<int, double> moisture_result_;
9296

9397
double calculateKoef(float windSpeed, double slopeAngleRad) const;
9498
double calculateWindKoef(const cell *c, directions InvestigatedDirection) const;
9599
double calculateGroundSlopeKoef(directions InvestigatedDirection, int altitudeDifference) const;
100+
double calculateBiomassKoef(const cell *c) const;
101+
double calculateMoistureKoeff(int moisture_percentage) const;
96102

97103
public:
98104
bool willSpread(const cell *c, directions InvestigatedDirection, int altitudeDifference) const override;

include/Wind.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ class Wind
66
private:
77
directions direction_;
88
float wind_speed_;
9+
int moisture_percentage_;
910
public:
10-
Wind(directions initialDirection, float w);
11+
Wind(directions initialDirection, float w, int moisture_percentage);
1112
~Wind();
1213
directions getWindDirection() const;
1314
float getWindSpeed() const;
15+
int getMoistureCoeff() const;
1416

1517
/// @deprecated now calculates over math module
1618
/// @brief calculate the wind coeff based on mathematical model

src/Cell.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,13 @@ cell::~cell()
111111
{
112112
setWindToCell(this, nullptr);
113113
}
114+
115+
void cell::setBiomass(const int biomass)
116+
{
117+
biomass_ = biomass;
118+
}
119+
120+
int cell::getBiomass() const
121+
{
122+
return biomass_;
123+
}

src/Math.cpp

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,54 @@ double Math1::calculateGroundSlopeKoef(directions InvestigatedDirection, int alt
8686
return result;
8787
}
8888

89+
double Math1::calculateBiomassKoef(const cell *c) const
90+
{
91+
int coeff = -1;
92+
switch (c->getBiomass())
93+
{
94+
case 0 ... 100:
95+
coeff = -0.4;
96+
break;
97+
case 101 ... 150:
98+
coeff = 0;
99+
break;
100+
case 151 ... 300:
101+
coeff = 0.3;
102+
break;
103+
default:
104+
throw std::invalid_argument("received strange value in biomass");
105+
break;
106+
}
107+
return double(1+coeff);
108+
}
109+
110+
double Math1::calculateMoistureKoeff(int moisture_percentage) const
111+
{
112+
clock_t start = clock();
113+
114+
auto a = moisture_result_[moisture_percentage];
115+
if (a != 0){
116+
clock_t per_iteration = clock() - start;
117+
moisture_timer_ = per_iteration+moisture_timer_;
118+
moisture_counter_++;
119+
return moisture_result_[moisture_percentage];
120+
};
121+
122+
double result = 2 * exp(-0.111 * moisture_percentage / 100);
123+
moisture_result_[moisture_percentage] = result;
124+
clock_t per_iteration = clock() - start;
125+
moisture_timer_ = per_iteration+moisture_timer_;
126+
moisture_counter_++;
127+
return result;
128+
}
129+
89130
bool Math1::willSpread(const cell *c, directions InvestigatedDirection, int altitudeDifference) const
90131
{
91132
double fireKoeff = calculateWindKoef(c, InvestigatedDirection);
92133
double slopeKoeff = calculateGroundSlopeKoef(InvestigatedDirection, altitudeDifference);
93-
int result = fireKoeff * slopeKoeff * 100 - 40;
134+
double biomassKoeff = calculateBiomassKoef(c);
135+
double moistureKoeff = calculateMoistureKoeff(c->getWind().get()->getMoistureCoeff());
136+
int result = fireKoeff * slopeKoeff * biomassKoeff * moistureKoeff * 100 - 40;
94137
result_overall[result]++;
95138
return result + (rand() % 100) > ignitionPercentage();
96139
}
@@ -108,6 +151,7 @@ bool to_bool(std::string const &s)
108151
}
109152

110153
Math1::Math1()
154+
:slope_timer(0), slope_counter(0), wind_timer_(0), wind_counter_(0), moisture_timer_(0), moisture_counter_(0)
111155
{
112156
std::ifstream csvFile{"data/slope.csv"};
113157
std::string row;
@@ -139,16 +183,26 @@ Math1::Math1()
139183
wind_result_[WindMetaData{atoi(cols[0].c_str()), a}] = atof(cols[2].c_str());
140184
}
141185
csvFile2.close();
142-
slope_counter = 0;
143-
slope_timer = 0;
144-
wind_counter_ = 0;
145-
wind_timer_ = 0;
186+
std::ifstream csvFile3{"data/moisture.csv"};
187+
while (std::getline(csvFile3, row))
188+
{
189+
std::stringstream rowStream(row);
190+
std::string col;
191+
std::vector<std::string> cols;
192+
while (std::getline(rowStream, col, ','))
193+
{
194+
cols.push_back(col);
195+
}
196+
moisture_result_[atoi(cols[0].c_str())] = atof(cols[1].c_str());
197+
}
198+
csvFile3.close();
146199
}
147200

148201
Math1::~Math1()
149202
{
150203
printf("total time %f, overall counts %d\n", (double)(slope_timer), slope_counter);
151204
printf("total time %f, overall counts %d\n", (double)(wind_timer_), wind_counter_);
205+
printf("total time %f, overall counts %d\n", (double)(moisture_timer_), moisture_counter_);
152206
// printf("slope profiling\n");
153207
// std::map<int, int> slope_distribution;
154208

src/ProfilingDecorator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <iostream>
44

55
ProfilingDecorator::ProfilingDecorator(Math *formula)
6-
: primary_class_(formula), counter_(0), positive_counter_(0)
6+
: primary_class_(formula), positive_counter_(0), counter_(0)
77
{
88
}
99

src/Wind.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
constexpr double pi() { return std::atan(1) * 4; }
55

6-
Wind::Wind(directions initialDirection, float w)
7-
: direction_(initialDirection), wind_speed_(w)
6+
Wind::Wind(directions initialDirection, float w, int moisture_percentage)
7+
: direction_(initialDirection), wind_speed_(w), moisture_percentage_(moisture_percentage)
88
{
99
}
1010

@@ -18,6 +18,11 @@ float Wind::getWindSpeed() const
1818
return this->wind_speed_;
1919
}
2020

21+
int Wind::getMoistureCoeff() const
22+
{
23+
return moisture_percentage_;
24+
}
25+
2126
Wind::~Wind()
2227
{
2328
}

src/main.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ int main()
2727
CellStorage s = CellStorage(&formulaDecorator);
2828
auto xRange = std::make_pair<int, int>(0, getXArea());
2929
auto yRange = std::make_pair<int, int>(0, getYArea()/2);
30-
auto w = std::make_shared<const Wind>(directions::SouthWest, float(5.0));
30+
auto w = std::make_shared<const Wind>(directions::SouthWest, float(5.0), 80); //Surgut
31+
// auto w = std::make_shared<const Wind>(directions::NorthEast, float(2.0)); // Vanderhoof
32+
3133
s.setWindToArea(xRange, yRange, w);
3234

3335
yRange = std::make_pair<int, int>(getYArea()/2, getYArea());
34-
w = std::make_shared<const Wind>(directions::SouthEast, float(2.0));
36+
w = std::make_shared<const Wind>(directions::SouthEast, float(2.0), 80);
3537
s.setWindToArea(xRange, yRange, w);
3638

3739
for (size_t i = 0; i < 10; i++)

tests/test_main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void test_Cell(){
3131
assert(c.getState() == cellState::Artificial);
3232

3333
float wS = 1.0;
34-
auto w = std::make_shared<const Wind>(directions::North, wS);
34+
auto w = std::make_shared<const Wind>(directions::North, wS, 0.1);
3535
c.setWind(w);
3636
assert(c.getWind().get() == w.get());
3737
}
@@ -55,7 +55,7 @@ void test_CellStorage_setWindToArea(){
5555
CellStorage s = CellStorage(&formula);
5656
std::pair<int, int> p = std::make_pair<int, int>(4, 5);
5757
float wS = 1.0;
58-
auto w = std::make_shared<const Wind>(directions::North, wS);
58+
auto w = std::make_shared<const Wind>(directions::North, wS, 0.1);
5959

6060
assert(!s.checkAndGetCell(4, 4)->getWind());
6161

@@ -87,7 +87,7 @@ void test_CellStorage(){
8787

8888
void test_Wind(){
8989
float wS = 1.0;
90-
Wind w = Wind(directions::North, wS);
90+
Wind w = Wind(directions::North, wS, 0.1);
9191
assert(w.getWindDirection() == directions::North);
9292

9393
assert(w.angleBetweenDirections(directions::North) == 0);

0 commit comments

Comments
 (0)