diff --git a/description.lua b/description.lua index 0553e83..782b90e 100644 --- a/description.lua +++ b/description.lua @@ -1,4 +1,4 @@ -version = "0.8.3" +version = "0.8.5" license = "GPL" package = "sysdyn" depends = "terrame (>= 2.0), sci (>= 0.3)" diff --git a/log/Dam-chart-1.bmp b/log/Dam-chart-1.bmp new file mode 100644 index 0000000..f4f18e8 Binary files /dev/null and b/log/Dam-chart-1.bmp differ diff --git a/log/Dam-chart-2.bmp b/log/Dam-chart-2.bmp new file mode 100644 index 0000000..dc60147 Binary files /dev/null and b/log/Dam-chart-2.bmp differ diff --git a/log/random-walk.png b/log/random-walk.png index 00e4f99..fbaf73b 100644 Binary files a/log/random-walk.png and b/log/random-walk.png differ diff --git a/lua/Dam.lua b/lua/Dam.lua new file mode 100644 index 0000000..8cdcdac --- /dev/null +++ b/lua/Dam.lua @@ -0,0 +1,68 @@ +--- A water in the dam model. +-- @arg data.water The initial stock of water measured in m³. The initial value is 5,000,000,000. +-- @arg data.inFlow1 The flow of water into the dam each first season. The default is 2e9. +-- @arg data.inFlow2 The flow of water into the dam each second season. The default value is 1.5e9. +-- @arg data.currentYear The starting year. The default value is 1950. +-- @arg data.population The total amount of inhabitants. The default value is 1e5. +-- @arg data.consumePerPerson The total amount of water per inhabitant. The default value is 10. +-- @arg data.kWh2cubicMeters The total amount of kWh produced by cubic meters. The default value is 100. +-- @arg data.growth The comsumption amount of kWh produced by cubic meters. The default value is 100. +-- @arg data.countYear The flag in which defines whether count or not the years . The default value is false. +-- @arg data.changedYear The year in which water values change. The default value is 1970. +-- @arg data.finalTime The final time of the simulation in months. The default value is 1000. +Dam = Model{ + water = 5e9, + inFlow1 = 2e9, + inFlow2 = 1.5e9, + population = 1e5, + consumePerPerson = 10, + kWh2cubicMeters = 100, + growth = 0.05, + countYear = false, + changedYear= 1970, + currentYear = 1950, + finalTime = 1000, + execute = function(self) -- each time step + local outFlow = self.population * self.consumePerPerson * self.kWh2cubicMeters -- update outflow + self.water = self.water - outFlow -- update water + + if self.water <= 0 then -- water amount less than zero + self.water = 0 + elseif self.water > 5e9 then -- water amount more than dam capacity + self.water = 5e9 + end + + if (self.countYear == true) then + if (self.timer:getTime()%12 == 0) then -- every each year increments current year + self.currentYear = self.currentYear + 1 + if (self.currentYear == self.changedYear) then -- when changedYear equal to currentYear + self.inFlow1 = self.inFlow1/2 + self.inFlow2 = self.inFlow2/2 + end + end + end + end, + + init = function (self) + self.chart = Chart{ + target = self, + select = "water" + } + + self.timer = Timer{ + Event{action = self, priority = 'high'}, + Event{start = 1, period = 12, action = function() + self.water = self.water + self.inFlow1 -- first season in first semester + end}, + Event{start = 7, period = 12, action = function() + self.water = self.water + self.inFlow2 -- second season in second semester + end}, + Event{period = 12, action = function() + self.consumePerPerson = self.consumePerPerson*(1 + self.growth) -- every year add 5% + end}, + Event{action = self.chart} + } + + end +} + diff --git a/lua/Tub.lua b/lua/Tub.lua index 3b395d6..ce54608 100644 --- a/lua/Tub.lua +++ b/lua/Tub.lua @@ -24,7 +24,7 @@ Tub = Model{ model.timer = Timer{ Event{action = model}, - Event{start = 10, period = 10, action = function() + Event{period = 10, action = function() model.water = model.water + model.inFlow end}, Event{action = model.chart} diff --git a/tests/Dam.lua b/tests/Dam.lua new file mode 100644 index 0000000..8bd75e9 --- /dev/null +++ b/tests/Dam.lua @@ -0,0 +1,19 @@ +-- Test file for Dam.lua +-- Author: Gilberto Camara and Pedro R. Andrade + +return{ + Dam = function(unitTest) + local model = Dam{} + + model:run() + + unitTest:assertSnapshot(model.chart, "Dam-chart-1.bmp", 0.20) + + model = Dam{countYear = true} + + model:run() + + unitTest:assertSnapshot(model.chart, "Dam-chart-2.bmp", 0.12) + end, +} + diff --git a/tests/RandomWalk.lua b/tests/RandomWalk.lua index 5b68447..926cb7b 100644 --- a/tests/RandomWalk.lua +++ b/tests/RandomWalk.lua @@ -6,7 +6,7 @@ return{ local r = RandomWalk{} r:run() - unitTest:assertEquals(r.value, 2) + unitTest:assertEquals(r.value, -4) unitTest:assertSnapshot(r.chart, "random-walk.png", 0.1) end, }