This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to scale a mesh

+1 vote

Hi,

How do I scale a mesh to a different size, i.e., scale it uniformly by a certain factor (in C++)?

asked Aug 25, 2013 by Theodore FEniCS Novice (780 points)
edited Aug 25, 2013 by Theodore

1 Answer

+5 votes
 
Best answer

Hi,

To scale the whole mesh:

mesh = UnitSquareMesh(2, 2)
x = mesh.coordinates()
scaling_factor = 2.
x[:, :] *= scaling_factor

To scale in just one single direction:

x[:, 1] *= scaling_factor

Finally a cached search tree needs to be explicitly updated after moving/deforming a mesh and before evaluating any function (even by assembler) without cell argument.

mesh.intersection_operator().clear() # up to version 1.2.0
mesh.bounding_box_tree().build(mesh) # development version
answered Aug 25, 2013 by mikael-mortensen FEniCS Expert (29,340 points)
selected Aug 25, 2013 by Theodore

Comment about search tree added to the answer.

Thanks mikael-mortensen and Jan but am I right in thinking that

x = mesh.coordinates()
x[:, :] *= scaling_factor

doesn't scale the mesh 'in place'. Because doing,

Some::FunctionSpace V(mesh) 

still uses the 'unscaled' mesh !?!

Also, could I have solutions in C++ (thanks).

Each object dependent on mesh uses a pointer to the original mesh object so everything's gets updated correctly. Functions gets naturally convected by mesh movement as DOFs are moved. Search tree is (probably) only exception which needs getting updated explicitly.

C++ (not tested)

std::vector<double>& x = mesh.coordinates();
for (std::size_t i=0; i<x.size(); ++i)    
  x[i] *= scaling_factor;
mesh.intersection_operator().clear();  // <= 1.2.0
mesh.bounding_box_tree()->build(mesh); // > 1.2.0

Thanks Jan.

So, would this be right in C++:

std::vector<double> x = mesh.coordinates()
for ( auto i = x.begin(); i != x.end(); ++i ) {
        (*i) *= scaling_factor; 
}

Would this update the mesh correctly?

Thanks, replied to your previous comment before I saw this one.

Note this is also true when using refine/adapt on a mesh. It appears this wasn't the case in older versions (~1.0), but after updating to 1.2 I would get a hard error without updating the bounding box.

...