<div dir="ltr">Barry --<div><br></div><div>Works for me!</div><div><br></div><div>Note that </div><div><br></div><div>import numpy</div><div>import PetscBinaryIO</div><div><span style="font-size:12.8px">fh = open('t.dat','r')</span><br style="font-size:12.8px"><span style="font-size:12.8px">t = numpy.fromfile(fh, dtype='>d')</span><br></div><div><span style="font-size:12.8px">fh.close()</span></div><div><div style=""><span style="font-size:12.8px">io = PetscBinaryIO.PetscBinaryIO()</span></div><div style=""><span style="font-size:12.8px">y = numpy.array(io.readBinaryFile('y.dat')).transpose()</span></div></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">reads the entire trajectory generated by</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px"> -ts_monitor binary:t.dat -ts_monitor_solution binary:y.dat</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Then</span></div><div><span style="font-size:12.8px"><br></span></div><div><div style=""><span style="font-size:12.8px">import matplotlib.pyplot as plt</span></div><div style="font-size:12.8px"><div style="font-size:12.8px">for k in range(np.shape(y)[0]):</div><div style="font-size:12.8px"> plt.plot(t,y[k],label='y[%d]' % k)</div><div style="font-size:12.8px">plt.xlabel('t')</div><div style="font-size:12.8px">plt.legend()</div></div></div><div><br></div><div>plots it with labels. This is only a reasonable for small-dimension ODEs; other vis. methods make more sense for PDEs.</div><div><br></div><div><span style="font-size:12.8px">It is hard to beat the convenience of this way of storing trajectories and doing quick visualizations from python, so I'll probably stick to it. Much appreciated!</span></div><div><span style="font-size:12.8px"><br></span></div><div>Ed</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Mar 2, 2016 at 11:00 AM, Barry Smith <span dir="ltr"><<a href="mailto:bsmith@mcs.anl.gov" target="_blank">bsmith@mcs.anl.gov</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Sorry for the delay. Using master you can now do<br>
<br>
./ex2 -ts_monitor binary:filename -ts_monitor_solution binary:filename<br>
<br>
and it will save each time step followed by each vector solution in the same file called filename.<br>
<br>
The following python script (without error handling and end of file handling) will read in the time steps and solutions<br>
<br>
import PetscBinaryIO<br>
import numpy<br>
io = PetscBinaryIO.PetscBinaryIO()<br>
fh = open('binaryoutput')<br>
while True:<br>
ts = numpy.fromfile(fh, dtype='>d', count=1)<br>
print ts<br>
objecttype = io.readObjectType(fh)<br>
if objecttype == 'Vec':<br>
v = io.readVec(fh)<br>
print v<br>
<br>
I think this what you need/want.<br>
<br>
As Hong noted you could also use -ts_save_trajectory 1 -tstrajectory_type visualization and it will store the timestep and the vector (each timestep/vector is in the same file, one file per timestep.) you could then use a variation of the above python code to read from those files (I think the vector comes first in the trajectory file).<br>
<span class="HOEnZb"><font color="#888888"><br>
Barry<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
> On Mar 1, 2016, at 1:26 AM, Ed Bueler <<a href="mailto:elbueler@alaska.edu">elbueler@alaska.edu</a>> wrote:<br>
><br>
> Barry --<br>
><br>
> I am reading the resulting file successfully using<br>
><br>
> import struct<br>
> import sys<br>
> f = open('timesteps','r')<br>
> while True:<br>
> try:<br>
> bytes = f.read(8)<br>
> except:<br>
> print "f.read() failed"<br>
> sys.exit(1)<br>
> if len(bytes) > 0:<br>
> print struct.unpack('>d',bytes)[0]<br>
> else:<br>
> break<br>
> f.close()<br>
><br>
> However, was there a more elegant intended method? I am disturbed by the apparent need to specify big-endian-ness (= '>d') in the struct.unpack() call.<br>
><br>
> Ed<br>
><br>
><br>
> On Mon, Feb 29, 2016 at 9:39 PM, Ed Bueler <<a href="mailto:elbueler@alaska.edu">elbueler@alaska.edu</a>> wrote:<br>
> Barry --<br>
><br>
> Will try it.<br>
><br>
> > ... since, presumably, other more powerful IO tools exist that would be used for "real" problems?<br>
><br>
> I know there are tools for snapshotting from PETSc, e.g. VecView to .vtk. In fact petsc binary seems fairly convenient for that. On the other hand, I am not sure I've ever done anything "real". ;-)<br>
><br>
> Anyone out there: Are there a good *convenient* tools for saving space/time-series (= movies) from PETSc TS? I want to add frames and movies from PETSc into slides, etc. I can think of NetCDF but it seems not-very-convenient, and I am worried not well-supported from PETSc. Is setting up TS with events (=TSSetEventMonitor()) and writing separate snapshot files the preferred scalable usage, despite the extra effort compared to "-ts_monitor_solution binary:foo.dat"?<br>
><br>
> Ed<br>
><br>
><br>
> On Mon, Feb 29, 2016 at 8:53 PM, Barry Smith <<a href="mailto:bsmith@mcs.anl.gov">bsmith@mcs.anl.gov</a>> wrote:<br>
><br>
> Ed,<br>
><br>
> I have added a branch barry/feature-ts-monitor-binary that supports -ts_monitor binary:timesteps that will store in simple binary format each of the time steps associated with each solution. This in conjugation with -ts_monitor_solution binary:solutions will give you two files you can read in. But note that timesteps is a simple binary file of double precision numbers you should read in directly in python, you cannot use PetscBinaryIO.py which is what you will use to read in the solutions file.<br>
><br>
> Barry<br>
><br>
> Currently PETSc has a binary file format where we can save Vec, Mat, IS, each is marked with a type id for PetscBinaryIO.py to detect, we do not have type ids for simple double precision numbers or arrays of numbers. This is why I have no way of saving the time steps in a way that PetscBinaryIO.py could read them in currently. I don't know how far we want to go in "spiffing up" the PETSc binary format to do more elaborate things since, presumably, other more power IO tools exist that would be used for "real" problems?<br>
><br>
><br>
> > On Feb 29, 2016, at 3:24 PM, Ed Bueler <<a href="mailto:elbueler@alaska.edu">elbueler@alaska.edu</a>> wrote:<br>
> ><br>
> > Dear PETSc --<br>
> ><br>
> > I have a short C ode code that uses TS to solve y' = g(t,y) where y(t) is a 2-dim'l vector. My code defaults to -ts_type rk so it does adaptive time-stepping; thus using -ts_monitor shows times at stdout:<br>
> ><br>
> > $ ./ode -ts_monitor<br>
> > solving from t0 = 0.000 with initial time step dt = 0.10000 ...<br>
> > 0 TS dt 0.1 time 0.<br>
> > 1 TS dt 0.170141 time 0.1<br>
> > 2 TS dt 0.169917 time 0.270141<br>
> > 3 TS dt 0.171145 time 0.440058<br>
> > 4 TS dt 0.173931 time 0.611203<br>
> > 5 TS dt 0.178719 time 0.785134<br>
> > 6 TS dt 0.0361473 time 0.963853<br>
> > 7 TS dt 0.188252 time 1.<br>
> > error at tf = 1.000 : |y-y_exact|_inf = 0.000144484<br>
> ><br>
> > I want to output the trajectory in PETSc binary and plot it in python using bin/PetscBinaryIO.py. Clearly I need the times shown above to do that.<br>
> ><br>
> > Note "-ts_monitor_solution binary:XX" gives me a binary file with only y values in it, but not the corresponding times.<br>
> ><br>
> > My question is, how to get those times in either the same binary file (preferred) or separate binary files? I have tried<br>
> ><br>
> > $ ./ode -ts_monitor binary:foo.dat # invalid<br>
> > $ ./ode -ts_monitor_solution binary:bar.dat # no t in file<br>
> > $ ./ode -ts_monitor_solution binary:baz.dat -ts_save_trajectory # no t in file<br>
> ><br>
> > without success. (I am not sure what the boolean option -ts_save_trajectory does, by the way.)<br>
> ><br>
> > Thanks!<br>
> ><br>
> > Ed<br>
> ><br>
> > PS Sorry if this is a "RTFM" question, but so far I can't find the documentation.<br>
> ><br>
> ><br>
> > --<br>
> > Ed Bueler<br>
> > Dept of Math and Stat and Geophysical Institute<br>
> > University of Alaska Fairbanks<br>
> > Fairbanks, AK 99775-6660<br>
> > 301C Chapman and 410D Elvey<br>
> > 907 474-7693 and 907 474-7199 (fax 907 474-5394)<br>
><br>
><br>
><br>
><br>
> --<br>
> Ed Bueler<br>
> Dept of Math and Stat and Geophysical Institute<br>
> University of Alaska Fairbanks<br>
> Fairbanks, AK 99775-6660<br>
> 301C Chapman and 410D Elvey<br>
> 907 474-7693 and 907 474-7199 (fax 907 474-5394)<br>
><br>
><br>
><br>
> --<br>
> Ed Bueler<br>
> Dept of Math and Stat and Geophysical Institute<br>
> University of Alaska Fairbanks<br>
> Fairbanks, AK 99775-6660<br>
> 301C Chapman and 410D Elvey<br>
> 907 474-7693 and 907 474-7199 (fax 907 474-5394)<br>
<br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">Ed Bueler<br>Dept of Math and Stat and Geophysical Institute<br>University of Alaska Fairbanks<br>Fairbanks, AK 99775-6660<br>301C Chapman and 410D Elvey<br>907 474-7693 and 907 474-7199 (fax 907 474-5394)</div>
</div>