How to get the vertex corodinates of all DG0 cells in a mesh

Asked by Jason

Hi,

Is this the correct way to get the coordinates of the vertices of all DG0 cells that make up a mesh? Am I also correct in assuming for DG0 cells, there's just a single vertex in the center?

for (CellIterator cell(mesh); !cell.end(); ++cell) {
       xy = new Array<double>(dim, geometry.x(cell->index()));

       std::cout << (*xy)[0] << "\t" << (*xy)[1] << "\t" << (*xy)[2] << std::endl;

       Point point( (*xy)[0], (*xy)[1], (*xy)[2] );
       vec.push_back( point );
}

This prints out some normal values and aslo some very small ones leading me to think the above is not correct.

Thanks,

Jason

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Johan Hake
Solved:
Last query:
Last reply:
Revision history for this message
Johan Hake (johan-hake) said :
#1

Please provide a complete minimal code example.

Also there is a method in the DofMap object for exactly this purpose.

   DofMap::tabulate_coordinates;

The DofMap resides within a FunctionSpace.

Johan

On 05/21/2012 10:05 PM, Jason wrote:
> New question #198006 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/198006
>
> Hi,
>
> Is this the correct way to get the coordinates of the vertices of all DG0 cells that make up a mesh? Am I also correct in assuming for DG0 cells, there's just a single vertex in the center?
>
> for (CellIterator cell(mesh); !cell.end(); ++cell) {
> xy = new Array<double>(dim, geometry.x(cell->index()));
>
>
> std::cout<< (*xy)[0]<< "\t"<< (*xy)[1]<< "\t"<< (*xy)[2]<< std::endl;
>
> Point point( (*xy)[0], (*xy)[1], (*xy)[2] );
> vec.push_back( point );
> }
>
> This prints out some normal values and aslo some very small ones leading me to think the above is not correct.
>
> Thanks,
>
> Jason
>

Revision history for this message
Jason (jason-langsley) said :
#2

Hi Johan,

Here's a minimal code examle, I'm not sure it's correct:

#include <dolfin.h>

using namespace dolfin;

int main()
{
 int dim = 3;

 // Create mesh
 Box mesh(0.0, 0.0, 0.0, 100.0, 100.0, 30.0, 100, 10, 30 );

 MeshGeometry& geometry = mesh.geometry();

 Array<double> *xy;
 int i = 0, c = 0;
 for ( CellIterator cell(mesh); !cell.end(); ++cell ) {
  //Iterate through all vertices in cell
  xy = new Array<double>(dim, geometry.x(cell->index()));

  if (i%1000 == 0) {
   std::cout << c << "\t" << (*xy)[0] << "\t" << (*xy)[1] << "\t" << (*xy)[2] << "\t" << std::endl;
   ++c;
  }

  ++i;
 }
}

Am I correct in assuming for DG0 cells, there's just a single vertex in the center?

Jason

Revision history for this message
Best Johan Hake (johan-hake) said :
#3

On 05/22/2012 01:00 AM, Jason wrote:
> Question #198006 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/198006
>
> Status: Answered => Open
>
> Jason is still having a problem:
> Hi Johan,
>
> Here's a minimal code examle, I'm not sure it's correct:
>
>
> #include<dolfin.h>
>
> using namespace dolfin;
>
> int main()
> {
> int dim = 3;
>
> // Create mesh
> Box mesh(0.0, 0.0, 0.0, 100.0, 100.0, 30.0, 100, 10, 30 );
>
> MeshGeometry& geometry = mesh.geometry();
>
> Array<double> *xy;
> int i = 0, c = 0;
> for ( CellIterator cell(mesh); !cell.end(); ++cell ) {
> //Iterate through all vertices in cell
> xy = new Array<double>(dim, geometry.x(cell->index()));
>
> if (i%1000 == 0) {
> std::cout<< c<< "\t"<< (*xy)[0]<< "\t"<< (*xy)[1]<< "\t"<< (*xy)[2]<< "\t"<< std::endl;
> ++c;
> }
>
> ++i;
> }
> }

Here you are mixing cells and vertices. geometry.x gives you a view into
the coordinate of the vertices. You are accessing these with the cell
index. There are way more cells than vertices so no surprise you get
funny results.

You are also calling new for each iteration which introduces memory leaks.

> Am I correct in assuming for DG0 cells, there's just a single vertex in the center?

Yes, so basically you can:

   Point p = cell.midpoint();

to get the coordinate.

But for a more general solution, I suggest you to look into
DofMap::tabulate_coordinates;

Johan

> Jason
>

Revision history for this message
Jason (jason-langsley) said :
#4

Thanks Johan Hake, that solved my question.