ParaView Calculator atan2
Asked Answered
M

1

6

I'm currently trying to convert given Cartesian coordinates (x,y,z) to spherical coordinates (r, theta, phi) using the ParaView Calculator-Filter, where theta is the polar angle and phi the azimuthal angle. This I want to do over a domain of a quarter-sphere:

(r in [r_inn, r_out], theta in [0, pi], phi in [0, 2pi].

So far I defined the following result variables that give the expected results:

r = sqrt(coordsX^2 + coordsY^2 + coordsZ^2)

theta = acos(coordsZ/r)

For the azimuthal vector I'm aware that I would have to take care of the quadrant of (x,y) when using

phi = atan(y/x).

This usually is achieved using an extra function like atan2 in C. Such a function does not seem to be supplied by the Calculator Filter or the Python Calculator Filter.

Is there any easy way to implementing something like the atan2 using the graphical interface?

Any comments are much appreciated, thanks!

UPDATE:

After Neil Twist pointed out, that in the Python Calculator the inverse tangent function can be called as arctan2(y, x), I'm now facing the problem that I can't access the coordinates of a cell via the variables coordsX/Y/Z, that are available in the simple Calculator filter.

Now, the question is: How can I access the cell coordinates in the Python calculator?

Melson answered 19/10, 2016 at 8:29 Comment(0)
I
7

You can use the numpy extensions of the Python Calculator in ParaView, but numpy have called the function arctan2 rather than atan2.

There are numpy docs for trigonometric functions, but annoyingly you can't use all the functions directly, for example you can do arctan2(x1, x2), but you can't do pi and have to use numpy.pi.

For context, there are PythonCalculator docs too.

To access the coordsX and coordsY is a bit tricky, but can be achieved using the points variable. This is actually an array of all the points, each of which is an array of x, y and z coords.

To use the co-ordinates you need to extract them like so:

[point[0] for point in points]
[point[1] for point in points]
[point[2] for point in points]

So to use the arctan function with Y and X coords, you could do the following:

arctan2([point[1] for point in points], [point[0] for point in points])

Update: After a little more investigation, there may be a slightly nicer way to get coordsX/Y/Z:

points[:,0]
points[:,1]
points[:,2]

giving

arctan2(points[:,1], points[:,0])

Another helpful reference is the numpy_interface algorithms.

Inquietude answered 27/10, 2016 at 11:20 Comment(7)
Hi, first thanks a lot for pointing this out. I had looked for some documentation, but could not come up with anything that helped me. I tried to call the function as arctan2(coordsY, coordsX), which now gives me an error that 'coordsY is not defined'. These coordinate names worked for the simple Calculator. Seems I'll have to find an alternative for the Python one. Thanks again!Melson
@ncim, you probably wouldn't have found the python calc docs there unless you are looking at programming in the python console (or using pvpython or pvbatch). And as for why numpy had to change the name...?!Inquietude
I agree on the naming issue, it seems to be a common point where people can make mistakes. As mentioned above, I am now facing the problem, that the coordinates of a cell are not longer available via the variables coordsX, ..Y, ..Z. In the PythonCalculator documentation I read, that 'points can be accessed using the points variable'. I can imagine that I could get the x-coordinate with an expression like 'points.x'. Do you know how this can be achieved?Melson
Ahhh, this is a pain. Update you're question and I will update my answer for completeness.Inquietude
this is exactly what I was looking for, thanks a lot!Melson
I'm new to this, accepting means clicking the "up-arrow", right? When I do that, it says that my acceptance has been noted but won't be shown publicly, as I have less than 15 reputation.Melson
There should be a shadowed tick I think?Inquietude

© 2022 - 2024 — McMap. All rights reserved.