From d341a71d10866956dddaefb9600af952fe6f94f9 Mon Sep 17 00:00:00 2001 From: Hendrik Dorn Date: Fri, 19 Jul 2024 13:44:18 +0200 Subject: [PATCH] Save the grids Is nobody thinking of the grids? TOML can save the grids! --- Project.toml | 2 + src/Ferrite.jl | 3 + src/Grid/parse_the_grids.jl | 120 ++++++++++++++++++++ src/Grid/save_the_grids.jl | 213 ++++++++++++++++++++++++++++++++++++ 4 files changed, 338 insertions(+) create mode 100644 src/Grid/parse_the_grids.jl create mode 100644 src/Grid/save_the_grids.jl diff --git a/Project.toml b/Project.toml index af5e162e41..c5cb8b6e84 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/Ferrite.jl b/src/Ferrite.jl index 0776f2f6be..acb96ccc4c 100644 --- a/src/Ferrite.jl +++ b/src/Ferrite.jl @@ -24,6 +24,7 @@ using Tensors: rotation_tensor, symmetric, tovoigt!, hessian, otimesu using ForwardDiff: ForwardDiff +using Printf,TOML include("CollectionsOfViews.jl") using .CollectionsOfViews: @@ -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") diff --git a/src/Grid/parse_the_grids.jl b/src/Grid/parse_the_grids.jl new file mode 100644 index 0000000000..9b1bfb6ffd --- /dev/null +++ b/src/Grid/parse_the_grids.jl @@ -0,0 +1,120 @@ +function parse_the_grids(path::String="/tmp/grid.toml") + 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 + +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 + diff --git a/src/Grid/save_the_grids.jl b/src/Grid/save_the_grids.jl new file mode 100644 index 0000000000..82bba336c7 --- /dev/null +++ b/src/Grid/save_the_grids.jl @@ -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) + 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 + + 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