]> Creatis software - CreaPhase.git/blob - octave_packages/fpl-1.2.0/FPL2dxoutputdata.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / fpl-1.2.0 / FPL2dxoutputdata.m
1 ## Copyright (C) 2004-2008,2009  Carlo de Falco, Massimiliano Culpo
2 ##
3 ##  This file is part of 
4 ##
5 ##                   FPL - Fem PLotting package for octave
6 ## 
7 ##  FPL is free software; you can redistribute it and/or modify
8 ##  it under the terms of the GNU General Public License as published by
9 ##  the Free Software Foundation; either version 2 of the License, or
10 ##  (at your option) any later version.
11 ## 
12 ##  FPL is distributed in the hope that it will be useful,
13 ##  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ##  GNU General Public License for more details.
16 ## 
17 ##  You should have received a copy of the GNU General Public License
18 ##  along with FPL; If not, see <http://www.gnu.org/licenses/>.
19 ##
20 ##
21 ##  AUTHORS:
22 ##  Carlo de Falco <cdf _AT_ users.sourceforge.net>
23 ##
24 ##  Culpo Massimiliano
25 ##  Bergische Universitaet Wuppertal
26 ##  Fachbereich C - Mathematik und Naturwissenschaften
27 ##  Arbeitsgruppe fuer Angewandte MathematD-42119 Wuppertal  Gaussstr. 20 
28 ##  D-42119 Wuppertal, Germany
29
30 ## -*- texinfo -*-
31 ##  @deftypefn {Function File} {} FPL2dxoutputdata ( @var{filename}, @
32 ##  @var{p}, @var{t}, @var{u},  @var{attr_name}, @var{attr_rank},  @
33 ##  @var{attr_shape}, @var{endfile} )
34 ##
35 ##   Outputs data in DX form.
36 ##
37 ##   Variable must be a scalar, vector or tensor of doubles   
38 ##
39 ## @itemize @minus
40 ##  @item @var{filename}= name of file to save (type string)
41 ##  @item @var{p}, @var{t} = mesh
42 ##  @item @var{u} = variable to save
43 ##  @item @var{attr_name}  = name of the variable (type string)
44 ##  @item @var{attr_rank}  = rank of variable data (0 for scalar, 1 for vector, etc.)
45 ##  @item @var{attr_shape} = number of components of variable data  (assumed 1 for scalar)
46 ##  @item @var{endfile} = 0 if you want to add other variables to the
47 ##  same file, 1 otherwise
48 ## @end itemize
49 ## @end deftypefn
50
51 function FPL2dxoutputdata(filename,p,t,u,attr_name,attr_rank,attr_shape,endfile)
52
53   p = p';
54   t = t';
55   t = t(:,1:3);
56
57   fid=fopen (filename,'w');
58   Nnodi = size(p,1);
59   Ntriangoli = size(t,1);
60   Ndati = size(u,1);
61
62   fprintf(fid,"object ""pos""\nclass array type float rank 1 shape 2 items %d data follows",Nnodi);
63
64   for i=1:Nnodi
65     fprintf(fid,"\n");
66     fprintf(fid,"    %1.7e",p(i,:));
67   endfor
68                   
69   if (min(min(t))==1)
70     t=t-1;
71   elseif(min(min(t))~=0)
72     disp('WARNING: check triangle structure')
73   endif                    
74   ## In DX format nodes are 
75   ## numbered starting from zero,
76   ## instead we want to number
77   ## them starting from 1!
78   ## Here we restore the DX
79   ## format
80
81   fprintf(fid,"\n\nobject ""con""\nclass array type int rank 1 shape 3 items %d data follows",Ntriangoli);
82   for i=1:Ntriangoli
83     fprintf(fid,"\n");
84     fprintf(fid,"      %d",t(i,:));
85   endfor
86                                   
87   fprintf(fid,"\nattribute ""element type"" string ""triangles""\nattribute ""ref"" string ""positions""\n\n");
88                                   
89   if ((attr_rank==0) & (min(size(u))==1))
90     fprintf(fid,"object ""%s.data""\nclass array type double rank 0 items %d data follows",attr_name,Ndati);
91     fprintf(fid,"\n %1.7e",u);
92     
93   else
94     fprintf(fid,"object ""%s.data""\nclass array type double rank %d shape %d items %d data follows", ...
95             attr_name,attr_rank,attr_shape,Ndati);
96     for i=1:Ndati
97       fprintf(fid,"\n");
98       fprintf(fid,"    %1.7e",u(i,:));
99     endfor
100     
101   endif
102   
103   if Ndati==Nnodi
104     fprintf(fid,["\nattribute ""dep"" string ""positions""\n\n" ...
105                  "object ""%s"" class field\n"...
106                  "component ""positions"" value ""pos""\n"...
107                  "component ""connections"" value ""con""\n"...
108                  "component ""data"" value ""%s.data""\n"],...
109             attr_name,attr_name);
110   elseif Ndati==Ntriangoli
111     fprintf(fid,["\nattribute ""dep"" string ""connections""\n\n" ...
112                  "object ""%s"" class field\n"...
113                  "component ""positions"" value ""pos""\n"...
114                  "component ""connections"" value ""con""\n"...
115                  "component ""data"" value ""%s.data""\n"],...
116             attr_name,attr_name);
117   endif
118
119   if(endfile)
120     fprintf(fid,"\nend\n");
121   endif
122                                                           
123   fclose (fid);
124   
125   
126 endfunction