# Build mesh from a vector of nodes

**URL:** https://community.freefem.org/t/build-mesh-from-a-vector-of-nodes/1479
**Category:** General Discussion
**Created:** [February 7, 2022, 10:24pm UTC](https://community.freefem.org/t/build-mesh-from-a-vector-of-nodes/1479 "2022-02-07T22:24:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![larismartins](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/larismartins/32/2712_2.png) [@larismartins](https://community.freefem.org/u/larismartins)
#### Post date: [February 7, 2022, 10:24pm UTC](https://community.freefem.org/t/build-mesh-from-a-vector-of-nodes/1479/1 "2022-02-07T22:24:43Z")

</div>

I’m trying to build a mesh using a vector of nodes

```auto
real[int,int] nodes = [[0,0],
					   [0.5,0],
				       [1,0],
				       [0,0.5],
				       [0.5,0.5],
				       [1,0.5],
				       [0,1],
				       [0.5,1],
				       [1,1]];

border bdry(t=0,nodes.n-1){ P.x=nodes(0,t); P.y=nodes(1,t); label=1; };
plot(bdry(nodes.n-1),dim=2,wait=1,cmm="boundary");
mesh Th = buildmesh(bdry(nodes.n-1),fixeborder=true); 
plot(Th,wait=1,cmm="mesh");

```

But I get the following error:

```auto
Out of bound 0 <=0 < 9 2 < 2 array type = P3KNMIdE
  current line = 11

```

What am I doing wrong?

---

<div class="post-metadata">

### Author: ![fivanci](https://avatars.discourse-cdn.com/v4/letter/f/e95f7d/32.png) [@fivanci](https://community.freefem.org/u/fivanci)
#### Post date: [February 8, 2022, 2:06am UTC](https://community.freefem.org/t/build-mesh-from-a-vector-of-nodes/1479/2 "2022-02-08T02:06:16Z")

</div>

The way you set up your matrix of nodes, it is \in\mathbb{R}^{9\times2}. You are treating it somehow mixed, like sometimes as \in\mathbb{R}^{9\times2} an sometimes as \in\mathbb{R}^{2\times 9}. Check the documentation [Types](https://doc.freefem.org/references/types.html#matrix-size) for `matrix size`.

```auto
real[int,int] nodes = [[0,0],[0.5,0],[1,0],[0,0.5],[0.5,0.5],[1,0.5],[0,1],
                      [0.5,1],[1,1]];

// cout << nodes << endl;
cout << "nrows = " << nodes.n << ", ncols" << nodes.m << endl;

border bdry(t=0,nodes.n-1){ P.x=nodes(t,0); P.y=nodes(t,1); label=1; };
plot(bdry(nodes.n-1),dim=2,wait=1,cmm="boundary");

```

Also, your boundary is not a closed loop, so it won’t mesh of course.

Check also post [Building a border from an array of vertices - #2 by fivanci](https://community.freefem.org/t/building-a-border-from-an-array-of-vertices/1253/2), although, I see now there is _lapsus calami_ in there as well. It should be, in declaration,  
`real[int,int] vtxCoords(2,nn);` and later in code `vtxCoords.n-1`\mapsto`vtxCoords.m-1`.

if

---

<div class="post-metadata">

### Author: ![larismartins](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/larismartins/32/2712_2.png) [@larismartins](https://community.freefem.org/u/larismartins)
#### Post date: [February 8, 2022, 2:15pm UTC](https://community.freefem.org/t/build-mesh-from-a-vector-of-nodes/1479/3 "2022-02-08T14:15:39Z")

</div>

Do you know a way to build a mesh using connectivity matrix? Something like:

```auto
matrix elems = [[1,2,5,4],
				[2,3,6,5],
				[4,5,8,7],
				[5,6,9,8]];

matrix edges = [[1,2],
				[2,3],
				[1,4],
				[2,5],
				[3,6],
				[4,5],
				[5,6],
				[4,7],
				[5,8],
				[6,9],
				[7,8],
				[8,9]];

```

I’m trying to get control over all edges, boundary and internal. Also, I wouldlike to label each edge

---

<div class="post-metadata">

### Author: ![fivanci](https://avatars.discourse-cdn.com/v4/letter/f/e95f7d/32.png) [@fivanci](https://community.freefem.org/u/fivanci)
#### Post date: [February 9, 2022, 1:51am UTC](https://community.freefem.org/t/build-mesh-from-a-vector-of-nodes/1479/4 "2022-02-09T01:51:48Z")

</div>

> [@larismartins](#):
>
> I’m trying to get control over all edges, boundary and internal. Also, I wouldlike to label each edge

When you say label, you mean label each edge in the triangle ( local [1,2,3] \mapsto global [i\_1,i\_2,i\_3] ) ? I.e., by

> [@larismartins](#):
>
> control over all edges

you mean (global) label each edge in the mesh by hand ?

1. In that case, **why use automatic mesh generator**? Seems to me it is easier to just write the mesh file directly – I am assuming you are checking some technical things and need only relatively small mesh (with small amount of entities), otherwise this would be a tedious procedure. If this is indeed the case, please read the documentation, e.g. [Mesh Generation](https://doc.freefem.org/documentation/mesh-generation.html#format-of-mesh-data) (there is everything you need). Also, for mesh connectivity you can check [Mesh Generation](https://doc.freefem.org/documentation/mesh-generation.html#mesh-connectivity-and-data) .

2. Alternatively, command `change(...)` ([Mesh Generation](https://doc.freefem.org/documentation/mesh-generation.html#the-command-change)) might also be of interest since it gives a certain amount of control over mesh entities. Actually, in the documentation it says you can prescribe a new numbering of both vertices and elements (`renumv`, `renumt`), although I never played with this.

3. As a third option, you might want to look at _gmsh_ software and explicitly construct your mesh via _.geo_ file – a tradeoff (up to some extent) between writing by hand directly a mesh into file and automatic mesh generation. You can have a great control over mesh entities labeling with a little bit of writing.

if
