~njansson/dolfin/hpc

« back to all changes in this revision

Viewing changes to src/kernel/io/OctaveFile.cpp

  • 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
 
// Copyright (C) 2003-2006 Johan Hoffman and Anders Logg.
2
 
// Licensed under the GNU LGPL Version 2.1.
3
 
//
4
 
// Modified by Erik Svensson, 2003.
5
 
// Modified by Garth N. Wells, 2006.
6
 
//
7
 
// First added:  2003-02-26
8
 
// Last changed: 2006-05-31
9
 
 
10
 
// FIXME: Use streams rather than stdio
11
 
#include <stdio.h>
12
 
 
13
 
#include <dolfin/dolfin_log.h>
14
 
#include <dolfin/Array.h>
15
 
#include <dolfin/Vector.h>
16
 
#include <dolfin/Matrix.h>
17
 
#include <dolfin/MatlabFile.h>
18
 
#include <dolfin/OctaveFile.h>
19
 
 
20
 
using namespace dolfin;
21
 
 
22
 
//-----------------------------------------------------------------------------
23
 
OctaveFile::OctaveFile(const std::string filename) : MFile(filename)
24
 
{
25
 
  type = "Octave";
26
 
}
27
 
//-----------------------------------------------------------------------------
28
 
OctaveFile::~OctaveFile()
29
 
{
30
 
  // Do nothing
31
 
}
32
 
//-----------------------------------------------------------------------------
33
 
void OctaveFile::operator<<(Matrix& A)
34
 
{
35
 
  // Octave file format for Matrix is not the same as the Matlab format,
36
 
  // since octave cannot handle sparse matrices.
37
 
 
38
 
  uint M = A.size(0);
39
 
  uint N = A.size(1);
40
 
  real* row = new real[N];
41
 
  
42
 
  FILE *fp = fopen(filename.c_str(), "a");
43
 
  fprintf(fp, "%s = zeros(%u, %u);\n", A.name().c_str(), M, N);
44
 
  
45
 
  for (uint i = 0; i < M; i++)
46
 
  {
47
 
    // Get nonzero entries
48
 
    int ncols = 0;
49
 
    Array<int> columns;
50
 
    Array<real> values;
51
 
    A.getRow(i, ncols, columns, values);
52
 
 
53
 
    // Write nonzero entries
54
 
    for (int pos = 0; pos < ncols; pos++)
55
 
      fprintf(fp, "%s(%d, %d) = %.16e;\n",
56
 
              A.name().c_str(), (int)i + 1, columns[pos] + 1, values[pos]);
57
 
  }
58
 
  
59
 
  fclose(fp);
60
 
  delete [] row;
61
 
  
62
 
  message(1, "Saved matrix %s (%s) to file %s in Octave format.",
63
 
          A.name().c_str(), A.label().c_str(), filename.c_str());
64
 
}
65
 
//-----------------------------------------------------------------------------