SOURCE_DRIVER = r'''
/*
* Driver minimal pour les noyaux verifies gr_maxwell_1d (lanyonai/GeneralRelativisticMaxwell).
*
* Le fichier amont declare un main() VIDE ("// Insert simulation drivers here.").
* Ce driver n'implemente AUCUNE formule physique : il appelle exclusivement les fonctions
* `static inline` amont, incluses telles quelles.
*
* Probleme (tranche alpha=1, beta=0, c=1) :
* d_t Dy + d_x (c^2 Bz) = 0, d_t Bz + d_x Dy = 0
*
* Usage : driver <N> <tmax> [dir] [dump] [cfl]
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define main upstream_main_unused
#include "gr_maxwell_1d.c"
#undef main
static double gauss(double x, double x0, double s)
{
double d = x - x0;
return exp(-(d * d) / (2.0 * s * s));
}
int main(int argc, char **argv)
{
int N = (argc > 1) ? atoi(argv[1]) : 128;
double tmax = (argc > 2) ? atof(argv[2]) : 0.4;
int dir = (argc > 3) ? atoi(argv[3]) : 1;
int dump = (argc > 4 && strcmp(argv[4], "dump") == 0);
double CFL = (argc > 5) ? atof(argv[5]) : 0.5;
if (N < 8) { fprintf(stderr, "N too small\n"); return 2; }
if (!(tmax > 0.0)) { fprintf(stderr, "tmax must be > 0\n"); return 2; }
if (dir != 1 && dir != -1) { fprintf(stderr, "dir must be +1 or -1\n"); return 2; }
if (!(CFL > 0.0)) { fprintf(stderr, "cfl must be > 0\n"); return 2; }
const double c = 1.0;
const double alpha = 1.0;
const double beta_x = 0.0, beta_y = 0.0, beta_z = 0.0;
const double width = 0.05;
double L = 1.0, dx = L / (double)N;
double lambda_max = fabs(alpha) * c;
double dt = CFL * dx / lambda_max;
int nsteps = (int)ceil(tmax / dt);
double tfinal = nsteps * dt;
gr_maxwell_1d_parameters P;
P.c = c;
gr_maxwell_1d_state *U = (gr_maxwell_1d_state *)calloc((size_t)N, sizeof *U);
gr_maxwell_1d_state *U0 = (gr_maxwell_1d_state *)calloc((size_t)N, sizeof *U0);
gr_maxwell_1d_lax_friedrichs_fluctuation *Aplus =
(gr_maxwell_1d_lax_friedrichs_fluctuation *)calloc((size_t)N, sizeof *Aplus);
gr_maxwell_1d_lax_friedrichs_fluctuation *Aminus =
(gr_maxwell_1d_lax_friedrichs_fluctuation *)calloc((size_t)N, sizeof *Aminus);
gr_maxwell_1d_coordinates *C = (gr_maxwell_1d_coordinates *)calloc((size_t)N, sizeof *C);
gr_maxwell_1d_state *Un = (gr_maxwell_1d_state *)calloc((size_t)N, sizeof *Un);
if (!U || !U0 || !Aplus || !Aminus || !C || !Un) { fprintf(stderr, "alloc failed\n"); return 2; }
for (int i = 0; i < N; i++) {
double x = (i + 0.5) * dx;
double g = gauss(x, 0.5, width);
C[i].x = x;
U[i].Dx = 0.0; U[i].Dy = g; U[i].Dz = 0.0;
U[i].Bx = 0.0; U[i].By = 0.0; U[i].Bz = (double)dir * g / c;
U[i].alpha = alpha;
U[i].beta_x = beta_x; U[i].beta_y = beta_y; U[i].beta_z = beta_z;
}
memcpy(U0, U, (size_t)N * sizeof *U);
for (int n = 0; n < nsteps; n++) {
for (int j = 0; j < N; j++) {
int k = (j + 1) % N;
gr_maxwell_1d_lax_friedrichs_x_right_fluctuation(&C[j], &C[k], &U[j], &U[k], &P, &Aplus[j]);
gr_maxwell_1d_lax_friedrichs_x_left_fluctuation(&C[j], &C[k], &U[j], &U[k], &P, &Aminus[j]);
}
for (int i = 0; i < N; i++) {
int im = (i - 1 + N) % N;
Un[i] = U[i];
Un[i].Dy -= (dt / dx) * (Aplus[im].fluctuation_Dy + Aminus[i].fluctuation_Dy);
Un[i].Bz -= (dt / dx) * (Aplus[im].fluctuation_Bz + Aminus[i].fluctuation_Bz);
}
memcpy(U, Un, (size_t)N * sizeof *U);
}
double l1 = 0.0, maxabs = 0.0, mass = 0.0, mass0 = 0.0, inv = 0.0;
double zre = 0.0, zim = 0.0, zre0 = 0.0, zim0 = 0.0;
for (int i = 0; i < N; i++) {
double x = (i + 0.5) * dx;
double shift = (double)dir * alpha * c * tfinal;
double xex = fmod(x - shift + 100.0 * L, L);
double gex = gauss(xex, 0.5, width);
l1 += fabs(U[i].Dy - gex) * dx;
if (fabs(U[i].Dy) > maxabs) maxabs = fabs(U[i].Dy);
double e = fabs(U[i].Dy - (double)dir * c * U[i].Bz);
if (e > inv) inv = e;
mass += U[i].Dy * dx;
mass0 += U0[i].Dy * dx;
double th = 2.0 * M_PI * x / L;
zre += U[i].Dy * cos(th); zim += U[i].Dy * sin(th);
zre0 += U0[i].Dy * cos(th); zim0 += U0[i].Dy * sin(th);
if (dump) printf("PROF %.10e %.10e %.10e\n", x, U[i].Dy, gex);
}
/* centroide circulaire : moyenne complexe ponderee (valide pour un dephasage < 1/2 tour) */
double dtheta = atan2(zim * zre0 - zre * zim0, zre * zre0 + zim * zim0);
double dcen = dtheta * L / (2.0 * M_PI);
double speed_est = (tfinal > 0.0) ? dcen / tfinal : 0.0;
printf("N=%d dir=%+d cfl=%.3f dx=%.10g dt=%.10g steps=%d t=%.6f lam=%g expected_shift=%.12e\n",
N, dir, CFL, dx, dt, nsteps, tfinal, lambda_max, (double)dir * alpha * c * tfinal);
printf("L1_Dy=%.12e Dy_max=%.12e mass=%.12e mass0=%.12e inv=%.12e dcen=%.12e speed_est=%.12e\n",
l1, maxabs, mass, mass0, inv, dcen, speed_est);
free(U); free(U0); free(Aplus); free(Aminus); free(C); free(Un);
return 0;
}
'''
TRAVAIL = pathlib.Path(tempfile.mkdtemp(prefix="grm_maxwell_"))
# write_bytes : ecrire le texte decode re-encoderait les fins de ligne (\n -> \r\n sous Windows)
# et la copie cesserait d'etre byte-identique. On ecrit les octets telecharges tels quels.
(TRAVAIL / "gr_maxwell_1d.c").write_bytes(brut)
(TRAVAIL / "driver.c").write_text(SOURCE_DRIVER, encoding="utf-8")
# Seul le nom du repertoire est affiche : le chemin absolu depend de la machine hote
# et n'apporte rien a la mesure.
print(f"repertoire de travail : {TRAVAIL.name}")
print(f"amont copie byte-identique : "
f"{hashlib.sha256((TRAVAIL / 'gr_maxwell_1d.c').read_bytes()).hexdigest() == SHA256}")
rc, sortie = compiler(["-O2", "-std=c99", "-D_GNU_SOURCE",
"-o", chemin_outil(TRAVAIL / "driver"),
chemin_outil(TRAVAIL / "driver.c"), "-lm"])
print(f"compilation rc={rc}")
print(sortie.strip() if sortie.strip() else "(aucun avertissement, aucune erreur)")
assert rc == 0, "la compilation a echoue : voir la sortie ci-dessus"