~njansson/dolfin/hpc

« back to all changes in this revision

Viewing changes to misc/utils/matlab/xmlmatrix.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 xmlmatrix(filename, A)
 
2
 
 
3
% XMLMATRIX - SAVE MATRIX TO DOLFIN IN XML FORMAT
 
4
%
 
5
% Usage: xmlmatrix(filename, A)
 
6
%
 
7
%   A - a matrix
 
8
%
 
9
% Copyright (C) 2004 Georgios Foufas.
 
10
% Licensed under the GNU LGPL Version 2.1.
 
11
%
 
12
% Modified by Anders Logg 2004-2005.
 
13
%
 
14
% First added:  2004-02-10
 
15
% Last changed: 2005
 
16
 
 
17
% Tolerance for sparse matrix
 
18
tol = 1e-16;
 
19
 
 
20
% Open file
 
21
fp = fopen(filename,'w');
 
22
 
 
23
%Get matrix size
 
24
nrows = size(A,1);
 
25
ncols = size(A,2);
 
26
 
 
27
% Write header
 
28
fprintf(fp,'<?xml version="1.0" encoding="UTF-8"?>\n\n');
 
29
fprintf(fp,'<dolfin xmlns:dolfin="http://www.phi.chalmers.se/dolfin/">\n');
 
30
 
 
31
% Write matrix values
 
32
disp('Writing matrix...')
 
33
fprintf(fp,'  <sparsematrix rows="%d" columns="%d">\n',nrows,ncols);  
 
34
 
 
35
for i=1:nrows
 
36
  
 
37
  % Compute size of row
 
38
  size = round(length(find(abs(A(i,:)) > tol)));
 
39
  
 
40
  fprintf(fp,'    <row row="%d" size="%d"/>\n', i-1, size);
 
41
  
 
42
  for j=1:ncols
 
43
    element = A(i,j);
 
44
    if abs(element) > tol
 
45
      fprintf(fp,'      <element column="%d" value="%f"/>\n', j-1, A(i,j));
 
46
    end
 
47
  end
 
48
  
 
49
  fprintf(fp,'    </row>\n');
 
50
 
 
51
end
 
52
 
 
53
fprintf(fp,'  </sparsematrix>\n');
 
54
fprintf(fp,'</dolfin>\n');  
 
55
 
 
56
% Close file
 
57
fclose(fp);
 
58
disp('Done')