added field concept

This commit is contained in:
Jukka Aho
2015-06-20 15:57:23 +03:00
parent 4756a32029
commit 0d6294b82b
2 changed files with 81 additions and 1 deletions
+56 -1
View File
@@ -1,7 +1,8 @@
# This file is a part of JuliaFEM. License is MIT: https://github.com/ovainola/JuliaFEM/blob/master/README.md
module JuliaFEM
export Model, new_model
export Model, new_model, new_field, get_field
type Model
model # For global variables
@@ -17,4 +18,58 @@ function new_model()
return Model(Dict(), Dict(), Dict(), Dict(), Dict())
end
function new_field(field_type, field_name)
"""Create new field to model.
Parameters
----------
field_type : Dict()
Target topology (model.model, model.nodes, model.elements,
model.element_nodes, model.element_gauss
field_name : str
Field name
Returns
-------
Dict
New field
"""
d = Dict()
setindex!(field_type, d, field_name)
return d
end
function get_field(field_type, field_name; create_if_doesnt_exist=false)
"""Get field from model.
Parameters
----------
field_type : Dict()
Target topology (model.model, model.nodes, model.elements,
model.element_nodes, model.element_gauss
field_name : str
Field name
create_if_doesnt_exist : bool, optional
If field doesn't exists, create one and return empty field
Raises
------
Error, if field not found and create_if_doesnt_exist == false
"""
if !(field_name in keys(field_type))
if create_if_doesnt_exist
field = new_field(field_type, field_name)
else
throw("Field not found")
end
else
field = getindex(field_type, field_name)
end
return field
end
end # module
+25
View File
@@ -11,6 +11,31 @@ function test_create_model()
@assert :element_gauss_points in fn
end
function test_add_field()
m = new_model()
field = new_field(m.elements, "color")
@assert "color" in keys(m.elements)
end
function test_get_field()
m = new_model()
field = new_field(m.elements, "color")
field[1] = "red" # set element 1 field value to "red"
field2 = get_field(m.elements, "color") # get color field
@assert field2[1] == "red"
end
function test_get_field_if_it_doesnt_exist()
m = new_model()
# FIXME: how to test exception?
# @assert "throw error when" field = get_field(m.elements, "temperature")
field = get_field(m.elements, "temperature"; create_if_doesnt_exist=true)
field[1] = 173
field2 = get_field(m.elements, "temperature")
@assert field2[1] == 173
end
# write your own tests here
# @test 1 == JuliaFEM.test()
# @test_approx_eq 1.0 1.0