Skip to content

Commit 12d3e06

Browse files
committed
Close TerraME#16 Adding Dam model
1 parent 4e0d552 commit 12d3e06

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

log/Dam-chart-1.bmp

879 KB
Binary file not shown.

log/Dam-chart-2.bmp

879 KB
Binary file not shown.

lua/Dam.lua

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
--- A water in the dam model.
2+
-- @arg data.water The initial stock of water measured in m³. The initial value is 5,000,000,000.
3+
-- @arg data.inFlow1 The flow of water into the dam each first season. The default is 2e9.
4+
-- @arg data.population The total amount of inhabitants. The default value is 1e5.
5+
-- @arg data.consumePerPerson The total amount of water per inhabitant. The default value is 10.
6+
-- @arg data.kWh2cubicMeters The total amount of kWh produced by cubic meters. The default value is 100.
7+
-- @arg data.growth The comsumption amount of kWh produced by cubic meters. The default value is 100.
8+
-- @arg data.countYear The flag in which defines whether count or not the years . The default value is false.
9+
-- @arg data.changedYear The year in which water values change. The default value is 1970.
10+
-- @arg data.finalTime The final time of the simulation in months. The default value is 1000.
11+
Dam = Model{
12+
water = 5e9,
13+
inFlow1 = 2e9,
14+
inFlow2 = 1.5e9,
15+
population = 1e5,
16+
consumePerPerson = 10,
17+
kWh2cubicMeters = 100,
18+
growth = 0.05,
19+
countYear = false,
20+
changedYear= 1970,
21+
currentYear = 1950,
22+
finalTime = 1000,
23+
execute = function(self) -- each time step
24+
local outFlow = self.population * self.consumePerPerson * self.kWh2cubicMeters -- update outflow
25+
self.water = self.water - outFlow -- update water
26+
27+
if self.water <= 0 then -- water amount less than zero
28+
self.water = 0
29+
elseif self.water > 5e9 then -- water amount more than dam capacity
30+
self.water = 5e9
31+
end
32+
if (self.countYear == true) then
33+
if (self.timer:getTime()%12 == 0) then -- every each year increments current year
34+
self.currentYear = self.currentYear + 1
35+
if (self.currentYear == self.changedYear) then -- when changedYear equal to currentYear
36+
self.inFlow1 = self.inFlow1/2
37+
self.inFlow2 = self.inFlow2/2
38+
end
39+
end
40+
end
41+
end,
42+
init = function (self)
43+
self.chart = Chart{
44+
target = self,
45+
select = "water"
46+
}
47+
48+
self.timer = Timer{
49+
Event{action = self, priority = 'high'},
50+
Event{period = 12, action = function()
51+
self.water = self.water + self.inFlow1 -- first season in first semester
52+
end},
53+
Event{start = 7, period = 12, action = function()
54+
self.water = self.water + self.inFlow2 -- second season in second semester
55+
end},
56+
Event{start = 12, period = 12, action = function()
57+
self.consumePerPerson = self.consumePerPerson*(1 + self.growth) -- every year add 5%
58+
end},
59+
Event{action = self.chart}
60+
}
61+
end
62+
}
63+

tests/Dam.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- Test file for Dam.lua
2+
-- Author: Gilberto Camara and Pedro R. Andrade
3+
4+
return{
5+
Dam = function(unitTest)
6+
local model = Dam{}
7+
8+
model:run()
9+
10+
unitTest:assertSnapshot(model.chart, "Dam-chart-1.bmp")
11+
12+
model = Dam{countYear = true}
13+
14+
model:run()
15+
16+
unitTest:assertSnapshot(model.chart, "Dam-chart-2.bmp")
17+
18+
end,
19+
}
20+

0 commit comments

Comments
 (0)