From 8823ff0fad9e5b495951245bfb08c6c7ab6ea8cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Fuhrmann?= Date: Tue, 22 Aug 2023 11:04:32 +0200 Subject: [PATCH] moved all io stuff to io.jl --- src/extendablegrid.jl | 3 - src/io.jl | 161 ++++++++++++++++++++++++++++++++++++++++++ src/simplexgrid.jl | 157 ---------------------------------------- 3 files changed, 161 insertions(+), 160 deletions(-) diff --git a/src/extendablegrid.jl b/src/extendablegrid.jl index c1092dcc..82cd56a3 100644 --- a/src/extendablegrid.jl +++ b/src/extendablegrid.jl @@ -522,6 +522,3 @@ function bbox(grid) map(a->a[1],e),map(a->a[2],e) end -function mixedgrid_from_gmsh end - -function load_mixedgrid_to_gmsh end diff --git a/src/io.jl b/src/io.jl index 914edbc9..960e94c9 100644 --- a/src/io.jl +++ b/src/io.jl @@ -40,3 +40,164 @@ function writeVTK(filename::String, grid::ExtendableGrid{Tc,Ti}; kwargs...) wher end end + + + +""" +$(TYPEDSIGNATURES) + +Write grid to file. Currently for pdelib sg format only +""" +function Base.write(fname::String, g::ExtendableGrid; format="") + (fbase,fext)=splitext(fname) + if format=="" + format=fext[2:end] + end + @assert format=="sg" + + dim_g=dim_grid(g) + dim_s=dim_space(g) + nn=num_nodes(g) + nc=num_cells(g) + nbf=num_bfaces(g) + coord=g[Coordinates] + cellnodes=g[CellNodes] + bfacenodes=g[BFaceNodes] + cellregions=g[CellRegions] + bfaceregions=g[BFaceRegions] + + # TODO: replace @sprintf by someting non-allocating + open(fname, "w") do file + write(file,@sprintf("SimplexGrid")) + write(file,@sprintf(" ")) + write(file,@sprintf("2.1\n")) + write(file,@sprintf("#created by ExtendableGrids.jl (c) J.Fuhrmann et al\n")) + write(file,@sprintf("#mailto:{fuhrmann|streckenbach}@wias-berlin.de\n")) + write(file,@sprintf("#%s\n",Dates.format(Dates.now(),"yyyy-mm-ddTHH-mm-SS"))) + + write(file,@sprintf("DIMENSION\n%d\n",dim_g)) + write(file,@sprintf("NODES\n%d %d\n",nn,dim_s)) + + for inode=1:nn + for idim=1:dim_s + write(file,@sprintf("%.20e ",coord[idim,inode])) + write(file,@sprintf("\n")) + end + end + + write(file,@sprintf("CELLS\n%d\n",nc)) + for icell=1:nc + for inode=1:dim_g+1 + write(file,@sprintf("%d ", cellnodes[inode,icell])) + end + write(file,@sprintf("%d\n",cellregions[icell])) + end + + write(file,@sprintf("FACES\n%d\n",nbf)) + for ibface=1:nbf + for inode=1:dim_g + write(file,@sprintf("%d ", bfacenodes[inode,ibface])) + end + write(file,@sprintf("%d\n",bfaceregions[ibface])) + end + write(file,@sprintf("END\n")) + flush(file) + flush(file) + end + nothing +end + +###################################################### +""" +$(TYPEDSIGNATURES) + +Read grid from file. Currently for pdelib sg format only. +""" +function simplexgrid(file::String;format="") + Ti=Cint + (fbase,fext)=splitext(file) + if format=="" + format=fext[2:end] + end + @assert format=="sg" + + tks=TokenStream(file) + expecttoken(tks,"SimplexGrid") + version=parse(Float64,gettoken(tks)) + version20=false; + + if (version==2.0) + version20=true; + elseif (version==2.1) + version20=false; + else + error("Read grid: wrong format version: $(version)") + end + + dim::Ti=0 + coord=Array{Float64,2}(undef,0,0) + cells=Array{Ti,2}(undef,0,0) + regions=Array{Ti,1}(undef,0) + faces=Array{Ti,2}(undef,0,0) + bregions=Array{Ti,1}(undef,0) + while(true) + if (trytoken(tks,"DIMENSION")) + dim=parse(Ti,gettoken(tks)); + elseif (trytoken(tks,"NODES")) + nnodes=parse(Ti,gettoken(tks)); + embdim=parse(Ti,gettoken(tks)); + if(dim!=embdim) + error("Dimension error (DIMENSION $(dim)) in section NODES") + end + coord=Array{Float64,2}(undef,dim,nnodes) + for inode=1:nnodes + for idim=1:embdim + coord[idim,inode]=parse(Float64,gettoken(tks)) + end + end + elseif (trytoken(tks,"CELLS")) + ncells=parse(Ti,gettoken(tks)); + cells=Array{Ti,2}(undef,dim+1,ncells) + regions=Array{Ti,1}(undef,ncells) + for icell=1:ncells + for inode=1:dim+1 + cells[inode,icell]=parse(Ti,gettoken(tks)); + end + regions[icell]=parse(Ti,gettoken(tks)); + if version20 + for j=1:dim+1 + gettoken(tks); # skip file format garbage + end + end + end + elseif (trytoken(tks,"FACES")) + nfaces=parse(Ti,gettoken(tks)); + faces=Array{Ti,2}(undef,dim,nfaces) + bregions=Array{Ti,1}(undef,nfaces) + for iface=1:nfaces + for inode=1:dim + faces[inode,iface]=parse(Ti,gettoken(tks)); + end + bregions[iface]=parse(Ti,gettoken(tks)); + if (version20) + for j=1:dim+2 + gettoken(tks); #skip file format garbage + end + end + end + else + expecttoken(tks,"END") + break + end + end + simplexgrid(coord,cells,regions,faces,bregions); +end + +# Implementations in Gmsh extension +function simplexgrid_from_gmsh end + +function load_simplexgrid_to_gmsh end + +function mixedgrid_from_gmsh end + +function load_mixedgrid_to_gmsh end diff --git a/src/simplexgrid.jl b/src/simplexgrid.jl index a7c01ac2..ad8fe4bb 100644 --- a/src/simplexgrid.jl +++ b/src/simplexgrid.jl @@ -644,163 +644,6 @@ function simplexgrid(grid2::ExtendableGrid, coordZ; bot_offset=0,cell_offset=0,t end -###################################################### -""" -$(TYPEDSIGNATURES) - -Read grid from file. Currently for pdelib sg format only. -""" -function simplexgrid(file::String;format="") - Ti=Cint - (fbase,fext)=splitext(file) - if format=="" - format=fext[2:end] - end - @assert format=="sg" - - tks=TokenStream(file) - expecttoken(tks,"SimplexGrid") - version=parse(Float64,gettoken(tks)) - version20=false; - - if (version==2.0) - version20=true; - elseif (version==2.1) - version20=false; - else - error("Read grid: wrong format version: $(version)") - end - - dim::Ti=0 - coord=Array{Float64,2}(undef,0,0) - cells=Array{Ti,2}(undef,0,0) - regions=Array{Ti,1}(undef,0) - faces=Array{Ti,2}(undef,0,0) - bregions=Array{Ti,1}(undef,0) - while(true) - if (trytoken(tks,"DIMENSION")) - dim=parse(Ti,gettoken(tks)); - elseif (trytoken(tks,"NODES")) - nnodes=parse(Ti,gettoken(tks)); - embdim=parse(Ti,gettoken(tks)); - if(dim!=embdim) - error("Dimension error (DIMENSION $(dim)) in section NODES") - end - coord=Array{Float64,2}(undef,dim,nnodes) - for inode=1:nnodes - for idim=1:embdim - coord[idim,inode]=parse(Float64,gettoken(tks)) - end - end - elseif (trytoken(tks,"CELLS")) - ncells=parse(Ti,gettoken(tks)); - cells=Array{Ti,2}(undef,dim+1,ncells) - regions=Array{Ti,1}(undef,ncells) - for icell=1:ncells - for inode=1:dim+1 - cells[inode,icell]=parse(Ti,gettoken(tks)); - end - regions[icell]=parse(Ti,gettoken(tks)); - if version20 - for j=1:dim+1 - gettoken(tks); # skip file format garbage - end - end - end - elseif (trytoken(tks,"FACES")) - nfaces=parse(Ti,gettoken(tks)); - faces=Array{Ti,2}(undef,dim,nfaces) - bregions=Array{Ti,1}(undef,nfaces) - for iface=1:nfaces - for inode=1:dim - faces[inode,iface]=parse(Ti,gettoken(tks)); - end - bregions[iface]=parse(Ti,gettoken(tks)); - if (version20) - for j=1:dim+2 - gettoken(tks); #skip file format garbage - end - end - end - else - expecttoken(tks,"END") - break - end - end - simplexgrid(coord,cells,regions,faces,bregions); -end - - -# Implementations in Gmsh extension -function simplexgrid_from_gmsh end - -function load_simplexgrid_to_gmsh end - - -""" -$(TYPEDSIGNATURES) - -Write grid to file. Currently for pdelib sg format only -""" -function Base.write(fname::String, g::ExtendableGrid; format="") - (fbase,fext)=splitext(fname) - if format=="" - format=fext[2:end] - end - @assert format=="sg" - - dim_g=dim_grid(g) - dim_s=dim_space(g) - nn=num_nodes(g) - nc=num_cells(g) - nbf=num_bfaces(g) - coord=g[Coordinates] - cellnodes=g[CellNodes] - bfacenodes=g[BFaceNodes] - cellregions=g[CellRegions] - bfaceregions=g[BFaceRegions] - - # TODO: replace @sprintf by someting non-allocating - open(fname, "w") do file - write(file,@sprintf("SimplexGrid")) - write(file,@sprintf(" ")) - write(file,@sprintf("2.1\n")) - write(file,@sprintf("#created by ExtendableGrids.jl (c) J.Fuhrmann et al\n")) - write(file,@sprintf("#mailto:{fuhrmann|streckenbach}@wias-berlin.de\n")) - write(file,@sprintf("#%s\n",Dates.format(Dates.now(),"yyyy-mm-ddTHH-mm-SS"))) - - write(file,@sprintf("DIMENSION\n%d\n",dim_g)) - write(file,@sprintf("NODES\n%d %d\n",nn,dim_s)) - - for inode=1:nn - for idim=1:dim_s - write(file,@sprintf("%.20e ",coord[idim,inode])) - write(file,@sprintf("\n")) - end - end - - write(file,@sprintf("CELLS\n%d\n",nc)) - for icell=1:nc - for inode=1:dim_g+1 - write(file,@sprintf("%d ", cellnodes[inode,icell])) - end - write(file,@sprintf("%d\n",cellregions[icell])) - end - - write(file,@sprintf("FACES\n%d\n",nbf)) - for ibface=1:nbf - for inode=1:dim_g - write(file,@sprintf("%d ", bfacenodes[inode,ibface])) - end - write(file,@sprintf("%d\n",bfaceregions[ibface])) - end - write(file,@sprintf("END\n")) - flush(file) - flush(file) - end - nothing -end - """