Skip to content

Commit 53ea113

Browse files
committed
Close TerraME#16. Adding Dam model.
1 parent 4e0d552 commit 53ea113

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
33+
if (self.countYear == true) then
34+
if (self.timer:getTime()%12 == 0) then -- every each year increments current year
35+
self.currentYear = self.currentYear + 1
36+
if (self.currentYear == self.changedYear) then -- when changedYear equal to currentYear
37+
self.inFlow1 = self.inFlow1/2
38+
self.inFlow2 = self.inFlow2/2
39+
end
40+
end
41+
end
42+
end,
43+
44+
init = function (self)
45+
self.chart = Chart{
46+
target = self,
47+
select = "water"
48+
}
49+
50+
self.timer = Timer{
51+
Event{action = self, priority = 'high'},
52+
Event{period = 12, action = function()
53+
self.water = self.water + self.inFlow1 -- first season in first semester
54+
end},
55+
Event{start = 7, period = 12, action = function()
56+
self.water = self.water + self.inFlow2 -- second season in second semester
57+
end},
58+
Event{start = 12, period = 12, action = function()
59+
self.consumePerPerson = self.consumePerPerson*(1 + self.growth) -- every year add 5%
60+
end},
61+
Event{action = self.chart}
62+
}
63+
64+
end
65+
}
66+

tests/Dam.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
end,
18+
}
19+

0 commit comments

Comments
 (0)