~njansson/dolfin/hpc

« back to all changes in this revision

Viewing changes to misc/utils/matlab/xmlmesh.m

  • Committer: Johannes Ring
  • Date: 2008-03-05 22:43:06 UTC
  • Revision ID: johannr@simula.no-20080305224306-2npsdyhfdpl2esji
The BIG commit!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
function xmlmesh(filename,p,t)
 
2
 
 
3
% XMLMESH - SAVE MATLAB 2D (AND FEMLAB 3D) MESH TO DOLFIN XML FORMAT
 
4
%
 
5
% Usage: xmlmesh(filename,p,t)
 
6
%
 
7
%   p - points    (exported from PDE Toolbox)
 
8
%   t - triangles (exported from PDE Toolbox)
 
9
%
 
10
% Copyright (C) 2004 Erik Svensson.
 
11
% Licensed under the GNU LGPL Version 2.1.
 
12
%
 
13
% Modified by Anders Logg 2004-2005.
 
14
%
 
15
% First added:  2004-02-10
 
16
% Last changed: 2005
 
17
 
 
18
% Open file
 
19
fp = fopen(filename,'w');
 
20
 
 
21
np = size(p,2);
 
22
nt = size(t,2);
 
23
 
 
24
% Write header
 
25
fprintf(fp,'<?xml version="1.0" encoding="UTF-8"?>\n\n');
 
26
fprintf(fp,'<dolfin xmlns:dolfin="http://www.phi.chalmers.se/dolfin/">\n');
 
27
 
 
28
% 2D mesh
 
29
if (size(p,1) == 2)
 
30
 
 
31
  % Write nodes
 
32
  disp('Writing vertices...')
 
33
  fprintf(fp,'  <mesh>\n');
 
34
  fprintf(fp,'    <vertices size="%d">\n',np);  
 
35
  for n=1:np
 
36
    fprintf(fp,'      <vertex name="%d" x="%f" y="%f" z="0.0"/>\n', ...
 
37
            n-1, p(1,n), p(2,n));
 
38
  end
 
39
  fprintf(fp,'    </vertices>\n');
 
40
  
 
41
  % Write cells
 
42
  disp('Writing cells...')
 
43
  fprintf(fp,'    <cells size="%d">\n',nt);
 
44
  for n=1:nt
 
45
    fprintf(fp,'      <triangle name="%d" n0="%d" n1="%d" n2="%d"/>\n', ...
 
46
            n-1,t(1,n)-1,t(2,n)-1,t(3,n)-1);
 
47
  end
 
48
  fprintf(fp,'    </cells>\n');
 
49
  fprintf(fp,'  </mesh>\n');
 
50
  fprintf(fp,'</dolfin>\n');  
 
51
  
 
52
% 3D mesh
 
53
elseif (size(p,1) == 3)
 
54
 
 
55
  % Write nodes
 
56
  disp('Writing nodes...')
 
57
  fprintf(fp,'  <mesh>\n');
 
58
  fprintf(fp,'    <vertices size="%d">\n',np);  
 
59
  for n=1:np
 
60
    fprintf(fp,'      <vertex name="%d" x="%f" y="%f" z="%f"/>\n', ...
 
61
            n-1,p(1,n),p(2,n),p(3,n));
 
62
  end
 
63
  fprintf(fp,'    </vertices>\n');
 
64
  
 
65
  % Write cells
 
66
  disp('Writing cells...')
 
67
  fprintf(fp,'    <cells size="%d">\n',nt);
 
68
  for n=1:nt
 
69
    fprintf(fp,'      <tetrahedron name="%d" n0="%d" n1="%d" n2="%d" n3="%d"/>\n', ...
 
70
            n-1,t(1,n)-1,t(2,n)-1,t(3,n)-1,t(4,n)-1);
 
71
  end
 
72
  fprintf(fp,'    </cells>\n');
 
73
  fprintf(fp,'  </mesh>\n');
 
74
  fprintf(fp,'</dolfin>\n');
 
75
  
 
76
end
 
77
 
 
78
% Close file
 
79
fclose(fp);
 
80
disp('Done')