//**************************************************************
//       Trois domaines : cercle1, cercle2, ellipse avec aero  *
//                  Auteur : M.N.I. Leblond                   *
//**************************************************************

load "iovtk"; // Chargement du module pour la sortie VTK

// Chargement de la bibliothèque
load "msh3";


// Définition des paramètres temporels et autres paramètres globaux
real dt = 0.1;
real dtau = dt/2;
real L = 20;
int Niter = 200; // Densité des points de frontière
int NiterAero = 20; // suffisant pour un petit cercle
// ---------------------------
// 1) CERCLE 1
// ---------------------------
real xc1 = -6.0, yc1 = 0.0, r1 = 3.0;
border C1(t = 0, 2*pi) { x = xc1 + r1*cos(t); y = yc1 + r1*sin(t); }
mesh Th1 = buildmesh(C1(Niter));

// --- Aéroport Cercle 1 ---
real airport1x = -6.8, airport1y = 2, radius1 = 0.2;
border Air1(t=0,2*pi){ x = airport1x + radius1*cos(t); y = airport1y + radius1*sin(t);}
mesh ThAir1 = buildmesh(Air1(NiterAero));

// ---------------------------
// 2) CERCLE 2
// ---------------------------
real xc2 = 0.0, yc2 = -4.0, r2 = 3;  //r2=2.5
border C2(t = 0, 2*pi) { x = xc2 + r2*cos(t); y = yc2 + r2*sin(t); }
mesh Th2 = buildmesh(C2(Niter));

// --- Aéroport Cercle 2 ---
real airport2x = 0.0, airport2y = -5.0, radius2 = 0.2;
border Air2(t=0,2*pi){ x = airport2x + radius2*cos(t); y = airport2y + radius2*sin(t);}
mesh ThAir2 = buildmesh(Air2(NiterAero));

// ---------------------------
// 3) ELLIPSE
// ---------------------------
real xc3 = 6.0, yc3 = 0.0, a3 = 3.0, b3 = 1.5, r3=3;
//border E1(t = 0, 2*pi) { x = xc3 + r3*cos(t); y = yc3 + r3*sin(t); }
//mesh Th2 = buildmesh(C2(Niter));
border E1(t = 0, 2*pi) { x = xc3 + a3*cos(t); y = yc3 + b3*sin(t); }
mesh Th3 = buildmesh(E1(Niter));

// --- Aéroport Ellipse ---
real airport3x = 8.5, airport3y = 0.0, radius3 = 0.2;
border Air3(t=0,2*pi){ x = airport3x + radius3*cos(t); y = airport3y + radius3*sin(t);}
mesh ThAir3 = buildmesh(Air3(NiterAero));

// ---------------------------
// Visualisation
// ---------------------------
plot(Th1, Th2, Th3, ThAir1, ThAir2, ThAir3, wait = true, cmm = "Domaines avec Aero");
// --- Affichage des aéroports ---

// ---------------------------
// Sauvegarde
// ---------------------------
savemesh(Th1, "cercle1.msh");
savemesh(Th2, "cercle2.msh");
savemesh(Th3, "ellipse.msh");
savemesh(ThAir1, "airport1.msh");
savemesh(ThAir2, "airport2.msh");
savemesh(ThAir3, "airport3.msh");

cout << "Trois domaines et leurs aero crees et sauves !" << endl;




// ---------------------------
// Définition des espaces de FE
// ---------------------------
fespace Uh1(Th1, P1);
fespace Uh2(Th2, P1);
fespace Uh3(Th3, P1);

// Constantes locals
real alpha1 = 0.5;
real alpha2 = 0.5;
real alpha3 = 0.3;

real mu1 = 0.3;
real mu2 = 0.3;
real mu3 = 0.5;

// Paramètres du modèle dans Omega1
real du1 = 0.020; // Diffusivité P1
real dv1 = 0.01; // Diffusivité N1


// Paramètres du modèle dans Omega2
real du2 = 0.02; // Diffusivité P2
real dv2 = 0.01; // Diffusivité N2


// Paramètres du modèle dans Omega2
real du3 = 0.02; // Diffusivité P3
real dv3 = 0.01; // Diffusivité N3

//coefficient de couplages

real mU21 = 0.1; // depart de proie de Omega_1 vers \Omega_2
real mU31 = 0.0; // depart de proie de Omega_1 vers \Omega_3

real mU12 = 0.0; // depart de proie de \Omega_2 vers \Omega_1
real mU32 = 0.1; // depart de  proie de \Omega_2 vers \Omega_3

real mU13 = 0.0; // depart de proie de \Omega_3 vers \Omega_1
real mU23 = 0.0; // depart de proie de \Omega_3 vers \Omega_2


real mV21 = 0.0; // depart de predateur de Omega_1 vers \Omega_2
real mV31 = 0.99; // depart de  predateur de Omega_1 vers \Omega_3

real mV12 = 0.0; // depart de predateur de \Omega_2 vers \Omega_1
real mV32 = 0.99; // depart de \Omega_2 vers \Omega_3

real mV13 = 0.0; // depart de predateur de \Omega_3 vers \Omega_1
real mV23 = 0.0; // depart de predateur de \Omega_3 vers \Omega_2

macro fu1(u1, v1) (u1*(1-u1)-alpha1*u1*v1) // EOM
macro fu2(u2, v2) (u2*(1-u2)-alpha2*u2*v2) // EOM
macro fu3(u3, v3) (u3*(1-u3)-alpha3*u3*v3) // EOM

macro fv1(u1, v1) (alpha1*u1*v1-mu1*v1) // EOM
macro fv2(u2, v2) (alpha2*u2*v2-mu2*v2) // EOM
macro fv3(u3, v3) (alpha3*u3*v3-mu3*v3) // EOM


// ============================================================
//   CONDITIONS INITIALES AVEC CONTROLE DE LA MASSE TOTALE
// ============================================================

// ------------------------------------------------------------
// Domaine 1 : Populations P1 (proies) et N1 (prédateurs)
// ------------------------------------------------------------
real xcP1 = -6.0, ycP1 = 0.0, rP1 = 0.5;
real xcN1 = -5.5, ycN1 = 0.0, rN1 = 0.3;

// Masses totales désirées
real M0P1 = 0.1;     // masse totale voulue pour U1
real M0N1 = 0.2;     // masse totale voulue pour V1

// Amplitudes correspondantes
real AP1 = M0P1 / (pi * rP1^2);
real AN1 = M0N1 / (pi * rN1^2);

// Conditions initiales
Uh1 u10 = AP1 * ((x - xcP1)^2 + (y - ycP1)^2 <= rP1^2);
Uh1 v10 =  AN1 * ((x - xcN1)^2 + (y - ycN1)^2 <= rN1^2);


// ------------------------------------------------------------
// Domaine 2 : Populations P2 et N2
// ------------------------------------------------------------
real xcP2 = 0.0, ycP2 = -4.0, rP2 = 0.5;
real xcN2 = 0.2, ycN2 = -4.2, rN2 = 0.3;

real M0P2 = 0.1;     // masse totale voulue pour U2
real M0N2 = 0.2;     // masse totale voulue pour V2

real AP2 = M0P2 / (pi * rP2^2);
real AN2 = M0N2 / (pi * rN2^2);

Uh2 u20 = 0;//AP2 * ((x - xcP2)^2 + (y - ycP2)^2 <= rP2^2);
Uh2 v20 =  AN2 * ((x - xcN2)^2 + (y - ycN2)^2 <= rN2^2);


// ------------------------------------------------------------
// Domaine 3 : Populations P3 et N3
// ------------------------------------------------------------
real xcP3 = 6.0, ycP3 = 0.0, rP3 = 0.5;
real xcN3 = 6.5, ycN3 = 0.0, rN3 = 0.5;

real M0P3 = 0.1;     // masse totale voulue pour U3
real M0N3 = 0.2;     // masse totale voulue pour V3

real AP3 = M0P3 / (pi * rP3^2);
real AN3 = M0N3 / (pi * rN3^2);

Uh3 u30 = 0;//AP3 * ((x - xcP3)^2 + (y - ycP3)^2 <= rP3^2);
Uh3 v30 =0; // AN3 * ((x - xcN3)^2 + (y - ycN3)^2 <= rN3^2);


// ============================================================
//   DIAGNOSTIC : VERIFICATION DES MASSES INITIALES
// ============================================================
cout << endl;
cout << "============================================================" << endl;
cout << "             DIAGNOSTIC DES CONDITIONS INITIALES            " << endl;
cout << "============================================================" << endl;

real Iu1 = int2d(Th1)(u10);
real Iv1 = int2d(Th1)(v10);
real Iu2 = int2d(Th2)(u20);
real Iv2 = int2d(Th2)(v20);
real Iu3 = int2d(Th3)(u30);
real Iv3 = int2d(Th3)(v30);

cout << " Domaine Omega1 : " << endl;
cout << "   Masse P1 (attendue = " << M0P1 << ") -> obtenue = " << Iu1 << endl;
cout << "   Masse N1 (attendue = " << M0N1 << ") -> obtenue = " << Iv1 << endl;

cout << "\n Domaine Omega2 : " << endl;
cout << "   Masse P2 (attendue = " << M0P2 << ") -> obtenue = " << Iu2 << endl;
cout << "   Masse N2 (attendue = " << M0N2 << ") -> obtenue = " << Iv2 << endl;

cout << "\n Domaine Omega3 : " << endl;
cout << "   Masse P3 (attendue = " << M0P3 << ") -> obtenue = " << Iu3 << endl;
cout << "   Masse N3 (attendue = " << M0N3 << ") -> obtenue = " << Iv3 << endl;

cout << "============================================================" << endl;
cout << endl;


// Affichage de toutes les populations u (P1, P2, P3)
plot(u10, u20, u30, fill=true, value=true, wait=true, cmm="Conditions initiales : u (P1, P2, P3)");

// Affichage de toutes les populations v (N1, N2, N3)
plot(v10, v20, v30, fill=true, value=true, wait=true, cmm="Conditions initiales : v (N1, N2, N3)");


Uh1 uP1, vN1, wu01, wv01;
Uh2 uP2, vN2, wu02, wv02;
Uh3 uP3, vN3, wu03, wv03;

// Déclaration des fonctions de migration

Uh1 NL1; // probabilités de migrations sur \Omega_1 (P_21=P_31=epsilon_1=NL1)
Uh2 NL2; // probabilités de migrations sur \Omega_2 (P_12=P_32=epsilon_2=NL2)
Uh3 NL3; // probabilités de migrations sur \ Omega_3 (P_13=P_23=epsilon_3=NL3)

// =============================================================
// Calcul des probabilités de migration (fonction gaussienne)
// =============================================================

// --- Domaine 1 ---
for (int i = 0; i < Th1.nv; i++) {
        real X = Th1(i).x;
        real Y = Th1(i).y;
        NL1[][i] =  exp(-(( airport1x - X)^2 + ( airport1y - Y)^2)/(radius1*radius1));
    }

// --- Domaine 2 ---
for (int i = 0; i < Th2.nv; i++) {
        real X = Th2(i).x;
        real Y = Th2(i).y;
        NL2[][i] =  exp(-(( airport2x - X)^2 + ( airport2y - Y)^2)/(radius2*radius2));
    }

// --- Domaine 3 ---
for (int i = 0; i < Th3.nv; i++) {
        real X = Th3(i).x;
        real Y = Th3(i).y;
        NL3[][i] =  exp(-(( airport3x - X)^2 + ( airport3y - Y)^2)/(radius3*radius3));
    }


// =============================================================
// Visualisation
// =============================================================
// Combined view of all domains
plot(NL1, NL2, NL3, fill=true, value=false, cmm="Transit zones on all domains", wait=true);

// Individual plots for clarity
plot(NL1, fill=true, value=true, cmm="Transit zone on Omega1", wait=true);
plot(NL2, fill=true, value=true, cmm="Transit zone on Omega2", wait=true);
plot(NL3, fill=true, value=true, cmm="Transit zone on Omega3", wait=true);


//**************************************************************
//        ✦ First Diagnostic: Inter-domain Migration Fluxes ✦
//          (Computed using nonlocal influence functions NL_i)
//**************************************************************

// --- Flux entering Omega_1 from Omega_2 and Omega_3 ---
real intU12 = int2d(Th2)(mU12 * u20 * NL2);   // flux of u from Ω2 → Ω1
real intV12 = int2d(Th2)(mV12 * v20 * NL2);   // flux of v from Ω2 → Ω1
real intU13 = int2d(Th3)(mU13 * u30 * NL3);   // flux of u from Ω3 → Ω1
real intV13 = int2d(Th3)(mV13 * v30 * NL3);   // flux of v from Ω3 → Ω1

// --- Flux entering Omega_2 from Omega_1 and Omega_3 ---
real intU21 = int2d(Th1)(mU21 * u10 * NL1);   // flux of u from Ω1 → Ω2
real intV21 = int2d(Th1)(mV21 * v10 * NL1);   // flux of v from Ω1 → Ω2
real intU23 = int2d(Th3)(mU23 * u30 * NL3);   // flux of u from Ω3 → Ω2
real intV23 = int2d(Th3)(mV23 * v30 * NL3);   // flux of v from Ω3 → Ω2

// --- Flux entering Omega_3 from Omega_1 and Omega_2 ---
real intU31 = int2d(Th1)(mU31 * u10 * NL1);   // flux of u from Ω1 → Ω3
real intV31 = int2d(Th1)(mV31 * v10 * NL1);   // flux of v from Ω1 → Ω3
real intU32 = int2d(Th2)(mU32 * u20 * NL2);   // flux of u from Ω2 → Ω3
real intV32 = int2d(Th2)(mV32 * v20 * NL2);   // flux of v from Ω2 → Ω3

//**************************************************************
//              ✦ Summary Report – Migration Diagnosis ✦
//**************************************************************

cout << endl;
cout << "============================================================" << endl;
cout << "              FIRST DIAGNOSTIC OF MIGRATION FLOWS           " << endl;
cout << "============================================================" << endl;

cout << "\n  Fluxes entering omega_1:" << endl;
cout << "    From omega_2 : U12 = " << intU12 << "   |   V12 = " << intV12 << endl;
cout << "    From omega_3 : U13 = " << intU13 << "   |   V13 = " << intV13 << endl;

cout << "\n  Fluxes entering omega_2:" << endl;
cout << "    From omega_1 : U21 = " << intU21 << "   |   V21 = " << intV21 << endl;
cout << "    From omega_3 : U23 = " << intU23 << "   |   V23 = " << intV23 << endl;

cout << "\n  Fluxes entering omega_3:" << endl;
cout << "    From omega_1 : U31 = " << intU31 << "   |   V31 = " << intV31 << endl;
cout << "    From omega_2 : U32 = " << intU32 << "   |   V32 = " << intV32 << endl;

cout << "============================================================" << endl;


// ---------------------------
// Omega_1 : Proie 1 (u13)
// --------------------------

problem Proie1(uP1,wu01) =
    int2d(Th1)(uP1*wu01) -int2d(Th1)(u10*wu01)
    +int2d(Th1)(dt/2*du1*dx(uP1)*dx(wu01)+ dt/2*du1*dy(uP1)*dy(wu01))
	+int2d(Th1)(-dt/2*NL1*intU12*wu01-dt/2*NL1*intU13*wu01)  // flux migratoire entrant
	+int2d(Th1)(dt/2*mU21*uP1*NL1*wu01+dt/2*mU31*uP1*NL1*wu01); //flux migratoire sortant
	
// ---------------------------
// Omega_1 : Predateur 1 (vN1)
// ---------------------------
problem Predat1(vN1, wv01) =
    int2d(Th1)( vN1*wv01 ) -int2d(Th1)( v10*wv01 )
    +int2d(Th1)( dt/2*dv1*dx(vN1)*dx(wv01) + dt/2*dv1*dy(vN1)*dy(wv01) )
    +int2d(Th1)(-dt/2*NL1*intV12*wv01-dt/2*NL1*intV13*wv01)   // flux migratoire entrant
    +int2d(Th1)(dt/2*mV21*vN1*NL1*wv01+dt/2*mV31*vN1*NL1*wv01); //flux migratoire sortant


// ---------------------------
// Omega_2 : Proie 2 (uP2)
// ---------------------------
problem Proie2(uP2,wu02) =
    int2d(Th2)(uP2*wu02) -int2d(Th2)(u20*wu02)
    +int2d(Th2)(dt/2*du2*dx(uP2)*dx(wu02)+ dt/2*du2*dy(uP2)*dy(wu02))
	+int2d(Th2)(-dt/2*NL2*intU21*wu02-dt/2*NL2*intU23*wu02)   // flux migratoire entrant
	+int2d(Th2)(dt/2*mU12*uP2*NL2*wu02+dt/2*mU32*uP2*NL2*wu02); //flux migratoire sortant
	
// ---------------------------
// Omega_2 : Predateur 2 (vN2)
// ---------------------------
problem Predat2(vN2, wv02) =
    int2d(Th2)( vN2*wv02 ) -int2d(Th2)( v20*wv02 )
    +int2d(Th2)( dt/2*dv2*dx(vN2)*dx(wv02) + dt/2*dv2*dy(vN2)*dy(wv02) )
    +int2d(Th2)(-dt/2*NL2*intV21*wv02-dt/2*NL2*intV23*wv02)   // flux migratoire entrant
    +int2d(Th2)(dt/2*mV12*vN2*NL2*wv02+dt/2*mV32*vN2*NL2*wv02); //flux migratoire sortant
    
    
// ---------------------------
// Omega_3 : Proie 3 (uP3)
// ---------------------------
problem Proie3(uP3, wu03) =
    int2d(Th3)( uP3 * wu03 ) - int2d(Th3)( u30 * wu03 )
  + int2d(Th3)( dt/2*du3*dx(uP3)*dx(wu03) + dt/2*du3*dy(uP3)*dy(wu03) )
  // flux migratoire entrant depuis Omega1 et Omega2 (pondéré par NL3)
  + int2d(Th3)( -dt/2 * NL3 * intU31 * wu03 - dt/2 * NL3 * intU32 * wu03 )
  // flux migratoire sortant vers Omega1 et Omega2 (m13 et m23)
  + int2d(Th3)( dt/2 * mU13 * uP3 * NL3 * wu03 + dt/2 * mU23 * uP3 * NL3 * wu03 );

// ---------------------------
// Omega_3 : Predateur 3 (vN3)
// ---------------------------
problem Predat3(vN3, wv03) =
    int2d(Th3)( vN3 * wv03 ) - int2d(Th3)( v30 * wv03 )
  + int2d(Th3)( dt/2*dv3*dx(vN3)*dx(wv03) + dt/2*dv3*dy(vN3)*dy(wv03) )
  // flux migratoire entrant depuis Omega1 et Omega2
  + int2d(Th3)( -dt/2 * NL3 * intV31 * wv03 - dt/2 * NL3 * intV32 * wv03 )
  // flux migratoire sortant vers Omega1 et Omega2
  + int2d(Th3)( dt/2 * mV13 * vN3 * NL3 * wv03 + dt/2 * mV23 * vN3 * NL3 * wv03 );

// ---- Résolution des problèmes ----
Proie1; u10 = uP1;
Predat1; v10 = vN1;

Proie2; u20 = uP2;
Predat2; v20 = vN2;

Proie3; u30 = uP3;
Predat3; v30 = vN3;

// ---- Visualisation des proies ----
plot(u10, u20, u30, fill=true, value=true,cmm="Populations de Proies : Omega1 - Omega2 - Omega3",wait=true);

// ---- Visualisation des prédateurs ----
plot(v10, v20, v30, fill=true, value=true, cmm="Populations de Predateurs : Omega1 - Omega2 - Omega3",wait=true);




int M = 250; // Nombre total d'étapes de temps



// Préparation des structures de données pour l'enregistrement des résultats
Uh1[int] X(2);
Uh2[int] Y(2);
Uh3[int] Z(2);


cout <<"entrez M le nombre d_iteration"<<endl;
cin >> M;
real t=0;




ofstream file1("data_P1N1-pp.csv"); 
file1.precision(3);
file1 << "t,U1_total,V1_total,U_flux_in,V_flux_in,U_flux_out,V_flux_out" << endl;

ofstream file2("data_P2N2-pp.csv");
file2.precision(3);
file2 << "t,U2_total,V2_total,U_flux_in,V_flux_in,U_flux_out,V_flux_out" << endl;

ofstream file3("data_P3N3-pp.csv");
file3.precision(3);
file3 << "t,U3_total,V3_total,U_flux_in,V_flux_in,U_flux_out,V_flux_out" << endl;



for (int m = 0; m < M; m++) {


 // --- Flux entering Omega_1 from Omega_2 and Omega_3 ---
intU12 = int2d(Th2)(mU12 * u20 * NL2);   // flux of u from Ω2 → Ω1
intV12 = int2d(Th2)(mV12 * v20 * NL2);   // flux of v from Ω2 → Ω1
intU13 = int2d(Th3)(mU13 * u30 * NL3);   // flux of u from Ω3 → Ω1
intV13 = int2d(Th3)(mV13 * v30 * NL3);   // flux of v from Ω3 → Ω1

// --- Flux entering Omega_2 from Omega_1 and Omega_3 ---
intU21 = int2d(Th1)(mU21 * u10 * NL1);   // flux of u from Ω1 → Ω2
intV21 = int2d(Th1)(mV21 * v10 * NL1);   // flux of v from Ω1 → Ω2
intU23 = int2d(Th3)(mU23 * u30 * NL3);   // flux of u from Ω3 → Ω2
intV23 = int2d(Th3)(mV23 * v30 * NL3);   // flux of v from Ω3 → Ω2

// --- Flux entering Omega_3 from Omega_1 and Omega_2 ---
intU31 = int2d(Th1)(mU31 * u10 * NL1);   // flux of u from Ω1 → Ω3
intV31 = int2d(Th1)(mV31 * v10 * NL1);   // flux of v from Ω1 → Ω3
intU32 = int2d(Th2)(mU32 * u20 * NL2);   // flux of u from Ω2 → Ω3
intV32 = int2d(Th2)(mV32 * v20 * NL2);   // flux of v from Ω2 → Ω3
    
// ---- Résolution des problèmes ----
Proie1; u10 = uP1;
Predat1; v10 = vN1;

Proie2; u20 = uP2;
Predat2; v20 = vN2;

Proie3; u30 = uP3;
Predat3; v30 = vN3;



    real U1total = int2d(Th1)(u10);
    real V1total = int2d(Th1)(v10);
    real U2total = int2d(Th2)(u20);
    real V2total = int2d(Th2)(v20);
    real U3total = int2d(Th3)(u30);
    real V3total = int2d(Th3)(v30);

    // --- Calcul des flux totaux sortants ---
    real U1fluxout = int2d(Th1)((mU21 + mU31) * u10 * NL1);
    real V1fluxout = int2d(Th1)((mV21 + mV31) * v10 * NL1);
    
    real U2fluxout = int2d(Th2)((mU12 + mU32) * u20 * NL2);
    real V2fluxout = int2d(Th2)((mV12 + mV32) * v20 * NL2);
    
    
    real U3fluxout = int2d(Th3)((mU13 + mU23) * u30 * NL3);
    real V3fluxout = int2d(Th3)((mV13 + mV23) * v30 * NL3);
    
    //----------------------------------------------------
    
    
    



    // --- Enregistrement des données ---
    file1 << t << "," << U1total << "," << V1total << "," 
          << intU12 + intU13 << "," << intV12 + intV13 << ","
          << U1fluxout << "," << V1fluxout << endl;

    file2 << t << "," << U2total << "," << V2total << ","
          << intU21 + intU23 << "," << intV21 + intV23 << ","
          << U2fluxout << "," << V2fluxout << endl;

    file3 << t << "," << U3total << "," << V3total << ","
          << intU31 + intU32 << "," << intV31 + intV32 << ","
          << U3fluxout << "," << V3fluxout << endl;

    // --- Mise à jour du temps ---

 t=t + dtau;
  	
     	 X[0]=uP1+dtau*fu1(uP1,vN1);//u1
  	 X[1]=vN1+dtau*fv1(uP1,vN1);//v1
  	 Y[0]=uP2+dtau*fu2(uP2,vN2);//u2
  	 Y[1]=vN2+dtau*fv2(uP2,vN2);//v2
  	 Z[0]=uP3+dtau*fu3(uP3,vN3);//u3
  	 Z[1]=vN3+dtau*fv3(uP3,vN3);//v3
  	  
  	 u10=uP1+dt*fu1(X[0],X[1]); //u1
   	 v10=vN1+dt*fv1(X[0],X[1]); //v1
         u20=uP2+dt*fu2(Y[0],Y[1]); //u2
  	 v20=vN2+dt*fv2(Y[0],Y[1]); //v2   
     	 u30=uP3+dt*fu3(Z[0],Z[1]); //u3
   	 v30=vN3+dt*fv3(Z[0],Z[1]); //v1
  t=t + dt ;

// --- Flux entering Omega_1 from Omega_2 and Omega_3 ---
intU12 = int2d(Th2)(mU12 * u20 * NL2);   // flux of u from Ω2 → Ω1
intV12 = int2d(Th2)(mV12 * v20 * NL2);   // flux of v from Ω2 → Ω1
intU13 = int2d(Th3)(mU13 * u30 * NL3);   // flux of u from Ω3 → Ω1
intV13 = int2d(Th3)(mV13 * v30 * NL3);   // flux of v from Ω3 → Ω1

// --- Flux entering Omega_2 from Omega_1 and Omega_3 ---
intU21 = int2d(Th1)(mU21 * u10 * NL1);   // flux of u from Ω1 → Ω2
intV21 = int2d(Th1)(mV21 * v10 * NL1);   // flux of v from Ω1 → Ω2
intU23 = int2d(Th3)(mU23 * u30 * NL3);   // flux of u from Ω3 → Ω2
intV23 = int2d(Th3)(mV23 * v30 * NL3);   // flux of v from Ω3 → Ω2

// --- Flux entering Omega_3 from Omega_1 and Omega_2 ---
intU31 = int2d(Th1)(mU31 * u10 * NL1);   // flux of u from Ω1 → Ω3
intV31 = int2d(Th1)(mV31 * v10 * NL1);   // flux of v from Ω1 → Ω3
intU32 = int2d(Th2)(mU32 * u20 * NL2);   // flux of u from Ω2 → Ω3
intV32 = int2d(Th2)(mV32 * v20 * NL2);   // flux of v from Ω2 → Ω3
    
// ---- Résolution des problèmes ----
Proie1; u10 = uP1;
Predat1; v10 = vN1;

Proie2; u20 = uP2;
Predat2; v20 = vN2;

Proie3; u30 = uP3;
Predat3; v30 = vN3;






plot(v10, v20, v30, fill=true, value=true,cmm="Populations de Predateurs : Omega1 - Omega2 - Omega3");

//plot(u10, u20, u30, fill=true, value=true,cmm="Populations de Proies : Omega1 - Omega2 - Omega3");

 // ---- Calcul des intégrales totales sur chaque domaine ----
    real totalU1 = int2d(Th1)(u10);
    real totalV1 = int2d(Th1)(v10);
    real totalU2 = int2d(Th2)(u20);
    real totalV2 = int2d(Th2)(v20);
    real totalU3 = int2d(Th3)(u30);
    real totalV3 = int2d(Th3)(v30);

    // ---- Diagnostic ----
    cout << endl;
    cout << "============================================================" << endl;
    cout << "             DIAGNOSTIC ET SUIVI DES POPULATIONS           " << endl;
    cout << "                  Temps t = " << t << "                         " << endl;
    cout << "============================================================" << endl;

    cout << "\n  Flux entrants sur omega_1 :" << endl;
    cout << "    Depuis omega_2 : U12 = " << intU12 << " | V12 = " << intV12 << endl;
    cout << "    Depuis omega_3 : U13 = " << intU13 << " | V13 = " << intV13 << endl;
    cout << "  Population totale sur omega_1 : U1 = " << totalU1 << " | V1 = " << totalV1 << endl;

    cout << "\n  Flux entrants sur omega_2 :" << endl;
    cout << "    Depuis omega_1 : U21 = " << intU21 << " | V21 = " << intV21 << endl;
    cout << "    Depuis omega_3 : U23 = " << intU23 << " | V23 = " << intV23 << endl;
    cout << "  Population totale sur omega_2 : U2 = " << totalU2 << " | V2 = " << totalV2 << endl;

    cout << "\n  Flux entrants sur omega_3 :" << endl;
    cout << "    Depuis omega_1 : U31 = " << intU31 << " | V31 = " << intV31 << endl;
    cout << "    Depuis omega_2 : U32 = " << intU32 << " | V32 = " << intV32 << endl;
    cout << "  Population totale sur omega_3 : U3 = " << totalU3 << " | V3 = " << totalV3 << endl;

    cout << "============================================================" << endl;
    
savevtk("Proie-predat-N1/N1_"+m+".vtu",Th1, u10, v10, dataname="  U1 V1");

savevtk("Proie-predat-N2/N2_"+m+".vtu",Th2, u20, v20,dataname=" U2 V2");

savevtk("Proie-predat-N3/N3_"+m+".vtu",Th3, u30, v30,dataname=" U3 V3");



	}






























