How to read finite element mesh and view it as UnstructuredGrid - paraview

I want to read a finite element mesh (i.e. a matrix with coordinates and a matrix with connectivity) from a HDF5 file and show it in ParaView, using the Python interface.
I know how to do something simple:
from paraview.simple import *
Sphere()
Show()
Render()
But, how to do the mesh? Let's skip the HDF5 part, and focus on this very simple 2-D mesh comprising 2-D quadrilaterals:
from paraview.simple import *
import numpy as np
coor = np.array([
[ 0.0 , 0.0 ],
[ 1.0 , 0.0 ],
[ 2.0 , 0.0 ],
[ 0.0 , 1.0 ],
[ 1.0 , 1.0 ],
[ 2.0 , 1.0 ],
])
conn = np.array([
[ 0 , 1 , 4 , 3 ],
[ 1 , 2 , 5 , 4 ],
])
Where to go from here?
Possible approach
One possible approach could be to build a vtkUnstructuredGrid. But the problem is that I don't know what to do with it. I.e. how do I tell ParaView to use it?
from paraview import vtk
points = vtk.vtkPoints()
for i,(x,y) in enumerate(coor):
points.InsertNextPoint(x,y,0.0)
grid = vtk.vtkUnstructuredGrid()
for el in conn:
cell = vtk.vtkQuad()
for i,ver in enumerate(el):
cell.GetPointIds().SetId(i,ver)
grid.InsertNextCell(cell.GetCellType(),cell.GetPointIds())
grid.SetPoints(points)
It would be extremely helpful to understand how to include cell-data and point-data, but I can probably figure this out starting from the solution to this problem.

First of all, it is important to note that there are a couple different "levels" of Python usage in ParaView. Your first example is from the high level Python interface to ParaView. Most things you can do in the user interface can be done in ParaView at this level through the Python Console or by running a script through the Python Console.
At a lower level, Python can be used to define operations with VTK. Programmable Filter and Programmable Source is where this level of programming is done. It is important to note that the Programmable Filter and Programmable Source do not know anything about ParaView or paraview.simple - they execute their Python scripts in a Python environment where only VTK is available. If you import paraview.simple for example, the behavior is undefined.
For your purposes, lower level Python programming seems appropriate. I would define a Programmable Source, set its Output Data Set Type to vtkUnstructuredGrid. Next, your script will look something like:
import numpy as np
coor = np.array([
[ 0.0 , 0.0 ],
[ 1.0 , 0.0 ],
[ 2.0 , 0.0 ],
[ 0.0 , 1.0 ],
[ 1.0 , 1.0 ],
[ 2.0 , 1.0 ],
])
conn = np.array([
[ 0 , 1 , 4 , 3 ],
[ 1 , 2 , 5 , 4 ],
])
import vtk
grid = self.GetOutput()
points = vtk.vtkPoints()
for i,(x,y) in enumerate(coor):
points.InsertNextPoint(x,y,0.0)
grid.SetPoints(points)
grid.Allocate()
for el in conn:
cell = vtk.vtkQuad()
for i,ver in enumerate(el):
cell.GetPointIds().InsertId(i,ver)
grid.InsertNextCell(cell.GetCellType(),cell.GetPointIds())

Please give credit to #CoryQuammen because it is really his solution
For future reference the full script, including some cell and point data
from paraview.simple import *
paraview.simple._DisableFirstRenderCameraReset()
# create a new 'Programmable Source'
mesh = ProgrammableSource()
mesh.OutputDataSetType = 'vtkUnstructuredGrid'
mesh.ScriptRequestInformation = ''
mesh.PythonPath = ''
mesh.Script = '''
import numpy as np
coor = np.array([
[ 0.0 , 0.0 ],
[ 1.0 , 0.0 ],
[ 2.0 , 0.0 ],
[ 0.0 , 1.0 ],
[ 1.0 , 1.0 ],
[ 2.0 , 1.0 ],
])
conn = np.array([
[ 0 , 1 , 4 , 3 ],
[ 1 , 2 , 5 , 4 ],
])
celldata = [
1.0 ,
2.0
]
normals = np.array([
[ -1.0 , -1.0 ],
[ 0.0 , -1.0 ],
[ +1.0 , -1.0 ],
[ -1.0 , +1.0 ],
[ 0.0 , +1.0 ],
[ +1.0 , +1.0 ],
])
normals /= np.tile(np.sqrt(np.sum(normals**2.,axis=1)).reshape(-1,1),(1,2))
import vtk
grid = self.GetOutput()
points = vtk.vtkPoints()
for i,(x,y) in enumerate(coor):
points.InsertNextPoint(x,y,0.0)
grid.SetPoints(points)
grid.Allocate()
for el in conn:
cell = vtk.vtkQuad()
for i,ver in enumerate(el):
cell.GetPointIds().InsertId(i,ver)
grid.InsertNextCell(cell.GetCellType(),cell.GetPointIds())
data = vtk.vtkDoubleArray()
data.SetName("Example data")
for i in celldata:
data.InsertNextValue(i)
grid.GetCellData().AddArray(data)
data = vtk.vtkDoubleArray()
data.SetNumberOfComponents(3)
data.SetName("Normals")
for i in normals:
data.InsertNextTuple([i[0],i[1],0.0])
grid.GetPointData().AddArray(data)
'''
# show data from mesh
Mesh = Show(mesh)
Render()

Related

How do I use the non-convenience SCNGeometrySource initializer correctly?

being new to Swift and SceneKit (not new to programming) and trying to get my first triangle displayed (in preparation of rendering a more complex procedurally generated geometry) I just stumbled over the following issue:
When I initialise the SCNGeometrySource as shown in the code below the triangle is in the wrong position. When using the shorter, commented out version to create vertices it works as expected. My understanding from the documentation and a couple of examples I found online was that these two should be equivalent.
Which embarrassing detail am I missing? Thanks!
assert( MemoryLayout< SCNVector3 >.size == 24 )
assert( MemoryLayout< SCNVector3 >.stride == 24 )
let varray = [ SCNVector3( -3.0, -3.0, 0.0 ),
SCNVector3( 3.0, 3.0, 0.0 ),
SCNVector3( -3.0, 3.0, 0.0 ) ]
let vdata = Data( bytes: varray, count: varray.count * 24 )
print( "size: \(vdata.count), data: \(hexDump( vdata ) )" ) // size: 72, data: 00000000000008c000000000000008c0000000000000000000000000000008400000000000000840000000000000000000000000000008c000000000000008400000000000000000
let vertices = SCNGeometrySource(data: vdata,
semantic: .vertex,
vectorCount: varray.count,
usesFloatComponents: true,
componentsPerVector: 3,
bytesPerComponent: 8,
dataOffset: 0,
dataStride: 24 )
// let vertices = SCNGeometrySource(vertices: varray) // This works, the line above not
Edit: It works correctly when the input data consists of (vectors of) 32-bit floats (and all the parameters are adjusted accordingly).

interpolate animation value in flutter

If I want to convert a range from 0.0 to 1.0, into a range from 20.0 to 80.0, I can use lerpDouble(20.0, 80.0, animationValue).
How do I convert a range from 0.8 to 1.0, into a range from 0.0 to 1.0?
After some digging I found a simple solution to the problem:
final value = Interval(0.8, 1.0).transform(animationValue);
Dear you can use this plugging to animation in a flutter Here Link
You can use this:
(lerpDouble(0.8, 1.0, animationValue) * 1.0/0.2) - 4.0
When:
animationValue=0.8 => (0.8*5)-4.0 = 0.0
animationValue=1.0 => (1.0*5)-4.0 = 1.0
This is to do the other way round:
0.8 + 0.2 * lerpDouble(0.0, 1.0, animationValue)
When:
animationValue=0.0 => 0.8+(0.2*0) = 0.8
animationValue=1.0 => 0.8+(0.2*1) = 1.0

Complex Number from square root of negative array [duplicate]

This question already has answers here:
How can I take the square root of -1 using python?
(6 answers)
Closed 5 years ago.
I am a new convert from Matlab to python and I am struggling with the generation of a complex array.
In matlab I have the following code:
xyAxis = linspace(-127,127,255);
[x,y] = meshgrid(xyAxis, xyAxis);
fz = -(x.^2 + y.^2);
ifz = sqrt(fz);
Which I am trying to replicate in python 3:
import numpy as np
xyAxis = np.intp( np.linspace(-127, 127, 255) )
x, y = np.meshgrid(xyAxis,xyAxis)
fz = - (x**2 + y**2)
ifz = np.sqrt(fz)
However, I get the following error:
RuntimeWarning: invalid value encountered in sqrt
I have done a bit of googling and I am not sure how to mimic the behavior of matlab in this case? Any suggestions?
One way is casting fz to complex dtype:
ifz = np.sqrt(fz.astype(np.complex))
Another way apart from what #PaulPanzer suggested -
import numpy as np
xyAxis = np.intp( np.linspace(-127, 127, 255) )
x, y = np.meshgrid(xyAxis,xyAxis)
fz = - (x**2 + y**2)
from numpy.lib.scimath import sqrt as csqrt
ifz = csqrt(fz)
print ifz
This comes straight from the numpy docs
Output
[[ 0.+179.60512242j 0.+178.89941308j 0.+178.19652073j ...,
0.+178.19652073j 0.+178.89941308j 0.+179.60512242j]
[ 0.+178.89941308j 0.+178.19090886j 0.+177.48521065j ...,
0.+177.48521065j 0.+178.19090886j 0.+178.89941308j]
[ 0.+178.19652073j 0.+177.48521065j 0.+176.7766953j ...,
0.+176.7766953j 0.+177.48521065j 0.+178.19652073j]
...,
[ 0.+178.19652073j 0.+177.48521065j 0.+176.7766953j ...,
0.+176.7766953j 0.+177.48521065j 0.+178.19652073j]
[ 0.+178.89941308j 0.+178.19090886j 0.+177.48521065j ...,
0.+177.48521065j 0.+178.19090886j 0.+178.89941308j]
[ 0.+179.60512242j 0.+178.89941308j 0.+178.19652073j ...,
0.+178.19652073j 0.+178.89941308j 0.+179.60512242j]]

differentiating and taking limit of complex vector valued function with maple

I would like to do the following with maple
let
omega := z -> 2*<Re(z), Im(z), 1>/(1+abs(z)^2):
and
phi := -> z (l*z+a)/(1-l*conjugate(a)*z):
where a is complex and l is real.
I consider Omega=omega( phi(z) ) and i would like to evaluate diff(Omega,x) diff(Omega,y) but also compute some limit like
> expr := omega(phi(1/e));
> Omega := simplify(map(limit, expr, e = 0));
> expr2 := (omega(phi(1/(e^2*conjugate(z))))-Omega)/e^2;
> H := limit(expr2, e = 0);
Unfortunately i have tried every thing (Vector Calculus , Complex...) and i have always a probleme either because i work with vector or because the variable is complex.
Does someone have any idea of the good way to code such a problem?
Thx
[edited:] I cannot tell whaht your original definition of operator phi is, as there is a syntax error in the original post (invalid syntax). In particular I cannot tell whether you meant,
phi := z -> (l*z+a)/(1-l*conjugate(a)*z):
or,
phi := z -> z (l*z+a)/(1-l*conjugate(a)*z):
I used the former below. The results will depend upon the choice, of course.
In a previous question by you an answer involved using evalc, under which all unknowns would be treated as real.
But now you seem to have a mix, where l is to be taken as real while a may be complex.
As shown in your earlier question another approach is to use assumptions on the unknowns, which in this case can give finer control over your mix.
Note that a will get treated as being possibly complex, by default. So we can use an assumption on just l.
restart:
omega := z -> 2*<Re(z), Im(z), 1>/(1+abs(z)^2):
phi := z -> (l*z+a)/(1-l*conjugate(a)*z):
expr := omega(phi(1/e)):
map(limit,expr,e=0) assuming l::real:
map(simplify,%);
[ 2 Re(a) ]
[- ----------]
[ 2 ]
[ | a | + 1]
[ ]
[ 2 Im(a) ]
[- ----------]
[ 2 ]
[ | a | + 1]
[ ]
[ 2 ]
[ 2 | a | ]
[ ---------- ]
[ 2 ]
[ | a | + 1 ]
Here is another way to get a result. We could let a=S+T*I and use evalc to handle altogether the assumptions that S and T (and l) are purely real.
map(limit,subs(a=S+T*I,expr),e=0) assuming l::real:
simplify(map(evalc,%));
[ 2 S ]
[- -----------]
[ 2 2 ]
[ S + T + 1]
[ ]
[ 2 T ]
[- -----------]
[ 2 2 ]
[ S + T + 1]
[ ]
[ 2 2 ]
[ 2 (S + T ) ]
[ ----------- ]
[ 2 2 ]
[ S + T + 1 ]

Get closest data using centerSphere - MongoDB

I'm trying to get the closest data from the following data
> db.points.insert({name:"Skoda" , pos : { lon : 30, lat : 30 } })
> db.points.insert({name:"Honda" , pos : { lon : -10, lat : -20 } })
> db.points.insert({name:"Skode" , pos : { lon : 10, lat : -20 } })
> db.points.insert({name:"Skoda" , pos : { lon : 60, lat : -10 } })
> db.points.insert({name:"Honda" , pos : { lon : 410, lat : 20 } })
> db.points.ensureIndex({ loc : "2d" })
then I tried
> db.points.find({"loc" : {"$within" : {"$centerSphere" : [[0 , 0],5]}}})
this time I got different error
error: {
"$err" : "Spherical MaxDistance > PI. Are you sure you are using radians?",
"code" : 13461
then I tried
> db.points.find({"loc" : {"$within" : {"$centerSphere" : [[10 , 10],2]}}})
error: {
"$err" : "Spherical distance would require wrapping, which isn't implemented yet",
"code" : 13462
How to get this done ? I just want to get the closest data based on the given radious from GEO point.
Thanks
A few things to note. Firstly, you are storing your coordinates in a field called "pos" but you are doing a query (and have created an index) on a field called "loc."
The $centerSphere takes a set of coordinates and a value that is in radians. So $centerSphere: [[10, 10], 2] searches for items around [10, 10] in a circle that is 2 * (earth's radius) = 12,756 km. The $centerSphere operator is not designed to search for documents in this large of an area (and wrapping around the poles of the Earth is tricky). Try using a smaller value, such as .005.
Finally, it is probably a better idea to store coordinates as elements in an array since some drivers may not preserve the order of fields in a document (and swapping latitude and longitude results in drastically different geo locations).
Hope this helps:
The radius of the Earth is approximately 3963.192 miles or 6378.137 kilometers.
For 1 mile:
db.places.find( { loc: { $centerSphere: [ [ -74, 40.74 ] ,
1 / 3963.192 ] } } )