Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save the grids #1024

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
NearestNeighbors = "b8a86587-4115-5ab1-83bc-aa920d37bbce"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
Tensors = "48a634ad-e948-5137-8d70-aa71f2a747f4"
WriteVTK = "64499a7a-5c06-52f2-abe2-ccb03c286192"

Expand Down
3 changes: 3 additions & 0 deletions src/Ferrite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ using Tensors:
rotation_tensor, symmetric, tovoigt!, hessian, otimesu
using ForwardDiff:
ForwardDiff
using Printf,TOML

include("CollectionsOfViews.jl")
using .CollectionsOfViews:
Expand Down Expand Up @@ -138,6 +139,8 @@ include("Grid/topology.jl")
include("Grid/utils.jl")
include("Grid/grid_generators.jl")
include("Grid/coloring.jl")
include("Grid/save_the_grids.jl")
include("Grid/parse_the_grids.jl")

# Dofs
include("Dofs/DofHandler.jl")
Expand Down
120 changes: 120 additions & 0 deletions src/Grid/parse_the_grids.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
function parse_the_grids(path::String="/tmp/grid.toml")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this function is exported users will see it. Hence a doc string should be added for users.

file = open(path,"r")
parsed_data = TOML.parse(file)
close(file)

cells = parse_the_cells(parsed_data)
nodes = parse_the_nodes(parsed_data)
cellsets = parse_the_cellsets(parsed_data)
nodesets = parse_the_nodesets(parsed_data)
facetsets = parse_the_facetsets(parsed_data)
vertexsets = parse_the_vertexsets(parsed_data)

return Grid(cells,nodes,cellsets,nodesets,facetsets,vertexsets)
end

export parse_the_grids
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


function parse_the_cells(parsed_data::Dict{String,Any})
p_cells = parsed_data["Cells"]

cells = AbstractCell[]

for p_cell in p_cells
p_nodes = p_cell["nodes"]
p_type = p_cell["type"]

cell_type = getfield(Ferrite,Symbol(p_type))
N = length(p_nodes)

cell = cell_type(NTuple{N,Int}(p_nodes))

push!(cells,cell)
end

# restate the vector, so that julia finds an adequate type
cells = [ cell for cell in cells ]

return cells
end

function parse_the_nodes(parsed_data::Dict{String,Any})
p_nodes = parsed_data["Nodes"]["nodes"]

dim = length(p_nodes[1])

nodes = Vector{Node{dim,Float64}}(undef,length(p_nodes))

for (n,p_node) in enumerate(p_nodes)
node_x = NTuple{dim,Float64}(p_node)
node = Node(node_x)
nodes[n] = node
end

return nodes
end

function parse_the_cellsets(parsed_data::Dict{String,Any})
p_cellsets = parsed_data["Cellsets"]

cellsets = Dict{String,OrderedSet{Int64}}()

for (setname,p_cellset) in p_cellsets
cellset = OrderedSet{Int64}(p_cellset)
push!(cellsets, setname => cellset)
end

return cellsets
end

function parse_the_nodesets(parsed_data::Dict{String,Any})
p_nodesets = parsed_data["Nodesets"]

nodesets = Dict{String,OrderedSet{Int64}}()

for (setname,p_nodeset) in p_nodesets
nodeset = OrderedSet{Int64}(p_nodeset)
push!(nodesets,setname => nodeset)
end

return nodesets
end

function parse_the_facetsets(parsed_data::Dict{String,Any})
p_facetsets = parsed_data["Facetsets"]

facetsets = Dict{String,OrderedSet{FacetIndex}}()

for (setname,p_facetset) in p_facetsets
facetset = OrderedSet{FacetIndex}()

for p_facet in p_facetset
facet = FacetIndex(p_facet[1],p_facet[2])
push!(facetset,facet)
end

push!(facetsets,setname => facetset)
end

return facetsets
end

function parse_the_vertexsets(parsed_data::Dict{String,Any})
p_vertexsets = parsed_data["Vertexsets"]

vertexsets = Dict{String,OrderedSet{VertexIndex}}()

for (setname,p_vertexset) in p_vertexsets
vertexset = OrderedSet{VertexIndex}()

for p_vertex in p_vertexset
vertex = VertexIndex(p_vertex[1],p_vertex[2])
push!(vertexset,vertex)
end

push!(vertexsets,setname => vertexset)
end

return vertexsets
end

213 changes: 213 additions & 0 deletions src/Grid/save_the_grids.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
function save_the_grid(grid::Grid,dir::String="/tmp",name::String="grid.toml")
filename = dir * "/" * name

file = open(filename,"w")
print(file,toml_saves_the_grids(grid))
close(file)
end

export save_the_grid

function toml_saves_the_grids(grid::Grid,accuracy::Integer=8)
t_string::String = ""

t_string *= toml_saves_the_grids(grid.nodes)
t_string *= toml_saves_the_grids(grid.nodesets,"Nodesets")
t_string *= toml_saves_the_grids(grid.cellsets,"Cellsets")
t_string *= toml_saves_the_grids(grid.vertexsets)
t_string *= toml_saves_the_grids(grid.facetsets)
t_string *= toml_saves_the_grids(grid.cells)

return t_string
end

function toml_saves_the_grids(cells::AbstractVector{<:Ferrite.AbstractCell})
t_string::String = ""
for cell in cells
t_string *= @sprintf("%s",toml_saves_the_grids(cell))
end

return t_string
end

function toml_saves_the_grids(cell::Ferrite.AbstractCell{rshape}) where {rshape}
t_string::String = "[[Cells]]\n"
t_string *= @sprintf("type = \"%s\"\n",typeof(cell))
t_string *= @sprintf("nodes = %s\n",toml_saves_the_grids(cell.nodes))
t_string *= "\n"

return t_string
end

function toml_saves_the_grids(tpl::NTuple{N,<:Integer}) where N
t_string::String = "[ "

for n in 1:N
t_string *= @sprintf("%d,",tpl[n])
end

t_string = t_string[1:end-1]

t_string *= " ]"
end

function toml_saves_the_grids(facetsets::Dict{String,OrderedSet{FacetIndex}})
t_string::String = "[Facetsets]\n"

for (setname,facetset) in facetsets
t_string *= @sprintf("%s = %s\n",setname,toml_saves_the_grids(facetset))
end

t_string *= "\n"

return t_string
end

function toml_saves_the_grids(facetset::OrderedSet{FacetIndex})
t_string::String = "[ "

for facet in facetset
t_string *= @sprintf("[%d,%d],",facet[1],facet[2])
end

t_string = t_string[1:end-1]

t_string *= " ]"

return t_string
end

# used for nodesets and cellsets
function toml_saves_the_grids(sets::Dict{String,OrderedSet{Int64}},collection_name::String)
t_string::String = @sprintf("[%s]\n",collection_name)

for (setname,cellset) in sets
t_string *= @sprintf("%s = %s\n", setname, toml_saves_the_grids(cellset))
end

t_string *= "\n"

return t_string
end

function toml_saves_the_grids(intset::OrderedSet{<:Integer})
t_string::String = "[ "

for x in intset
t_string *= @sprintf("%d,",x)
end

t_string = t_string[1:end-1]

t_string *= " ]"

return t_string
end

function toml_saves_the_grids(nodes::AbstractVector{<:Node},accuracy::Integer=8)
t_string::String = "[Nodes]\n"
t_string *= "nodes = [ "

for node in nodes
t_string *= @sprintf("%s,",toml_saves_the_grids(node))
end

t_string = t_string[1:end-1]

t_string *= " ]\n"
t_string *= "\n"

return t_string
end

function toml_saves_the_grids(node::Node{dim}) where dim
t_string::String = "["

for i in 1:dim
t_string *= @sprintf("%s,",toml_saves_the_grids(node.x[i]))
end

t_string = t_string[1:end-1]

t_string *= "]"

return t_string
end

# find a small number of digits for precise storage
function toml_saves_the_grids(x::AbstractFloat)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not confident that this works properly for all float types. Seems like this implementation is specific to Float64 and (more narrow types). But this function essentially generates a string of the truncated real number represenation?

N = 17

for n in 1:17
if round(x,digits=n) - x == 0
N = n

break
end
end

if N == 1
t_string = @sprintf("%.0E",x)
elseif N == 2
t_string = @sprintf("%.1E",x)
elseif N == 3
t_string = @sprintf("%.2E",x)
elseif N == 4
t_string = @sprintf("%.3E",x)
elseif N == 5
t_string = @sprintf("%.4E",x)
elseif N == 6
t_string = @sprintf("%.5E",x)
elseif N == 7
t_string = @sprintf("%.6E",x)
elseif N == 8
t_string = @sprintf("%.7E",x)
elseif N == 9
t_string = @sprintf("%.8E",x)
elseif N == 10
t_string = @sprintf("%.9E",x)
elseif N == 11
t_string = @sprintf("%.10E",x)
elseif N == 12
t_string = @sprintf("%.11E",x)
elseif N == 13
t_string = @sprintf("%.12E",x)
elseif N == 14
t_string = @sprintf("%.13E",x)
elseif N == 15
t_string = @sprintf("%.14E",x)
elseif N == 16
t_string = @sprintf("%.15E",x)
elseif N == 17
t_string = @sprintf("%.16E",x)
end
Comment on lines +149 to +183
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just be something like

Suggested change
if N == 1
t_string = @sprintf("%.0E",x)
elseif N == 2
t_string = @sprintf("%.1E",x)
elseif N == 3
t_string = @sprintf("%.2E",x)
elseif N == 4
t_string = @sprintf("%.3E",x)
elseif N == 5
t_string = @sprintf("%.4E",x)
elseif N == 6
t_string = @sprintf("%.5E",x)
elseif N == 7
t_string = @sprintf("%.6E",x)
elseif N == 8
t_string = @sprintf("%.7E",x)
elseif N == 9
t_string = @sprintf("%.8E",x)
elseif N == 10
t_string = @sprintf("%.9E",x)
elseif N == 11
t_string = @sprintf("%.10E",x)
elseif N == 12
t_string = @sprintf("%.11E",x)
elseif N == 13
t_string = @sprintf("%.12E",x)
elseif N == 14
t_string = @sprintf("%.13E",x)
elseif N == 15
t_string = @sprintf("%.14E",x)
elseif N == 16
t_string = @sprintf("%.15E",x)
elseif N == 17
t_string = @sprintf("%.16E",x)
end
t_string = @sprintf("%.$(N-1)E",x)


return t_string
end

function toml_saves_the_grids(vertexsets::Dict{String,OrderedSet{VertexIndex}})
t_string::String = "[Vertexsets]\n"

for (setname,vertexset) in vertexsets
t_string *= @sprintf("%s = %s\n",setname,toml_saves_the_grids(vertexset))
end

t_string *= "\n"

return t_string
end

function toml_saves_the_grids(vertexset::OrderedSet{VertexIndex})
t_string::String = "[ "

for vertex in vertexset
t_string *= @sprintf("[%d,%d],",vertex[1],vertex[2])
end

t_string = t_string[1:end-1]
t_string *= " ]"

return t_string
end

export toml_saves_the_grids
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.