Write data to matlab file

Asked by Melanie Jahny

I'd like to write output data to a Matlab file.
I already did this in an older FEniCS version. Now
I changed to the new Ubuntu version
1:1.0-beta2-2~ppa1~natty1

The following code used to work, but now I
get an error and I can't find a solution to it.

mesh = UnitSquare(10,10)
mesh.order()

V = FunctionSpace(mesh, "Lagrange", 2)
conc = Function(V)

# calculate conc...

conc_file = File("test.m")
conc_file << conc.vector()

The error is:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/dolfin/cpp.py", line 21501, in __init__
    _cpp.File_swiginit(self,_cpp.new_File(*args))
RuntimeError: *** Error: Unknown file type for "test.m".

I'm sorry about asking again something about the new version,
but has anything changed in the syntax (I couldn't find it out),
is something wrong with my installation or isn't it possible anymore
to write Matlab files?

Thanks for any help!

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Johan Hake
Solved:
Last query:
Last reply:
Revision history for this message
Best Johan Hake (johan-hake) said :
#1

A while ago the File interface of DOLFIN was updated. Unfortunately was not
the Matlab File updated, and hence removed. If you are using the Python
interface you can use scipy.io.savemat, and save a numpy representation
(conc.vector().array()) of your tensor.

Johan

On Wednesday November 9 2011 08:30:49 Melanie Jahny wrote:
> New question #178167 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/178167
>
> I'd like to write output data to a Matlab file.
> I already did this in an older FEniCS version. Now
> I changed to the new Ubuntu version
> 1:1.0-beta2-2~ppa1~natty1
>
> The following code used to work, but now I
> get an error and I can't find a solution to it.
>
>
> mesh = UnitSquare(10,10)
> mesh.order()
>
> V = FunctionSpace(mesh, "Lagrange", 2)
> conc = Function(V)
>
> # calculate conc...
>
> conc_file = File("test.m")
> conc_file << conc.vector()
>
> The error is:
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/usr/lib/python2.7/dist-packages/dolfin/cpp.py", line 21501, in
> __init__ _cpp.File_swiginit(self,_cpp.new_File(*args))
> RuntimeError: *** Error: Unknown file type for "test.m".
>
>
> I'm sorry about asking again something about the new version,
> but has anything changed in the syntax (I couldn't find it out),
> is something wrong with my installation or isn't it possible anymore
> to write Matlab files?
>
> Thanks for any help!

Revision history for this message
Andre Massing (massing) said :
#2

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi!

On 11/09/2011 10:30 AM, Melanie Jahny wrote:
> New question #178167 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/178167
>
> I'd like to write output data to a Matlab file. I already did this
> in an older FEniCS version. Now I changed to the new Ubuntu
> version 1:1.0-beta2-2~ppa1~natty1
>
> The following code used to work, but now I get an error and I can't
> find a solution to it.
>
>
> mesh = UnitSquare(10,10) mesh.order()
>
> V = FunctionSpace(mesh, "Lagrange", 2) conc = Function(V)
>
> # calculate conc...
>
> conc_file = File("test.m") conc_file << conc.vector()
>
> The error is:
>
> Traceback (most recent call last): File "<stdin>", line 1, in
> <module> File "/usr/lib/python2.7/dist-packages/dolfin/cpp.py",
> line 21501, in __init__
> _cpp.File_swiginit(self,_cpp.new_File(*args)) RuntimeError: ***
> Error: Unknown file type for "test.m".
>
>
> I'm sorry about asking again something about the new version,

No reason to be sorry, that's what questions at Launchpad are for :)

> but has anything changed in the syntax (I couldn't find it out), is
> something wrong with my installation or isn't it possible anymore
> to write Matlab files?

I ran into the same issue some time ago and IIRC that functionality
has been removed at some point. Actually writing matlab files is still
mentioned in the FEniCS tutorial, maybe that has to be adapted as well?
But the DOLFIN developers may correct me if I am wrong.

Kent-Andre sent me a short code snippet to dump a matrix to
matlab/octave format, maybe that helps you already?

def dump(filename, name, AA):
    f = open(filename, 'w')
    for i in range(AA.shape[0]):
        for j in range(AA.shape[1]):
           if abs(AA[i,j]) > 10e-10:
               f.write("%s (%d, %d) = %e;\n " % (name,i+1,j+1,AA[i,j]))

HTH
Andre

>
> Thanks for any help!
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJOurCiAAoJEA79ggnbq9dmVGcH/2ws06H/aoYjNK/6n0NOyNdC
YSijWKQh0F1YTzp9a158vqQUY7eQx4s+Ae9wRlM9TagCsNOBbYl4XtRMJbk1bwnm
Pz/2hqXFNRYr7R3cxVpH1Qa0Na/0GTy4q8dxmwBHrn/4kxRDFlZBeFQ0IZ+9AFIw
eEs1bRqfWCjAYYSGNJeX6yhGFE5K+E8JmsOdXtogMFhcS4EhUKDCdEMRsEIJEo/w
t/SU+HXbFjYxWYSupXgUlKN8bRSHsZs+D17OHvw68x3E57iUy5uGUXK2pzHlT0kK
4ocsO55nmgQMf15VZnlxn/JGO5T7NIrGKMoHKu9TE5C5pBL+WYSlNstnzJgpF94=
=ZRTL
-----END PGP SIGNATURE-----

Revision history for this message
Anders Logg (logg) said :
#3

On Wed, Nov 09, 2011 at 05:05:55PM -0000, Johan Hake wrote:
> Question #178167 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/178167
>
> Status: Open => Answered
>
> Johan Hake proposed the following answer:
> A while ago the File interface of DOLFIN was updated. Unfortunately was not
> the Matlab File updated, and hence removed. If you are using the Python
> interface you can use scipy.io.savemat, and save a numpy representation
> (conc.vector().array()) of your tensor.

It would be very easy to add MATLAB/Octave output for matrices and
vectors (and very useful). It would take me 2 hours but I don't have
the time at the moment. Patches appreciated.

I've added a blueprint:

https://blueprints.launchpad.net/dolfin/+spec/matlab-octave-output

--
Anders

> Johan
>
> On Wednesday November 9 2011 08:30:49 Melanie Jahny wrote:
> > New question #178167 on DOLFIN:
> > https://answers.launchpad.net/dolfin/+question/178167
> >
> > I'd like to write output data to a Matlab file.
> > I already did this in an older FEniCS version. Now
> > I changed to the new Ubuntu version
> > 1:1.0-beta2-2~ppa1~natty1
> >
> > The following code used to work, but now I
> > get an error and I can't find a solution to it.
> >
> >
> > mesh = UnitSquare(10,10)
> > mesh.order()
> >
> > V = FunctionSpace(mesh, "Lagrange", 2)
> > conc = Function(V)
> >
> > # calculate conc...
> >
> > conc_file = File("test.m")
> > conc_file << conc.vector()
> >
> > The error is:
> >
> > Traceback (most recent call last):
> > File "<stdin>", line 1, in <module>
> > File "/usr/lib/python2.7/dist-packages/dolfin/cpp.py", line 21501, in
> > __init__ _cpp.File_swiginit(self,_cpp.new_File(*args))
> > RuntimeError: *** Error: Unknown file type for "test.m".
> >
> >
> > I'm sorry about asking again something about the new version,
> > but has anything changed in the syntax (I couldn't find it out),
> > is something wrong with my installation or isn't it possible anymore
> > to write Matlab files?
> >
> > Thanks for any help!
>

Revision history for this message
Melanie Jahny (melanie-jahny) said :
#4

Thank you Johan for the quick answer.
I read that I can write Matlab files with scipy, but it was easier to do it
with FEniCS because working with python arrays makes
the program much slower. But I will try using it.

Revision history for this message
Melanie Jahny (melanie-jahny) said :
#5

Thank you Andre for the function code and your help!!! I will try it with
the function.

Revision history for this message
Hans Petter Langtangen (hpl) said :
#6

The latest tutorial teaches the scipy.io.savemat method, which I think is sufficient and very flexible since you can stick a lot of data from the Python program into one dictionary and dump it. There is some overhead in calling conc.vector().array() since this generates a copy of the data, but the writing to file by scipy.io.savemat should take place in fast C code in scipy (haven't checked in detail, but that's the idea of scipy).

Hans Petter

Revision history for this message
Andre Massing (massing) said :
#7

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 11/09/2011 11:56 AM, Hans Petter Langtangen wrote:
> Question #178167 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/178167
>
> Hans Petter Langtangen posted a new comment: The latest tutorial
> teaches the scipy.io.savemat method,

Ok, but does tutorial at the FEniCS webpage
http://128.39.36.31/documentation/tutorial/fundamentals.html
then need an update/ a pull from sources? At least the page there states
"""
This is easily done by opening a File object with a filename extension
.m and dump the Matrix and Vector objects as follows:

mfile = File('A.m'); mfile << A
mfile = File('b.m'); mfile << b
The data files A.m and b.m can be loaded directly into Matlab or Octave.
"""

Cheers,
Andre

> which I think is sufficient and very flexible since you can stick a
> lot of data from the Python program into one dictionary and dump
> it. There is some overhead in calling conc.vector().array() since
> this generates a copy of the data, but the writing to file by
> scipy.io.savemat should take place in fast C code in scipy (haven't
> checked in detail, but that's the idea of scipy).
>
> Hans Petter
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJOusDjAAoJEA79ggnbq9dmBvQH/RBvk/jP5iDiOY5OV4IziGHF
uOdBZasWEm4vblc77+zsqCfzxNRXE1v2JCAUVbZ1FKD5Q1VrcZXaHdr3YFkWOLui
irL4TyLOw6XX1UISHmt3ac72q8TXla4/DVAKWVgc06LGkXiGnmXNB6s4m95xHLK5
/gPMCXS+iOuQW9NdoLrLc1IWFqQ54nB52Sr+8OUbSwJyS8gBNgEv3lISKk5dx3IR
39eXaWCV7s8GTQhKM3inS3LVmcyJDxc2+nzCk0DCzvAjqOJj1bjW8Y0O/UUl9ipN
uNQcFvHvUBo1gvovaozea+e34Lr7QYAaeKD0EQ+oLOPejgy+xZnm5BThkSVci/w=
=Ich8
-----END PGP SIGNATURE-----

Revision history for this message
Hans Petter Langtangen (hpl) said :
#8

The online tutorial is old (has VariationalProblem etc.), new (major revision) is ready but not yet uploaded.

Hans Petter

Revision history for this message
Anders Logg (logg) said :
#9

On Wed, Nov 09, 2011 at 05:56:01PM -0000, Hans Petter Langtangen wrote:
> Question #178167 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/178167
>
> Hans Petter Langtangen posted a new comment:
> The latest tutorial teaches the scipy.io.savemat method, which I think
> is sufficient and very flexible since you can stick a lot of data from
> the Python program into one dictionary and dump it. There is some
> overhead in calling conc.vector().array() since this generates a copy of
> the data, but the writing to file by scipy.io.savemat should take place
> in fast C code in scipy (haven't checked in detail, but that's the idea
> of scipy).

Yes, but that won't help C++ users.

--
Anders