Skip to content

Commit

Permalink
moved all io stuff to io.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
j-fu committed Aug 22, 2023
1 parent d5ebf3d commit 8823ff0
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 160 deletions.
3 changes: 0 additions & 3 deletions src/extendablegrid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
161 changes: 161 additions & 0 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
157 changes: 0 additions & 157 deletions src/simplexgrid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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



"""
Expand Down

0 comments on commit 8823ff0

Please sign in to comment.