Re: interpolation taking too long in an adaptive meshing effort (remeshing)

2018-08-17 Thread Daniel Wheeler
On Sun, Aug 12, 2018 at 2:52 PM Drew Davidson  wrote:
>
> I am posting because before I tried this, I was unable to find on this 
> mailing list a clear statement/warning that such an interpolation can run 
> slow (eat up computer time).  Is there a faster way to interpolate?

The call method to cell variable takes a nearestCellIDs argument which
can greatly speed up the interpolation if you're interpolating between
the same meshes multiple times. For adaptive meshing the meshes need
to retain knowledge of the adaptation to make the interpolation faster
otherwise a brute force technique is required. Scipy or Sklearn
probably has better algorithms for finding nearest neighbors so that
could be used to find the nearest neighbors instead of FiPy's method.
To use the nearestCellIDs argument, try this,

import fipy

m0 = fipy.Grid2D(nx=10, ny=10)
m1 = fipy.Grid2D(nx=20, ny=10)

v0 = fipy.CellVariable(mesh=m0, value= m0.x * m0.y)
v1 = fipy.CellVariable(mesh=m1)

# very slow and can be replaced
ids = m0._getNearestCellID(m1.cellCenters)

# should be fast at nearest neighbors are already calculated
v1[:] = v0(points=m1.cellCenters, order=1, nearestCellIDs=ids)

print v1

You could replace "_getNearestCellID" sklearn's,
http://scikit-learn.org/stable/modules/neighbors.html, without doing
anything to fipy. "ids" is just a numpy array.

> Google seemed to lead me to various interpolation tools, but I do not see a 
> way to take the data outside of FiPy to do the interpolation and then bring 
> the data back into FiPy.  I looked at the FiPy CellVariable source code but 
> it is beyond me to mess around with it.  It looked like it might run fast 
> since it seems to operate on only the nearest neighbors, but maybe it is 
> slowed down by having to do one cell at a time?

To get data out of fipy, you don't need to do much more than,

import pandas
pandas.DataFrame(dict(x=m1.x, y=m1.y, v=v1)).to_csv('test.csv')

for example. This will write the position and values of a cell
variable to a CSV file.

-- 
Daniel Wheeler

___
fipy mailing list
fipy@nist.gov
http://www.ctcms.nist.gov/fipy
  [ NIST internal ONLY: https://email.nist.gov/mailman/listinfo/fipy ]


interpolation taking too long in an adaptive meshing effort (remeshing)

2018-08-12 Thread Drew Davidson
Hello,


Short story:

I am posting because before I tried this, I was unable to find on this
mailing list a clear statement/warning that such an interpolation can run
slow (eat up computer time).  Is there a faster way to interpolate?


Long story:

I thought of an adaptive meshing scheme for examples.phase.anisotropy after
reading:


Ferreira, Alexandre Furtado, Leonardo de Olivé Ferreira, and Abner da Costa
Assis. “Numerical Simulation of the Solidification of Pure Melt by a
Phase-Field Model Using an Adaptive Computation Domain.” *Journal of the
Brazilian Society of Mechanical Sciences and Engineering* 33, no. 2 (June
2011): 125–30. https://doi.org/10.1590/S1678-5878201100022.


Their adaptive meshing scheme is quite simple and the paper is short. They
do not interpolate, because they are writing their own code from the ground
up. But in FiPy, it seems necessary to interpolate a solution from an old
mesh to a new mesh after remeshing.


I wrote a script and it runs for tiny problems, but seems useless because
the interpolation in remeshing takes way too long (which defeats the
purpose of ~Ferreira et al 2011 adaptive meshing), with interpolation time
exploding as mesh cell count increases. I didn’t record the cell count
exactly, but let’s say at ~5E5 cells, after 3.5 hours in a single
interpolation, I gave up and hit ctrl-c. 2500 cells took less than 2
seconds, 2E4 cells 100 seconds, 6E4 cells 900 seconds. For 5E5 cells, my 4
core 2013 desktop PC shows multiple cores in use, but most cores at 50%
load; using about 1/3 of the 8 GB RAM.


Interpolation means transferring a FiPy solution from a first unstructured
mesh of triangles to a second unstructured mesh of triangles (2D):


newPhase=CellVariable(mesh=newMesh,
value=oldPhase(newMesh.cellCenters,order=1), hasOld=True)


new_dT=CellVariable(mesh=newMesh,
value=old_dT(newMesh.cellCenters,order=1), hasOld=True)


Mesh 2 always has more cells than mesh 1.  Eventually mesh 2 is the mesh in
examples.phase.anisotropy.


Does anybody know a way to speed this interpolation up, within FiPy? I
don’t believe I can use Grid2D meshes because my final domain of interest
is not rectangular (examples.phase.anisotropy is just a beginner problem to
play with). The meshes are unstructured sets of triangular cells and are
created using gmsh via pygmsh/meshio. Mesh density varies with x,y
(nonuniform meshes).


Google seemed to lead me to various interpolation tools, but I do not see a
way to take the data outside of FiPy to do the interpolation and then bring
the data back into FiPy.  I looked at the FiPy CellVariable source code but
it is beyond me to mess around with it.  It looked like it might run fast
since it seems to operate on only the nearest neighbors, but maybe it is
slowed down by having to do one cell at a time?


Thanks
___
fipy mailing list
fipy@nist.gov
http://www.ctcms.nist.gov/fipy
  [ NIST internal ONLY: https://email.nist.gov/mailman/listinfo/fipy ]