# Calling a macro inside a loop error

**URL:** https://community.freefem.org/t/calling-a-macro-inside-a-loop-error/1988
**Category:** General Discussion
**Created:** [September 5, 2022, 11:56am UTC](https://community.freefem.org/t/calling-a-macro-inside-a-loop-error/1988 "2022-09-05T11:56:55Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Stefanosv](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@Stefanosv](https://community.freefem.org/u/Stefanosv)
#### Post date: [September 5, 2022, 11:56am UTC](https://community.freefem.org/t/calling-a-macro-inside-a-loop-error/1988/1 "2022-09-05T11:56:55Z")

</div>

Hello, I want to call multiple macros inside a loop but the macros only run at the first iteration and then they just get skipped every time they get called again.  
I think the issue might be with the way I set up the macros  
One of them follows bellow.

```auto
macro Elasticityfunc //
;
//Mesh
real bottombeam = 2;
border a(t=2, 0){x=0; y=t ;label=1;}	// left beam
border b(t=0, 10){x=t; y=0 ;label=2;}	// bottom of beam
border c(t=0, 2){x=10; y=t ;label=1;}	// rigth beam
border d(t=0, 4){x=10-t; y=2; label=4;}	// top beam
border d1(t=4, 5){x=10-t; y=2; label=3;} // Area where forces are applied
border d2(t=5,10) { x=10-t; y=2;label=4; };

real sigma = 0.29;
real gravity = 0;

mesh th = buildmesh(b(20) + c(5) + d(9) + a(5)+d1(2)+d2(9),fixeborder=1);
plot(th,wait=1);

//fespace Ph(th,P0); // constant par triangle
//Ph E;

// Fespace
fespace Vh(th, P2);
Vh uu, vv, w, s;

fespace Ph(th,P2);
Ph E;

real[int] Estart1(E.n);
{
	ifstream Ein("E.txt");
	Ein >> Estart1;
}

for(int i=0; i < E.n; i++){
    E[](i) = Estart1(i);
}

// Macro
real sqrt2 = sqrt(2.);
macro epsilon(u1, u2) [dx(u1), dy(u2), (dy(u1) + dx(u2))/sqrt2] // EOM
macro div(u, v) (dx(u) + dy(v)) // EOM

// Problem (with solve)
func F1=0;
func F2=-0.3;
func mu = E/(2*(1 + sigma));
func lambda = E*sigma/((1 + sigma)*(1 - 2*sigma));
//cout << "Lambda = " << lambda << endl;
//cout << "Mu = " << mu << endl;
//cout << "Gravity = " << gravity << endl;
solve Elasticity ([uu, vv], [w, s], solver=sparsesolver)
	= int2d(th)(
		  lambda*div(w, s)*div(uu, vv)
		+ 2.*mu*(epsilon(w, s)' * epsilon(uu, vv))
	)
	- int2d(th)(gravity*s)
	//+ brhs
      - int1d(th,3)( F1*w + F2*s )
	+ on(1, uu=0, vv=0)
	;
//cout << "Max displacement = " << uu[](386) << endl;
cout << "Max displacement = " << vv[].linfty << endl;

// Plot
plot([uu, vv], wait=1);
//plot([uu, vv], wait=1, bb=[[-0.5, 2.5], [2.5, -0.5]]);

// Move mesh
mesh th1 = movemesh(th, [x+uu, y+vv]);
plot(th1, wait=1);

//Stiffness Matrix
varf bin(uu,vv) = int2d(th)(dx(uu) * dx(vv) + dy(uu) * dy(vv));
matrix K = bin(Vh, Vh);

real[int] U(uu.n * 2);
real[int] ux(uu.n);
real[int] uy(vv.n);
for(int i=0; i < K.n; i++){
    U(2*i) = uu[](i);
    U(2*i+1) = vv[](i);
	ux(i) = uu[](i);
	uy(i) = vv[](i);
}

real[int] f1(K.n);
real[int] f2(K.n);

f1 = K * ux;
f2 = K * uy;

real[int] F(K.n * 2);

for(int i=0; i < K.n; i++){
    F(i) = f1(i);
    F(2*i) = f2(i);
}

//for(int i=0; i < E.n; i++){
// E2(i) = E[](i);
//}

{ofstream fout("U.txt");
fout << U << endl;
}

{ofstream fout("ux.txt");
fout << ux << endl;
}

{ofstream fout("uy.txt");
fout << uy << endl;
}

{ofstream fout("f1.txt");
fout << f1;
}

{ofstream fout("f2.txt");
fout << f2 << endl;
}

//cout << "Result = " << K.n << endl;

{ofstream fout("Stiffness_matrix.matrix");
fout << K << endl;
}
//End

```

---

<div class="post-metadata">

### Author: ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)
#### Post date: [September 6, 2022, 4:05pm UTC](https://community.freefem.org/t/calling-a-macro-inside-a-loop-error/1988/2 "2022-09-06T16:05:27Z")

</div>

Sorry , I do not understand your problem!

---

<div class="post-metadata">

### Author: ![Stefanosv](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@Stefanosv](https://community.freefem.org/u/Stefanosv)
#### Post date: [September 7, 2022, 8:33am UTC](https://community.freefem.org/t/calling-a-macro-inside-a-loop-error/1988/3 "2022-09-07T08:33:24Z")

</div>

Hello,  
I’ve the following files.  
On the Run.edp I have a loop that calls the rest of the files every time the loop starts though I’ve notices that the rest of the files only run once, at the first iteration then they get skipped.

[CalcDerivatives.idp](https://community.freefem.org/uploads/short-url/wSZaPDF9U3czao0zOdo52mJ410o.idp) (966 Bytes)  
[Elasticity.idp](https://community.freefem.org/uploads/short-url/dRSdm54Dhde9I8G9F2v9sf1ouiy.idp) (2.6 KB)  
[Interpolation.idp](https://community.freefem.org/uploads/short-url/b1tomaeFCh4aYTrUtNn2dXiiFyH.idp) (447 Bytes)  
[Run.edp](https://community.freefem.org/uploads/short-url/AitrsdYOUBr5sxXb1VPXP3QMuoE.edp) (1.3 KB)  
[Update.idp](https://community.freefem.org/uploads/short-url/nGdNmRrDbUqMdkjygBDnXLV00dD.idp) (1.3 KB)

---

<div class="post-metadata">

### Author: ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)
#### Post date: [September 7, 2022, 9:12am UTC](https://community.freefem.org/t/calling-a-macro-inside-a-loop-error/1988/4 "2022-09-07T09:12:15Z")

</div>

Your problem is due to a miss understanding about macro,

all you macro

```auto
   Elasticityfunc;
    Interpolationfunc(size);
    Derivativesfunc(size);
    Updatefunc(size);

```

are empty because the end of macro is // in basique definition  
see examplemacro in the doc [Developers](https://doc.freefem.org/examples/developers.html#examplemacro) and use the couple NewMacro / EndMacro see: [Types](https://doc.freefem.org/references/types.html#newmacro-endmacro)
