//  run with MPI:  ff-mpirun -np 4 script.edp
// NBPROC 4

load "medit"
load "PETSc"                        // PETSc plugin
load "parmmg"
macro dimension()3// EOM            // 2D or 3D
include "macro_ddm.idp"             // additional DDM functions

mesh3 Th = cube(getARGV("-global", 30), getARGV("-global", 30), getARGV("-global", 30));
DmeshCreate(Th);
fespace Vh(Th, P1);
{
    mesh3 ThParMmg;
    int[int] n2o;
    int[int][int] communicators;
    ParMmgCreateCommunicators(Th, ThParMmg, n2o, communicators);
    fespace VhParMmg(ThParMmg, P1);
    VhParMmg met = sqrt((x-0.5)^2 + (y-0.5)^2 + (z-0.5)^2) - 0.3;
    real hmin = 2.0e-2;
    real hmax = 1.0e-0;
    met = max(hmin, min(hmax, abs(met)));
    mesh3 ThNew = parmmg3d(ThParMmg, metric = met[], hausd = 0.05, nodeCommunicators = communicators);
    DmeshReconstruct(ThNew);
    DmeshCopy(ThNew, Th);
}
Vh u = sqrt((x-0.5)^2 + (y-0.5)^2 + (z-0.5)^2) - 0.3;
medit("Th", Th, u);
