← Back to team overview

dolfin team mailing list archive

Re: python: xml read for vector / std::vector

 

On Monday August 9 2010 04:42:51 Anders Logg wrote:
> On Fri, Aug 06, 2010 at 01:37:12PM -0700, Johan Hake wrote:
> > On Friday August 6 2010 12:10:34 Anders Logg wrote:
> > > On Fri, Aug 06, 2010 at 07:57:52PM +0100, Garth N. Wells wrote:
> > > > On Fri, 2010-08-06 at 11:55 -0700, Johan Hake wrote:
> > > > > On Friday August 6 2010 11:48:37 Anders Logg wrote:
> > > > > > On Wed, Aug 04, 2010 at 05:06:57PM -0700, Johan Hake wrote:
> > > > > > > On Wednesday August 4 2010 13:09:47 Riesen Patrick wrote:
> > > > > > > > -----Original Message-----
> > > > > > > > From: Garth N. Wells [mailto:gnw20@xxxxxxxxx]
> > > > > > > > Sent: Wed 04.08.2010 19:07
> > > > > > > > To: johan.hake@xxxxxxxxx
> > > > > > > > Cc: Riesen  Patrick; dolfin@xxxxxxxxxxxxxxxxxxx
> > > > > > > > Subject: Re: [Dolfin] python: xml read for vector /
> > > > > > > > std::vector
> > > > > > > > 
> > > > > > > > On Wed, 2010-08-04 at 10:03 -0700, Johan Hake wrote:
> > > > > > > > > On Wednesday August 4 2010 09:28:22 Patrick Riesen wrote:
> > > > > > > > > > Patrick Riesen wrote:
> > > > > > > > > > > Johan Hake wrote:
> > > > > > > > > > >> Patrick!
> > > > > > > > > > >> 
> > > > > > > > > > >> On Wednesday August 4 2010 09:13:24 Patrick Riesen 
wrote:
> > > > > > > > > > >>> hello dolfin developers
> > > > > > > > > > >>> 
> > > > > > > > > > >>> I write out a std::vector in XML file format from
> > > > > > > > > > >>> cpp. Can I read the XML file in again in python and
> > > > > > > > > > >>> construct a numpy array? I know how to do it with a
> > > > > > > > > > >>> dolfin Vector, but is it possible with a
> > > > > > > > > > >>> std::vector<double>? I could not figure out the
> > > > > > > > > > >>> syntax, i tried
> > > > > > > > > > >>> 
> > > > > > > > > > >>> # this is ok
> > > > > > > > > > >>> fn = File("data/norm.xml")
> > > > > > > > > > >>> div_vec = d.Vector()
> > > > > > > > > > >>> a = n.array((11,1))
> > > > > > > > > > >>> 
> > > > > > > > > > >>> # fails
> > > > > > > > > > >>> fn >> a
> > > > > > > > > > >>> # fails
> > > > > > > > > > >>> fn >> div_vec.array()
> > > > > > > > > > >> 
> > > > > > > > > > >> Have you tried:
> > > > > > > > > > >>   fn >> div_vec
> > > > > > > > > > >>   a = div_vec.array()
> > > > > > > > > > > 
> > > > > > > > > > > yes, that gives the error:
> > > > > > > > > > > *** Error: Handler stack not empty. Something is wrong!
> > > > > > > > > 
> > > > > > > > > The format is then different for the Vector XML file and
> > > > > > > > > the std::vector file?
> > > > > > > > > 
> > > > > > > > > > if i write out a dolfin::Vector in xml, it's all fine
> > > > > > > > > > with:
> > > > > > > > > > 
> > > > > > > > > > fn = File("data/norm.xml")
> > > > > > > > > > div_vec = d.Vector()
> > > > > > > > > > fn >> div_vec
> > > > > > > > > > a = div_vec.array()
> > > > > > > > > > 
> > > > > > > > > > but if i write out a std::vector<double> in xml, I can't
> > > > > > > > > > read it in.
> > > > > > > > > 
> > > > > > > > > Silly question from a Python user:
> > > > > > > > >   How do you write the std::vector to a XML file?
> > > > > > > > 
> > > > > > > > You don't ;);
> > > > > > > > 
> > > > > > > > DOLFIN does. It's overloaded in the XMLFile class.
> > > > > > > > Well, anyway, i thought if dolfin can write out a std::vector
> > > > > > > > to xml via File/XMLFile, pydolfin can also read it in, but
> > > > > > > > obviously not.
> > > > > > > 
> > > > > > > I think you stumble across some unused code there. I am not
> > > > > > > sure but I think Ola implemented this for some debugging of
> > > > > > > the parallel interface. But I am not sure actually.
> > > > > > > 
> > > > > > > If we had a typemap for std::vector <-> numpy array (which we
> > > > > > > could have, but
> > > > > > > 
> > > > > > > desided we did not want...) we sould be able to do:
> > > > > > >   somefile >> array
> > > > > > >   somefile << array
> > > > > > > 
> > > > > > > Where somefile will be file generated by XMLArray, which
> > > > > > > probably define a different XML interfaces than XMLVector.
> > > > > > 
> > > > > > Input/output is implemented for the Array class so it would be
> > > > > > appropriate to use that. It was implemented to handle
> > > > > > input/output for parallel mesh data (stored in the data section
> > > > > > of a mesh as MeshFunctions, Arrays and maps).
> > > > > 
> > > > > Ok
> > > > > 
> > > > > > Can someone remind me of why we don't have a typemap for
> > > > > > numpy.array and dolfin::Array? Or what am I missing here?
> > > > > 
> > > > > We have that, but the XMLArray read and writes  std::vector, which
> > > > > typemaps we decided to ditch.
> > > > 
> > > > The best solution is to use dolfin::Array in XMLArray. It will also
> > > > simplify io of dolfin::Vector.
> > > 
> > > Aha. I missed that. I suggest we support input/output for both
> > > dolfin::Array and std::vector. It should be possible to implement
> > > with a good deal of code reuse.
> > 
> > Another thing, is it anything that prevent us to harmonize the xml format
> > of an XMLArray and an XMLVector? Any potential issues during parallel?
> > If so one should be able to do what Patrick did in the first place.
> > 
> > > Speaking of which, would it be possible to add a constructor to
> > > dolfin::Array that takes a std::vector?
> > > 
> > >   std::vector<int> x(10);
> > >   
> > >   dolfin::Array<int> xx(x);
> > > 
> > > ?
> > > 
> > > I have been missing that on a few occasions.
> > 
> > That should be possible with copying of data of course.
> 
> I meant without copying. Isn't that one of the main points with the
> Array class that it is flexible in terms of ownership?

Yes you are probably right there. We then need to wrap a shared_array around 
the pointer to the first element of the std::vector in a constructor. The 
vector can then of course not go out of scope. We do have a problem if a user 
resize the std::vector though.

Johan

Johan



References