880 likes | 997 Views
Spatial Dynamical Modeling with TerraME. Tiago Carneiro Gilberto Câmara Pedro Andrade. Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial ̶̶̶̶ Share Alike http://creativecommons.org/licenses/by-nc-sa/2.5/. Modelling human-environment interactions.
E N D
Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial ̶̶̶̶ Share Alike http://creativecommons.org/licenses/by-nc-sa/2.5/
Modelling human-environment interactions Whatmodels are needed to describehumanactions?
Clouds: statistical distributions Clocks, clouds or ants? Clocks: deterministic equations Ants: emerging behaviour
Dynamic Spatial Models f (It) f (It+1) f (It+2) f ( It+n ) F F . . “A dynamical spatial model is a computational representation of a real-world process where a location on the earth’s surface changes in response to variations on external and internal dynamics on the landscape” (Peter Burrough)
Nature-society modelling with TerraME Nature: Physical equations Describe processes Society: Decisions on how to Use Earth´s resources
Nature-society modelling with TerraME Nature: Cellular space Society: Agents Nature: Physical equations Describe processes Society: Decisions on how to Use Earth´s resources
Modelling collective spatial actions Space Agent Agent Space Benenson and Torrens, “Geographic Automata Systems”, IJGIS, 2005 (but many questions remain...)
CellSpaces • Generalized Proximity Matriz – GPM • Hybrid Automata model • Nested scales • Agents in space Computational Modelling with Cell Spaces
TerraME - overview Model data in cellspaces Read/write data from a database
2500 m 2.500 m e 500 m Cellular Data Base Resolution
Spatial structure Large farmer (25 cells) Small farmer (2 cells) 500 m (all)
Express anisotropy Statistics and agents Phase transitions latency > 6 years Deforesting Newly implanted Deforestation > 80% Year of creation Slowing down Iddle Deforestation = 100% y=a0 + a1x1 + a2x2 + ... +aixi +E Multi-scale modelling
TerraLib: spatio-temporal database as a basis for innovation G. Câmara et al.“TerraLib: An open-source GIS library for large-scale environmental and socio-economic applications”. In: B. Hall, M. Leahy (eds.), “Open Source Approaches to Spatial Data Handling”. Berlin, Springer, 2008. Modelling (TerraME) Visualization (TerraView) Spatio-temporal Database (TerraLib) Data Mining(GeoDMA) Statistics (aRT)
TerraME C++ Framework C++ Signal Processing librarys C++ Mathematicallibrarys C++ Statisticallibrarys TerraME: Software Architecture Model 1 Model 2 Model 3 Model 4 TerraML Language TerraMLCompiler TerraML Virtual Machine TerraLib
Lua and the Web Where is Lua? • Inside Brazil • Petrobras, the Brazilian Oil Company • Embratel (the main telecommunication company in Brazil) • many other companies • Outside Brazil • Lua is used in hundreds of projects, both commercial and academic • CGILua still in restricted use • until recently all documentation was in Portuguese TerraME Programming Language: Extension of Lua Lua is the language of choice for computer games [Ierusalimschy et al, 1996] source: the Lua team
Lua Roberto Ierusalimschy PUC-Rio, Brazil
-- a Lua script color = RED b = button { label = ‘OK’, x = 10, y = 20} Host Program Lua Interpreter What is Lua? • Yet Another Scripting Language • an “extension” language • implemented as a library in ANSI C
Why Lua? • Simple and flexible • “Simple things simple, complex things possible” • Small, Efficient, Portable • Whole library written in ANSI C, compiles the same source code in all platforms • Typical uses: MS-DOS, Windows (3.1, 95, NT), Unix (Linux, Solaris, IRIX, AIX, ULTRIX), Next, OS/2, Mac
How is Lua? function fat (n) if n == 0 then return 1 else return n*fat(n-1) end end • Pascal-like Syntax. • Interpreter executes sequence of statements. • function definitions are also statements (see later) • Six types: numbers, tables, functions, strings, userdata, nil
Variables and Values • Case sensitive • semicolon may optionally follow any statement a = 1 b = a*2 print(a) print(b)
Comments • double hyphen (--) until the end of the line. • block comments start with --[[ and run until ]] print("hello") -- my comment -- print("hello”) --[[ print(10) -- no action (comment) --]]
My first Lua program C = 2 -- rain/t K = 0.4 -- flow coefficient q = 0 -- RULES for time = 0, 20, 1 do -- soil water q = q + C - K*q end print(“q = "..q)
Type nil Different from everything else Default variable type Also acts as false (boolean)
Type boolean • Comparison value • if (rain == true) then ....
boolean • false/true • nil and false are false, everything else is true • zero and the empty string are true • operators and, or, and not print(true and false) print(true and (false or true)) print(false or (true and false) or (true and true))
number • the only type for numeric values • double-precision floating-point number • arithmetic operators: +, –, *, / • exponent (^) and modulus(%) • boolean operators (<, >, <=, >=, ~=, and ==) A = 6 + 2.2 * 4e+3 a = A ^ 2 b = A % 7 print(a > b) print(b ~= 2)
Parentheses Always optional (except in the case of function call) When in doubt, use parentheses a+-i < b/2+1 <--> (a + (-i)) < ((b/2)+1) 5+x^2*8 <--> 5 + ( (x^2)*8 ) a < y and y <= z <--> (a < y) and (y <= z) –x^y^z <--> –(x^(y^z))
Type string • Immutable • No size limit (read large files as strings) • No termination value (‘\0’) • Powerful Pattern-matching in standard library • myname = “Werner Kuhn”;
if statement An if statement tests condition and executes its then-part or its else-part (optional) accordingly a = 6; b = 5 if a < b then print("a < b") elseif a < b + 5 then print("b <= a < b+5") else print("a > b+5") end
for statement for var = exp1, exp2, exp3 do something end • Execute something for each value of var from exp1 to exp2, using exp3 as the step to increment var. This third expression is optional (default is 1). for i = 1, 10 do print(i) end for i = 1, 10, 2 do print(i) end
Tables • Implement associative arrays: • any value (including functions and other tables) can be used both for indices and values t = {} -- creates an empty table t[1] = "hello" t.x = print -- t.x is sugar for t[‘x’] t.x(t[1]) -- prints ‘hello’ t.next = t -- circular list
table • Tables can be indexed not only with numbers, but also with strings or any other value of the language, except nil loc = { cover = "forest", distRoad = 0.3, distUrban = 2 } print(loc["cover"]) print(loc.cover) loc.distRoad = loc.distRoad^2 loc.distTotal = loc.distRoad + loc.distUrban loc.deforestationPot = 1/loc.distTotal
Tables within tables loc = { cover = "forest", dist = {road = 0.3, urban = 2} } print(loc.dist.road) loc.dist.total = loc.dist.road + loc.dist.urban print(loc.dist.total)
Constructors: Create and init tables • Record style • point={x=10,y=20} • print(point.y) --> 20 • List style • days={"Sun","Mon","Tue","Wed”, Sat"} • print(days[3]) --> Tue • Mixed style • points={{x=0,y=0}, point, n=2} • print(points[points.n].y) --> 20
Constructors calls function “article” article{ author="F.P.Brooks", title="The Mythical Man-Month", year=1975, } news = { {text = "New version 2.0", date = "21/05/1997"}, {text = "New example", date = "21/05/1997"}, {text = "New version: 2.1",date = "17/06/1997"}, } Lua and the Web
function • A function can carry out a specific task (commonly called procedure) or compute and return values. • A function is a first-class value in Lua. • Functions can be stored in variables and in tables, can be passed as arguments, and can be returned by other functions, giving great flexibility to the language. myprint = print print = nil myprint(2) print = myprint
Functions in Lua functionfat (n) if n == 0 then return 1 else return n*fat(n-1) end end
Higher-order Functions • Functions can also be parameters to other functions. This kind of function is what we call a higher-order function. function foreach(tab, func) for position, value in pairs(tab) do func(value, position) end end x = {7, 3, 2, 6, 4} foreach(x, function(element) print(element) end) foreach(x, function(value, position) print(position, value) end)
function inc (x) return x+1 end inc = function (x) return x+1 end sugar Functions in Lua • First class values • Example: cloning a table t clone = {} foreach(t, function (i,e) clone[i]=e end) Lua and the Web
Functions and Tables w = { redraw = function () ... end, pick = function (x,y) ... end, } if w.pick(x,y) then w.redraw() end
Tables with functions Tables may have their own functions. loc = { cover = "forest", distRoad = 0.3, distUrban = 2, deforestPot = function(myloc) return 1/(myloc.distRoad + myloc.distUrban) end } print(loc.deforestPot(loc)) print(loc:deforestPot())
Tables with functions • We can declare a “class” in Lua by creating a function that takes a table constructor as argument. function MyLocation(locdata) locdata.covertype = "forest" locdata.deforPot = function(self) return 1/(self.distRoad + self.distUrban) end return locdata end loc = MyLocation({distRoad = 0.3, distUrban = 2}) loc = MyLocation{distRoad = 0.3, distUrban = 2} print(loc.covertype) print(loc:deforPot())
list = {value=v, next=list} value - v next - Tables x Objects • Tables are dynamically created objects. list old list ...
function a:foo (x) ... end a.foo = function (self,x) ... end sugar sugar a:foo(x) a.foo(a,x) Objects • First-class functions+ tables = almost OO • Tables can have functions as fields • Sugar for method definition and call • Implicit parameter self
My second Lua program C = 2; -- rain/t K = 0.4; -- flow coefficient q = 0; -- function rain (t) if (t < 10) then return 4 – 4*math.cos(math.pi*t/10); else return 4 – 4*math.cos(math.pi*(t-10)/10); end end -- for time = 0, 20, 1 do -- soil water q = q + rain(time) - K*q; end -- report print(“q = "..q);
Standard libraries Basic String Table Math IO OS Debug Coroutine