From: <cu...@us...> - 2010-02-01 07:51:57
|
Revision: 6820 http://octave.svn.sourceforge.net/octave/?rev=6820&view=rev Author: culpo Date: 2010-02-01 07:51:51 +0000 (Mon, 01 Feb 2010) Log Message: ----------- Added new functions. Updated INDEX, DESCRIPTION. Modified Paths: -------------- trunk/octave-forge/extra/fpl/DESCRIPTION trunk/octave-forge/extra/fpl/INDEX Added Paths: ----------- trunk/octave-forge/extra/fpl/inst/fpl_dx_write_field.m trunk/octave-forge/extra/fpl/inst/fpl_dx_write_series.m trunk/octave-forge/extra/fpl/inst/fpl_vtk_write_field.m Modified: trunk/octave-forge/extra/fpl/DESCRIPTION =================================================================== --- trunk/octave-forge/extra/fpl/DESCRIPTION 2010-02-01 07:46:43 UTC (rev 6819) +++ trunk/octave-forge/extra/fpl/DESCRIPTION 2010-02-01 07:51:51 UTC (rev 6820) @@ -1,12 +1,11 @@ -Name: FPL -Version: 0.1.6 -Date: 2009-02-25 -Author: Carlo de Falco and Massimiliano Culpo +Name: fpl +Version: 1.0.0 +Date: 2010-02-01 +Author: Carlo de Falco, Massimiliano Culpo Maintainer: Massimiliano Culpo -Title: FEM Plotting -Description: Collection of routines to plot data on unstructured triangular and tetrahedral meshes -Categories: Graphics -Depends: Octave ( >= 3.0.0 ) -Autoload: NO +Title: Fem PLotting +Description: Collection of routines to save data in different graphical formats. +Categories: Graphics +Depends: octave ( >= 3.0.0 ) License: GNU/GPL SystemRequirements: dx ( >= 4.3.2), sed, bash Modified: trunk/octave-forge/extra/fpl/INDEX =================================================================== --- trunk/octave-forge/extra/fpl/INDEX 2010-02-01 07:46:43 UTC (rev 6819) +++ trunk/octave-forge/extra/fpl/INDEX 2010-02-01 07:51:51 UTC (rev 6820) @@ -1,5 +1,7 @@ FPL >> FEM Plotting Functions to save data in DX format + fpl_dx_write_field.m + fpl_dx_write_series.m FPL2dxappenddata FPL2dxoutputdata FPL2dxoutputtimeseries @@ -15,4 +17,5 @@ FPL2pdequiver FPL2ptcquiver Functions to save data in VTK format + fpl_vtk_write_field.m FPL2vtkoutputdata Added: trunk/octave-forge/extra/fpl/inst/fpl_dx_write_field.m =================================================================== --- trunk/octave-forge/extra/fpl/inst/fpl_dx_write_field.m (rev 0) +++ trunk/octave-forge/extra/fpl/inst/fpl_dx_write_field.m 2010-02-01 07:51:51 UTC (rev 6820) @@ -0,0 +1,222 @@ +## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo +## +## This file is part of: +## FPL - Fem PLotting package for octave +## +## FPL is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## FPL is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with FPL; If not, see <http://www.gnu.org/licenses/>. +## +## author: Carlo de Falco <cdf _AT_ users.sourceforge.net> +## author: Massimiliano Culpo <culpo _AT_ users.sourceforge.net> + +## -*- texinfo -*- +## @deftypefn {Function File} {} fpl_dx_writefield (@var{basename}, @ +## @var{mesh}, @var{u}, @var{attr_name}, @var{attr_rank}, @ +## @var{attr_shape}, @var{endfile}) +## +## Output data field in ASCII Open-DX format. +## +## @var{basename} is a string containing the base-name of the dx file where the +## data will be saved. +## +## @var{mesh} is a PDE-tool like mesh, like the ones generated by the +## "msh" package. +## +## @var{u} is the field to be saved. It should represent scalar, vector +## or tensor of doubles. +## +## @var{attr_name} is a descriptive name for the field @var{u}, while +## @var{attr_rank} is the rank of the field (0 for scalar, 1 for vector, +## etc.) and @var{attr_shape} is the number of components of the field +## (assumed 1 for scalar). +## +## @var{endfile} should be 0 if you want to add other variables to the +## same file, 1 otherwise. +## +## Notice that when appending fields to an already existing file: +## +## @itemize +## @item @var{mesh} will not be printed to @var{filename}, but it will +## be only used to determine if the field is piece-wise constant or +## piece-wise linear +## @item @var{u} is not checked for consistency against the @var{mesh} +## already printed in @var{filename} +## @end itemize +## +## Example 1 (wrong usage): +## @example +## <generate msh1/u1 msh2/u2 in some way> +## fpl_dx_write_field("field.dx",msh1,u1,"density",1,0,0); +## fpl_dx_write_field("field.dx",msh2,u2,"temperature",1,0,1); +## @end example +## generate a file that fails at OpenDX run-time. +## +## Example 2: +## @example +## <generate msh1 and two fields u1-u2 in some way> +## fpl_dx_write_field("field",msh1,u1,"density",1,0,0); +## fpl_dx_write_field("field",msh1,u2,"temperature",1,0,1); +## @end example +## will generate a valid OpenDX ASCII data file. +## +## @seealso{fpl_dx_write_series} +## +## @end deftypefn + +function fpl_dx_write_field(basename,mesh,u,attr_name,attr_rank,attr_shape,endfile) + + ## Check input + if nargin!=7 + error("fpl_dx_write_field: wrong number of input"); + endif + + if !ischar(basename) + error("fpl_dx_write_field: basename should be a valid string"); + elseif !( isstruct(mesh) ) + error("fpl_dx_write_field: mesh should be a valid structure"); + elseif !ismatrix(u) + error("fpl_dx_write_field: u should be a valid matrix"); + elseif !ischar(attr_name) + error("fpl_dx_write_field: attr_name should be a valid string"); + elseif !isscalar(attr_rank) + error("fpl_dx_write_field: attr_rank should be a valid scalar"); + elseif !isscalar(attr_shape) + error("fpl_dx_write_field: attr_shape should be a valid scalar"); + elseif !isscalar(endfile) + error("fpl_dx_write_field: endfile should be a valid scalar"); + endif + + filename = [basename ".dx"]; + + if ! exist(filename,"file") + ## If file does not exist, create it + fid = fopen (filename,"w"); + create = 1; + else + ## FIXME: the following should be performed in a cleaner way! Does a + ## backward fgetl function exist? + + ## If file exist, check if it was already closed + fid = fopen (filename,"r"); + fseek(fid,-4,SEEK_END); + tst = fgetl(fid); + if strcmp(tst,"end") + error("fpl_dx_write_field: file %s exist and was already closed",filename); + endif + fclose(fid); + fid = fopen(filename,"a"); + create = 0; + endif + + p = mesh.p'; + dim = columns(p); # 2D or 3D + + if dim == 2 + t = mesh.t(1:3,:)'; + elseif dim == 3 + t = mesh.t(1:4,:)'; + else + error("fpl_dx_write_field: neither 2D triangle nor 3D tetrahedral mesh"); + endif + + nnodes = rows(p); + nelems = rows(t); + ndatas = rows(u); + + if ndatas == nnodes + dep = "positions"; + elseif ndatas == nelems + dep = "connections"; + else + error("fpl_dx_write_field: neither position nor connection data type") + endif + + if create + ## If the file has just been created, print mesh information + print_grid(fid,dim,p,nnodes,t,nelems); + endif + ## Otherwise assume the mesh is consistent with the one in the file + ## and print only field information + print_data(fid,u,ndatas,dep,attr_name,attr_rank,attr_shape); + + if(endfile) + fprintf(fid,"\nend\n"); + endif + fclose (fid); + +endfunction + +## fprint a 2Dtrg or 3Dtet mesh +function print_grid(fid,dim,p,nnodes,t,nelems) + + fprintf(fid,"object ""pos""\n"); + fprintf(fid,"class array type float rank 1 shape %d items %d data follows",dim,nnodes); + + for ii = 1:nnodes + fprintf(fid,"\n"); + fprintf(fid," %1.7e",p(ii,:)); + endfor + + ## In DX format nodes are + ## numbered starting from zero, + ## instead we want to number + ## them starting from 1! + ## Here we restore the DX + ## format + if (min(min(t))==1) + t -= 1; + elseif(min(min(t))~=0) + error("fpl_dx_write_field: check triangle structure") + endif + + fprintf(fid,"\n\nobject ""con""\n"); + fprintf(fid,"class array type int rank 1 shape %d items %d data follows",dim+1,nelems); + for ii = 1:nelems + fprintf(fid,"\n"); + fprintf(fid," %d",t(ii,:)); + endfor + + fprintf(fid,"\n"); + if dim == 2 + fprintf(fid,"attribute ""element type"" string ""triangles""\n"); + elseif dim == 3 + fprintf(fid,"\nattribute ""element type"" string ""tetrahedra""\n"); + endif + fprintf(fid,"attribute ""ref"" string ""positions""\n\n"); + +endfunction + +## fprint data on a trg grid +function print_data(fid,u,ndatas,dep,attr_name,attr_rank,attr_shape) + + if ((attr_rank == 0) && (min(size(u))==1)) + fprintf(fid,"object ""%s.data""\n",attr_name); + fprintf(fid,"class array type double rank 0 items %d data follows",ndatas); + fprintf(fid,"\n %1.7e",u); + else + fprintf(fid,"object ""%s.data""\n",attr_name); + fprintf(fid,"class array type double rank %d shape %d items %d data follows",attr_rank,attr_shape,ndatas); + for ii=1:ndatas + fprintf(fid,"\n"); + fprintf(fid," %1.7e",u(ii,:)); + endfor + endif + + fprintf(fid,"\n"); + fprintf(fid,"attribute ""dep"" string ""%s"" \n\n",dep); + fprintf(fid,"object ""%s"" class field\n",attr_name); + fprintf(fid,"component ""positions"" value ""pos""\n"); + fprintf(fid,"component ""connections"" value ""con""\n"); + fprintf(fid,"component ""data"" value ""%s.data""\n",attr_name); + fprintf(fid,"\n"); +endfunction Added: trunk/octave-forge/extra/fpl/inst/fpl_dx_write_series.m =================================================================== --- trunk/octave-forge/extra/fpl/inst/fpl_dx_write_series.m (rev 0) +++ trunk/octave-forge/extra/fpl/inst/fpl_dx_write_series.m 2010-02-01 07:51:51 UTC (rev 6820) @@ -0,0 +1,119 @@ +## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo +## +## This file is part of: +## FPL - Fem PLotting package for octave +## +## FPL is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## FPL is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with FPL; If not, see <http://www.gnu.org/licenses/>. +## +## author: Carlo de Falco <cdf _AT_ users.sourceforge.net> +## author: Massimiliano Culpo <culpo _AT_ users.sourceforge.net> + +## -*- texinfo -*- +## @deftypefn {Function File} {} fpl_dx_write_series (@var{basename}, @ +## @var{mesh}, @var{u}, @var{sp}, @var{attr_name}, @var{attr_rank}, @ +## @var{attr_shape}) +## +## Output data series in ASCII Open-DX format. +## +## @var{basename} is a string containing the base-name of the dx file where the +## data will be saved. +## +## @var{mesh} is a PDE-tool like mesh, like the ones generated by the +## "msh" package. +## +## @var{u} is the series to be saved. It should represent scalar, vector +## or tensor of doubles. @var{sp} is the vector of the sampled points +## (e.g. time points in the case of a time series). +## +## @var{attr_name} is a descriptive name for the series @var{u}, while +## @var{attr_rank} is the rank of the field items (0 for scalar, 1 for +## vector, etc.) and @var{attr_shape} is the number of components of the +## field items (assumed 1 for scalar). +## +## @seealso{fpl_dx_write_field} +## +## @end deftypefn + +function fpl_dx_write_series(basename,mesh,u,tp,attr_name,attr_rank,attr_shape) + + ## FIXME: add append capabilities as in fpl_dx_write_field?? + + if nargin!=7 + error("fpl_dx_write_series: wrong number of input"); + endif + + if !ischar(basename) + error("fpl_dx_write_series: basename should be a valid string"); + elseif !( isstruct(mesh) ) + error("fpl_dx_write_series: mesh should be a valid structure"); + elseif !ismatrix(u) + error("fpl_dx_write_series: u should be a valid matrix"); + elseif !ischar(attr_name) + error("fpl_dx_write_series: attr_name should be a valid string"); + elseif !isscalar(attr_rank) + error("fpl_dx_write_series: attr_rank should be a valid scalar"); + elseif !isscalar(attr_shape) + error("fpl_dx_write_series: attr_shape should be a valid scalar"); + ##elseif !isscalar(endfile) + ##error("fpl_dx_write_series: endfile should be a valid scalar"); + endif + + filename = [basename ".dx"]; + + npoints = length(tp); + + p = mesh.p'; + dim = columns(p); # 2D or 3D + + if dim == 2 + t = mesh.t(1:3,:)'; + elseif dim == 3 + t = mesh.t(1:4,:)'; + else + error("fpl_dx_write_series: neither 2D triangle nor 3D tetrahedral mesh"); + endif + + nnodes = rows(p); + nelems = rows(t); + ndatas = rows(u); + + if ndatas == nnodes + dep = "positions"; + elseif ndatas == nelems + dep = "connections"; + else + error("fpl_dx_write_series: neither position nor connection data type") + endif + + ## Write field items to file + idx = (1:attr_shape); + for ii = 1:npoints + field = u(:,idx); + fname = sprintf("%s.%d",attr_name,ii); + fpl_dx_write_field(basename,mesh,field,fname,attr_rank,attr_shape,0); + idx += attr_shape; + endfor + + ##if endfile + fid = fopen(filename,"a"); + fprintf (fid, "object \"%s_series\" class series\n",attr_name); + for ii = 1:npoints + fname = sprintf("%s.%d",attr_name,ii); + fprintf (fid,"member %d position %g value \"%s\"\n",ii-1,tp(ii),fname); + endfor + fprintf (fid, "\nend\n"); + fclose(fid); + ##endif + +endfunction \ No newline at end of file Added: trunk/octave-forge/extra/fpl/inst/fpl_vtk_write_field.m =================================================================== --- trunk/octave-forge/extra/fpl/inst/fpl_vtk_write_field.m (rev 0) +++ trunk/octave-forge/extra/fpl/inst/fpl_vtk_write_field.m 2010-02-01 07:51:51 UTC (rev 6820) @@ -0,0 +1,227 @@ +## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo +## +## This file is part of: +## FPL - Fem PLotting package for octave +## +## FPL is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## FPL is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with FPL; If not, see <http://www.gnu.org/licenses/>. +## +## author: Carlo de Falco <cdf _AT_ users.sourceforge.net> +## author: Massimiliano Culpo <culpo _AT_ users.sourceforge.net> + +## -*- texinfo -*- +## @deftypefn {Function File} {} fpl_vtk_write_field (@var{basename}, @ +## @var{mesh}, @var{nodedata}, @var{celldata}, @var{endfile}) +## +## Output data field in serial XML-VTK UnstructuredGrid format. +## +## @var{basename} is a string containing the base-name of the (vtu) file +## where the data will be saved. +## +## @var{mesh} is a PDE-tool like mesh, like the ones generated by the +## "msh" package. +## +## @var{nodedata} and @var{celldata} are (Ndata x 2) cell arrays containing +## respectively <PointData> and <CellData> representing scalars or +## vectors: +## @itemize @minus +## @item @var{*data}@{:,1@} = variable data; +## @item @var{*data}@{:,2@} = variable names; +## @end itemize +## +## @var{endfile} should be 0 if you want to add other variables to the +## same file, 1 otherwise. +## +## Example: +## @example +## <generate msh1, node centered field nc1, cell centered field cc1> +## fpl_vtk_write_field("example",msh1,@{nc1, "temperature"@},@ +## @{cc1, "density"@},0); +## <generate msh2, node centered field nc2> +## fpl_vtk_write_field("example",msh2,@{nc2, "temperature"@},@ +## @{@},1); +## @end example +## will generate a valid XML-VTK UnstructuredGrid file. +## +## @seealso{fpl_dx_write_field, fpl_dx_write_series} +## +## @end deftypefn + +function fpl_vtk_write_field(basename,mesh,nodedata,celldata,endfile) + + ## Check input + if nargin!=5 + error("fpl_vtk_write_field: wrong number of input"); + endif + + if !ischar(basename) + error("fpl_vtk_write_field: basename should be a valid string"); + elseif !( isstruct(mesh) ) + error("fpl_vtk_write_field: mesh should be a valid structure"); + elseif !(iscell(nodedata) && iscell(celldata)) + error("fpl_vtk_write_field: nodedata and celldata should be a valid cells"); + endif + + filename = [basename ".vtu"]; + + if ! exist(filename,"file") + fid = fopen (filename,"w"); + ## Header + fprintf (fid,"<?xml version=""1.0""?>\n"); + fprintf (fid,"<VTKFile type=""UnstructuredGrid"" version=""0.1"" byte_order=""LittleEndian"">\n"); + fprintf (fid,"<UnstructuredGrid>\n"); + else + ## FIXME: the following should be performed in a cleaner way! Does a + ## backward fgetl function exist? + + ## If file exist, check if it was already closed + fid = fopen (filename,"r"); + fseek(fid,-10,SEEK_END); + tst = fgetl(fid); + if strcmp(tst,"</VTKFile>") + error("fpl_vtk_write_field: file %s exist and was already closed",filename); + endif + fclose(fid); + fid = fopen (filename,"a"); + endif + + p = mesh.p; + dim = rows(p); # 2D or 3D + + if dim == 2 + t = mesh.t(1:3,:); + elseif dim == 3 + t = mesh.t(1:4,:); + else + error("fpl_vtk_write_field: neither 2D triangle nor 3D tetrahedral mesh"); + endif + + t -= 1; + + nnodes = columns(p); + nelems = columns(t); + + ## Header for <Piece> + fprintf (fid,"<Piece NumberOfPoints=""%d"" NumberOfCells=""%d"">\n",nnodes,nelems); + ## Print grid + print_grid(fid,dim,p,nnodes,t,nelems); + ## Print PointData + print_data_points(fid,nodedata,nnodes) + print_cell_data (fid,celldata,nelems) + ## Footer for <Piece> + fprintf (fid, "</Piece>\n"); + + if (endfile) + ## Footer + fprintf (fid, "</UnstructuredGrid>\n"); + fprintf (fid, "</VTKFile>"); + endif + + fclose (fid); + +endfunction + +## Print Points and Cells Data +function print_grid(fid,dim,p,nnodes,t,nelems) + + if dim == 2 + p = [p; zeros(1,nnodes)]; + eltype = 5; + else + eltype = 10; + endif + + ## VTK-Points (mesh nodes) + fprintf (fid,"<Points>\n"); + fprintf (fid,"<DataArray type=""Float64"" Name=""Array"" NumberOfComponents=""3"" format=""ascii"">\n"); + fprintf (fid,"%g %g %g\n", p); + fprintf (fid,"</DataArray>\n"); + fprintf (fid,"</Points>\n"); + + ## VTK-Cells (mesh elements) + fprintf (fid, "<Cells>\n"); + fprintf (fid, "<DataArray type=""Int32"" Name=""connectivity"" format=""ascii"">\n"); + if dim == 2 + fprintf (fid, "%d %d %d \n", t); + else + fprintf (fid, "%d %d %d %d \n", t); + endif + fprintf (fid, "</DataArray>\n"); + fprintf (fid, "<DataArray type=""Int32"" Name=""offsets"" format=""ascii"">\n"); + fprintf (fid, "%d %d %d %d %d %d\n", (dim+1):(dim+1):((dim+1)*nelems)); + fprintf (fid, "</DataArray>\n"); + fprintf (fid, "<DataArray type=""Int32"" Name=""types"" format=""ascii"">\n"); + fprintf (fid, "%d %d %d %d %d %d\n", eltype*ones(nelems,1)); + fprintf (fid, "</DataArray>\n"); + fprintf (fid, "</Cells>\n"); + +endfunction + +## Print DataPoints +function print_data_points(fid,nodedata,nnodes) + + ## # of data to print in + ## <PointData> field + nvdata = size(nodedata,1); + + if (nvdata) + fprintf (fid, "<PointData>\n"); + for ii = 1:nvdata + data = nodedata{ii,1}; + dataname = nodedata{ii,2}; + nsamples = rows(data); + ncomp = columns(data); + if nsamples != nnodes + error("fpl_vtk_write_field: wrong number of samples in <PointData> ""%s""",dataname); + endif + fprintf (fid,"<DataArray type=""Float32"" Name=""%s"" ",dataname); + fprintf (fid,"NumberOfComponents=""%d"" format=""ascii"">\n",ncomp); + for jj = 1:nsamples + fprintf (fid,"%g ",data(jj,:)); + fprintf (fid,"\n"); + endfor + fprintf (fid,"</DataArray>\n"); + endfor + fprintf (fid, "</PointData>\n"); + endif + +endfunction + +function print_cell_data(fid,celldata,nelems) + + ## # of data to print in + ## <CellData> field + nvdata = size(celldata,1); + + if (nvdata) + fprintf (fid, "<CellData>\n"); + for ii = 1:nvdata + data = celldata{ii,1}; + dataname = celldata{ii,2}; + nsamples = rows(data); + ncomp = columns(data); + if nsamples != nelems + error("fpl_vtk_write_field: wrong number of samples in <CellData> ""%s""",dataname); + endif + fprintf (fid,"<DataArray type=""Float32"" Name=""%s"" ",dataname); + fprintf (fid,"NumberOfComponents=""%d"" format=""ascii"">\n",ncomp); + for jj = 1:nsamples + fprintf (fid,"%g ",data(jj,:)); + fprintf (fid,"\n"); + endfor + fprintf (fid,"</DataArray>\n"); + endfor + fprintf (fid, "</CellData>\n"); + endif + +endfunction \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |