How to compute the volume of an unstructured mesh using TVTK and Python? - volume

I'm trying to calculate the volume of an unstructured grid using mayavi and tvtk. My idea was to tetrahedronalize the Point cloud by means of the Delaunay3d-Filter. Then I Need to somehow extract the tetrahedra from this dataset while ignoring other cell-types such as lines and triangles.
But how can i accomplish this? My Python code so far Looks as follows:
import numpy as np
from mayavi import mlab
x, y, z = np.random.random((3, 100))
data = x**2 + y**2 + z**2
src = mlab.pipeline.scalar_scatter(x, y, z, data)
field = mlab.pipeline.delaunay3d(src)
Can i use the field-object to retrieve the polyhedras Vertices?
Thanks in advance.
frank.

Is this the best way to go about it? scipy.spatial has delaunay functionality as well. Having recently worked with both myself, I would note that scipy is a much lighter dependency, easier to use, and better documented. Note that either method will work on the convex hull of the pointcloud, which may not be what you want. the scipy version also easily allows you to compute the boundary primitives as well, amongst other things, which may be useful for further processing.

Related

How to use an external code component within the optimization framework

Hi I am trying to use the paraboloid external code component to get the same results as in the paraboloid optimization problem (openmdao v 2.2.0).
So in my mind the independent variables x,y should be updated and thus changing the input file of the external component to minimize the output f.
Not that I got this working but I basically add the external component's output to be the objective and the independent variables to be the design variables etc (see code below).
But more importantly I have a problem to conceptually understand how the optimizer would know the derivatives in such external codes.
I tried 'COBYLA' thinking that could be a way to go gradient-free approach but there seems to be a bug in the iprint statement, since I can not run the example paraboloid optimization either.
I think I have a similar problem with surrogates. For example I use Metamodelunstructured component to find my surrogate which performs well if I ask for a known value. But I do not see how to couple this component's output to be the objective of the optimizer. I think I am doing the right thing by giving the model objective. but not sure...
The answer might be that I am completely off from the optimization logic if so please refer me to the related papers for the algorithms behind.
Thanks in advance
from openmdao.api import Problem, Group, IndepVarComp
from openmdao.api import ScipyOptimizeDriver
from openmdao.components.tests.test_external_code import ParaboloidExternalCode
top = Problem()
top.model = model = Group()
# create and connect inputs
model.add_subsystem('p1', IndepVarComp('x', 3.0))
model.add_subsystem('p2', IndepVarComp('y', -4.0))
model.add_subsystem('p', ParaboloidExternalCode())
model.connect('p1.x', 'p.x')
model.connect('p2.y', 'p.y')
top.driver = ScipyOptimizeDriver()
top.driver.options['optimizer'] = 'SLSQP'
top.model.add_design_var('p1.x', lower=-50, upper=50)
top.model.add_design_var('p2.y', lower=-50, upper=50)
top.model.add_objective('p.f_xy')
top.driver.options['tol'] = 1e-9
top.driver.options['disp'] = True
top.setup()
top.run_driver()
# minimum value
# location of the minimum
print(top['p1.x'])
print(top['p2.y'])
So, I think the main thing you are asking is how to provide derivatives for external codes. I think there are really two options for this.
Finite difference across the external component.
The test example doesn't show how to do this, which is unfortunate, but you do this the same way that you would declare derivatives fd for a pure python component, namely by adding this line to the external component's setup method:
self.declare_partials(of='*', wrt='*', method='fd')
Provide another external method to calculate the derivatives, and wrap it in the "compute_partials" method.
We do this with CFD codes that provide an adjoint solution. You could possibly also use automatic differentiation on the external source code to produce a callable function in this way. However, I think method 1 is what you are asking for here.

Finetune a Torch model

I have loaded a model in Torch and I would like to fine-tune it. For now I'd like to retrain the last 2 layers of the network (though in the future I may want to add layers). How can I do this? I have been looking for tutorials, but I haven't found what I am looking for. Any tips?
I don't know if I understood what you are asking for. If you want to leave the net as it was except for the 2 layers you want to train (or fine-tune) you have to stop the backpropagation on the ones you don't want to train, like this:
for i=1, x do
c = model:get(i)
c.updateGradInput = function(self, inp, out) end
c.accGradParameters = function(self,inp, out) end
end
Now only the layers outside of this loop will upgrade their parameters. If you want to add new layers just call model:insert(module, position), you can have a look here Torch containers
If that was not what you were looking for, please elaborate more on the question.

inner product - homomorphic encryption library

I want to do a very simple thing:
Given two vectors, I want to encrypt them and do some calculation, then decrypt the result and get the inner product between both vectors.
Can you recommend me some library that can do this thing? Any other material will help me as well.
I found HELIB, but I still dont know if that is the best that I can do. Would you recommend this library? Do you know better ones for my purpose? I want to do it as fast as possible for the biggest vector dimension possible.
I have only basic knowledge in crypto, so I would like use that as black box as much as possible without putting to much effort in the mathematics behind it.
Thanks for any help!
You can do exactly that with the homomorphic encryption library Pyfhel in Python3. Just install it using pip install Pyfhel and create a simple demo:
from Pyfhel import Pyfhel, PyCtxt
he = Pyfhel() # Object in charge of all homomorphic operations
he.contextGen(10000) # Choose the maximum size of your values
he.keyGen(10000) # Generate your private/public key pair
# Now to the encryption/operation/decryption
v1 = [5.34, 3.44, -2.14, 9.13]
v2 = [1, 2.5, -2.1, 3]
p1 = [he.encrypt(val) for val in v1]
p2 = [he.encrypt(val) for val in v2]
pMul = [a*b for a,b in zip(p1, p2)]
pScProd = [pMul[0]+p for p in pMul[1:]
round(he.decrypt(pScProd), 3)
#> 45.824

Export R object for 3D printing

If I have a data set in R, what would be a good way to export it so I could get it to a service like Shapeways for 3D printing?
I don't have any "real" CAD software, but I've used Google Sketchup before.
In my case the object can be described by two surface plots, something like this:
x <- y <- seq(0,1,by=0.01)
persp(x, y, outer(x, y, function(x,y) (x+y)^2))
persp(x, y, outer(x, y, function(x,y) rep(0,length(x))), zlim=c(-1,1))
...which I would like to appear together as one object to be printed. Any ideas?
Shapeways says it can take output from MeshLab: http://sourceforge.net/projects/meshlab/files/meshlab
MeshLab, an open-source, free-as-in-beer project, is able to import this file using its .asc format option:
dat <- data.frame(x=x, # will be recycled 101 times
y=rep(y, each=101),
z=as.vector(outer(x, y, function(x,y) (x+y)^2)))
write.table(dat, file="out.asc", row.names=FALSE, col.names=FALSE)
I probably should have done an sos-search;
library(sos)
findFn("3d printing")
.... did bring up the r2stl package whose sole function has the same name. It also found other convex hull functions that might be useful to others that want to build other 3D shapes from data.
DWin has already made one suggestion for the mesh. If you need to export the resulting object from Meshlab and manipulate it in an extraordinarily intuitive 3D application that doesn't cost the earth then you should try MoI 3D.
I mention this because MoI has a very competent mesh engine and many of MoI's users seem to be involved in 3D printing (see for example this thread).
The developer Michael Gibson often responds to forum questions in, literally, minutes and other users in the forum are very supportive. There is a full 30-day trial version that allows you to experiment at no cost. MoI can also be scripted using JavaScript.
By its nature 3D printing is irrevocably real so it pays to be sure before you commit!

numerical differentiation with Scipy

I was trying to learn Scipy, using it for mixed integrations and differentiations, but at the very initial step I encountered the following problems.
For numerical differentiation, it seems that the only Scipy function that works for callable functions is scipy.derivative() if I'm right!? However, I couldn't work with it:
1st) when I am not going to specify the point at which the differentiation is to be taken, e.g. when the differentiation is under an integral so that it is the integral that should assign the numerical values to its integrand's variable, not me. As a simple example I tried this code in Sage's notebook:
import scipy as sp
from scipy import integrate, derivative
var('y')
f=lambda x: 10^10*sin(x)
g=lambda x,y: f(x+y^2)
I=integrate.quad( sp.derivative(f(y),y, dx=0.00001, n=1, order=7) , 0, pi)[0]; show(I)
show( integral(diff(f(y),y),y,0,1).n() )
also it gives the warning that "Warning: The occurrence of roundoff error is detected, which prevents the requested tolerance from being achieved. The error may be underestimated." and I don't know what does this warning stand for as it persists even with increasing "dx" and decreasing the "order".
2nd) when I want to find the derivative of a multivariable function like g(x,y) in the above example and something like sp.derivative(g(x,y),(x,0.5), dx=0.01, n=1, order=3) gives error, as is easily expected.
Looking forward to hearing from you about how to resolve the above cited problems with numerical differentiation.
Best Regards
There are some strange problems with your code that suggest you need to brush up on some python! I don't know how you even made these definitions in python since they are not legal syntax.
First, I think you are using an older version of scipy. In recent versions (at least from 0.12+) you need from scipy.misc import derivative. derivative is not in the scipy global namespace.
Second, var is not defined, although it is not necessary anyway (I think you meant to import sympy first and use sympy.var('y')). sin has also not been imported from math (or numpy, if you prefer). show is not a valid function in sympy or scipy.
^ is not the power operator in python. You meant **
You seem to be mixing up the idea of symbolic and numeric calculus operations here. scipy won't numerically differentiate an expression involving a symbolic object -- the second argument to derivative is supposed to be the point at which you wish to take the derivative (i.e. a number). As you say you are trying to do numeric differentiation, I'll resolve the issue for that purpose.
from scipy import integrate
from scipy.misc import derivative
from math import *
f = lambda x: 10**10*sin(x)
df = lambda x: derivative(f, x, dx=0.00001, n=1, order=7)
I = integrate.quad( df, 0, pi)[0]
Now, this last expression generates the warning you mentioned, and the value returned is not very close to zero at -0.0731642869874073 in absolute terms, although that's not bad relative to the scale of f. You have to appreciate the issues of roundoff error in finite differencing. Your function f varies on your interval between 0 and 10^10! It probably seems paradoxical, but making the dx value for differentiation too small can actually magnify roundoff error and cause numerical instability. See the second graph here ("Example showing the difficulty of choosing h due to both rounding error and formula error") for an explanation: http://en.wikipedia.org/wiki/Numerical_differentiation
In fact, in this case, you need to increase it, say to 0.001: df = lambda x: derivative(f, x, dx=0.001, n=1, order=7)
Then, you can integrate safely, with no terrible roundoff.
I=integrate.quad( df, 0, pi)[0]
I don't recommend throwing away the second return value from quad. It's an important verification of what happened, as it is "an estimate of the absolute error in the result". In this case, I == 0.0012846582250212652 and the abs error is ~ 0.00022, which is not bad (the interval that implies still does not include zero). Maybe some more fiddling with the dx and absolute tolerances for quad will get you an even better solution, but hopefully you get the idea.
For your second problem, you simply need to create a proper scalar function (call it gx) that represents g(x,y) along y=0.5 (this is called Currying in computer science).
g = lambda x, y: f(x+y**2)
gx = lambda x: g(x, 0.5)
derivative(gx, 0.2, dx=0.01, n=1, order=3)
gives you a value of the derivative at x=0.2. Naturally, the value is huge given the scale of f. You can integrate using quad like I showed you above.
If you want to be able to differentiate g itself, you need a different numerical differentiation functio. I don't think scipy or numpy support this, although you could hack together a central difference calculation by making a 2D fine mesh (size dx) and using numpy.gradient. There are probably other library solutions that I'm not aware of, but I know my PyDSTool software contains a function diff that will do that (if you rewrite g to take one array argument instead). It uses Ridder's method and is inspired from the Numerical Recipes pseudocode.

Resources