<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<blockquote type="cite"> I actually am increasing my extents
(extent[1]+1 for example) to compensate for this,</blockquote>
yes you added one. that is needed to compute the number of cells
from the extents. you still need to add one to the result (the
number of cells) to get the number of points. As is, you are not
looking up the values you think you are in your input array. You
won't crash since you'll stay within the point array bounds.<br>
<br>
for cells compute the number of them like this:<br>
<blockquote><tt>ncx = ext[1]-ext[0]+1</tt><br>
<tt> ncy = ext[3]-ext[2]+1</tt><br>
<tt>ncxy = ncx*ncy</tt><br>
<tt>ncz = ext[3]-ext[2]+1</tt><br>
</blockquote>
and in your loop index a cell at i,j,k like this:<br>
<blockquote><tt>cidx = k*ncxy + j*ncx+i</tt><br>
</blockquote>
For points compute the number of them like this:<br>
<blockquote><tt>npx = ncx+1</tt><br>
<tt>npy = ncy+1</tt><br>
<tt>npxy = npx*npy</tt><br>
<tt>npz = ncz+1</tt><br>
</blockquote>
and in your loop index a point at i,j,k like this:<br>
<blockquote><tt>pidx = k*npxy + j*npx+i</tt><br>
</blockquote>
<br>
<blockquote type="cite">I agree that i should be the fastest
changing index in some sense, but because I have to step over all
values of i,j,k to read the entire array into my python array, I
don't see why any other looping direction would be quicker?</blockquote>
This has to do with the cache inside your cpu. When you use a memory
location the cpu automatically loads the adjacent locations(a cache
line) into the cache. In memory i direction changes fastest, meaning
location (i+1,j,k) is adjacent following location (i,j,k). when you
access the adjacent memory locations in your loops sequentially your
data are more likely to be in the cache and hence a faster. On the
other hand memory location (i,j,k+1) is npx*npy elements away from
location (i,j,k). That's usually quite a number of cache lines away
and hence less likely to already be in the cache resulting in a
cache miss. Execution stalls while the cpu loads the cache line. the
cache is fairly small so a cache miss likely results in some data
being evicted to make room for the new data. When this happens on
every array access it's called thrashing.This is what happens when
you use the wrong order in your loops.<br>
<br>
<blockquote type="cite">I think you're right, it does seem to
construct a list explicitly, but I don't really know what the
alternative is?</blockquote>
I am not a great python programmer, but one way would be to use a
while loop:<br>
<br>
<tt>k=ext[4]<br>
while(k<=ext[5]):<br>
#<br>
j=ext[2]<br>
while(j<=ext[3]):<br>
#<br>
i=ext[0]<br>
while(i<=ext[1]):<br>
#<br>
pidx = k*npxy + j*npx+i<br>
# do something here<br>
i+=1<br>
#<br>
j+=1<br>
#<br>
k+=1<br>
#</tt><br>
<br>
On 10/28/2011 03:27 PM, stofem wrote:
<blockquote
cite="mid:CADi1s15ZMfA0znvf=QLE+QBOWbQ+YSK26x=H2kKAGM0XN4dtzg@mail.gmail.com"
type="cite">Hi,
<div><br>
</div>
<div>Thanks for your help both of you. </div>
<div><br>
</div>
<div>Cheers for pointing out my Cell-Point mistake Burlen, I
actually am increasing my extents (extent[1]+1 for example) to
compensate for this, but I did make a mistake in my final call </div>
<div><br>
</div>
<div><span class="Apple-style-span" style="font-family:
arial,sans-serif; font-size: 13px; background-color: rgb(255,
255, 255);"><b>out.GetCellData().AddArray(newArray)</b></span></div>
<div><span class="Apple-style-span" style="font-family:
arial,sans-serif; font-size: 13px; background-color: rgb(255,
255, 255);"><b><br>
</b></span></div>
<div>which should be getting PointData, not CellData. My filter
now works as planned :)</div>
<div><br>
</div>
<div>However I am interested in some of your comments. Being a new
Python/Paraview/VTK programmer I would appreciate a bit of
further advice:</div>
<div><br>
</div>
<blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex;
border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<span class="Apple-style-span" style="font-family:
arial,sans-serif; font-size: 13px; background-color: rgb(255,
255, 255);">In the two first loops you are treating 'i' as the
slowest changing direction when it is the fastest. That will
kill your performance. Last loop has correct order.</span></blockquote>
<div><br>
</div>
<div>I agree that i should be the fastest changing index in some
sense, but because I have to step over all values of i,j,k to
read the entire array into my python array, I don't see why any
other looping direction would be quicker?</div>
<div><br>
</div>
<blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex;
border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<span class="Apple-style-span" style="font-family:
arial,sans-serif; font-size: 13px; background-color: rgb(255,
255, 255);">You may want to avoid the range function in your
loop since if my recollection is correct that explicitly
constructs a list of values</span></blockquote>
<div><br>
</div>
<div>I think you're right, it does seem to construct a list
explicitly, but I don't really know what the alternative is?</div>
<div><br>
</div>
<div>Thanks again.</div>
<div><br>
</div>
<div>Looking forward to seeing that new filter built in!</div>
<br>
<div class="gmail_quote">On Sat, Oct 29, 2011 at 3:08 AM, Burlen
Loring <span dir="ltr"><<a moz-do-not-send="true"
href="mailto:bloring@lbl.gov">bloring@lbl.gov</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt
0.8ex; border-left: 1px solid rgb(204, 204, 204);
padding-left: 1ex;">
<div bgcolor="#ffffff" text="#000000"> Hi,<br>
<br>
You haven't configured your output, assuming rectilinear
grid you need to set the extent and provide coordinate
arrays.<br>
<br>
You are indexing a point based array with cell based index,
so the lookup you make into vtkVarray is incorrect. 'Extent'
tells you the number of cells, not the number of points, for
that use 'Dimensions' or add one in each direction. Your
script would certainly be more readable if you used some
variables such as:<br>
<br>
ncx = ext[1]-ext[0]+1<br>
ncy = ext[3] -ext[2] +1<br>
ncxy = nx*ny<br>
ncz = ext[5]-ext[4] +1<br>
cidx = k*nxy + j*nx+i<br>
<br>
for number of cells and a cell array index.<br>
<br>
In the two first loops you are treating 'i' as the slowest
changing direction when it is the fastest. That will kill
your performance. Last loop has correct order.<br>
<br>
You may want to avoid the range function in your loop since
if my recollection is correct that explicitly constructs a
list of values.<br>
<br>
I hope this is helpful.<br>
Burlen
<div>
<div class="h5"><br>
<br>
On 10/26/2011 10:41 PM, Mr FancyPants wrote: </div>
</div>
<blockquote type="cite">
<div>
<div class="h5">Hi there,
<div><br>
</div>
<div>I am trying to write a programmable filter which
will take my RectilinearGrid data and modify one of
the data arrays. Specifically I want to sum over one
dimension. I have written something to do this, but
my data comes out garbled.</div>
<div><br>
</div>
<div>Below is my code. Basically all I am doing is
extracting all the data, doing my sum over the z
direction and then putting this data into another
rectilinear grid.</div>
<div><br>
</div>
<div>
<div><br>
</div>
<div><b>data=self.GetInput()</b></div>
<div><b>out=self.GetOutput()</b></div>
<div><b>extent=data.GetExtent()</b></div>
<div><b>vtkVarray=data.GetPointData().GetArray('velocity')</b></div>
<div><b><br>
</b></div>
<div><b>import numpy</b></div>
<div><b>pyVarray_x =
numpy.zeros([extent[1]+1,extent[3]+1,extent[5]+1])</b></div>
<div><b>pyVarray_y =
numpy.zeros([extent[1]+1,extent[3]+1,extent[5]+1])</b></div>
<div><b>pyVarray_z =
numpy.zeros([extent[1]+1,extent[3]+1,extent[5]+1])</b></div>
<div><b><br>
</b></div>
<div><b>for i in range(0,extent[1]+1):</b></div>
<div><b><span style="white-space: pre-wrap;"><br>
</span>for j in range(0,extent[3]+1):</b></div>
<div><b><span style="white-space: pre-wrap;"><br>
</span>for k in range(0,extent[5]+1):</b></div>
<div><b><span style="white-space: pre-wrap;"><br>
</span>tup=vtkVarray.GetTuple(i+j*(extent[1]+1)+k*(extent[1]+1)*(extent[3]+1))</b></div>
<div><b><span style="white-space: pre-wrap;"><br>
</span>pyVarray_x[i,j,k]=tup[0]</b></div>
<div><b><span style="white-space: pre-wrap;"><br>
</span>pyVarray_y[i,j,k]=tup[1]</b></div>
<div><b><span style="white-space: pre-wrap;"><br>
</span>pyVarray_z[i,j,k]=tup[2]<span
style="white-space: pre-wrap;"> </span></b></div>
<div><b><br>
</b></div>
<div><b><br>
</b></div>
<div><b><br>
</b></div>
<div><b>pyVarray_x_noz =
numpy.zeros([extent[1]+1,extent[3]+1,1])</b></div>
<div><b>pyVarray_y_noz =
numpy.zeros([extent[1]+1,extent[3]+1,1])</b></div>
<div> <b>pyVarray_z_noz =
numpy.zeros([extent[1]+1,extent[3]+1,1])</b></div>
<div><b><br>
</b></div>
<div><b>for i in range(0,extent[1]+1):</b></div>
<div><b><span style="white-space: pre-wrap;"><br>
</span>for j in range(0,extent[3]+1):</b></div>
<div><b><span style="white-space: pre-wrap;"><br>
</span>pyVarray_x_noz[i,j]=pyVarray_x[i,j,:].sum()</b></div>
<div><b><span style="white-space: pre-wrap;"><br>
</span>pyVarray_y_noz[i,j]=pyVarray_y[i,j,:].sum()</b></div>
<div><b><span style="white-space: pre-wrap;"><br>
</span>pyVarray_z_noz[i,j]=pyVarray_z[i,j,:].sum()</b></div>
<div><b><br>
</b></div>
<div><b>newArray=vtk.vtkDoubleArray()</b></div>
<div><b>newArray.SetName("test")</b></div>
<div><b>newArray.SetNumberOfComponents(3)</b></div>
<div><b><br>
</b></div>
<div><b>print newArray</b></div>
<div><b><br>
</b></div>
<div><b>for k in range(0,extent[5]+1):</b></div>
<div><b><span style="white-space: pre-wrap;"><br>
</span>for j in range(0,extent[3]+1):</b></div>
<div><b><span style="white-space: pre-wrap;"><br>
</span>for i in range(0,extent[1]+1):</b></div>
<div><b><span style="white-space: pre-wrap;"><br>
</span>tup0=pyVarray_x_noz[i,j]</b></div>
<div> <b><span style="white-space: pre-wrap;"> </span>tup1=pyVarray_y_noz[i,j]</b></div>
<div><b><span style="white-space: pre-wrap;"><br>
</span>tup2=pyVarray_z_noz[i,j]</b></div>
<div><b><span style="white-space: pre-wrap;"><br>
</span>tup=(tup0,tup1,tup2)</b></div>
<div><b><span style="white-space: pre-wrap;"><br>
</span>newArray.InsertNextTuple((tup0,tup1,tup2))</b></div>
<div><b><br>
</b></div>
<div><b>print newArray</b></div>
<div><b>print vtkVarray</b></div>
<div><b><br>
</b></div>
<div><b>out.GetCellData().AddArray(newArray)</b></div>
</div>
<div><b><br>
</b></div>
<div>I have no idea whats going wrong with this. Any
help is much appreciated, new to using paraview.</div>
<div><br>
</div>
<div>Thanks, James</div>
</div>
</div>
<pre><fieldset></fieldset>
_______________________________________________
Powered by <a moz-do-not-send="true" href="http://www.kitware.com" target="_blank">www.kitware.com</a><div class="im">
Visit other Kitware open-source projects at <a moz-do-not-send="true" href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a>
Please keep messages on-topic and check the ParaView Wiki at: <a moz-do-not-send="true" href="http://paraview.org/Wiki/ParaView" target="_blank">http://paraview.org/Wiki/ParaView</a>
Follow this link to subscribe/unsubscribe:
<a moz-do-not-send="true" href="http://www.paraview.org/mailman/listinfo/paraview" target="_blank">http://www.paraview.org/mailman/listinfo/paraview</a>
</div></pre>
</blockquote>
<br>
</div>
</blockquote>
</div>
<br>
</blockquote>
<br>
</body>
</html>