You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
simulacion-permeabilidad/fftma_module/gen/lib_src/fftma2.c

110 lines
3.4 KiB
C

#include "geostat.h"
#include "chunk_array.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/*FAST FOURIER TRANSFORM MOVING AVERAGE METHOD */
/*Turns a Gaussian white noise vector into a */
/*spatially correlated vector */
/*input: */
/*variogram: structure defining the variogram */
/* model */
/*grid: structure defining the grid */
/*n: vector with the number of cells along the */
/* X, Y and Z axes for the underlying grid */
/* i = [0 1 2] */
/* --> 0 0 0 : n will be computed and */
/* updated as output */
/* --> nx ny nz: these dimensions are used */
/*realin: structure defining a realization - */
/* must be a Gaussian white noise */
/*output: */
/*realout: structure defining a realization - */
void FFTMA2(struct vario_mod variogram, struct grid_mod grid, int n[3], struct realization_mod* realin, struct realization_mod* realout, int cores, long* seed) {
int NTOT, i, j, k, NMAX, NDIM, ntot, nmax, NXYZ, nxyz;
int solver;
double temp;
chunk_array_t *covar, *ireal, *realization;
double *workr, *worki;
/*covariance axis normalization*/
axes(variogram.ap, variogram.scf, variogram.Nvario);
/*pseudo-grid definition*/
cgrid(variogram, grid, n, cores);
/*constant definition*/
NTOT = n[0] * n[1] * n[2];
ntot = NTOT + 1;
NMAX = n[0];
NDIM = 3;
for (i = 1; i < 3; i++) {
if (n[i] > NMAX)
NMAX = n[i];
if (n[i] == 1)
NDIM--;
}
nmax = NMAX + 1;
NXYZ = grid.NX * grid.NY * grid.NZ;
nxyz = NXYZ + 1;
/*array initialization*/
covar = chunk_array_create("covar.txt", ntot, 512);
ireal = chunk_array_create("ireal.txt", ntot, 512);
realization = chunk_array_create("realization.txt", ntot, 512);
workr = (double*)malloc(nmax * sizeof(double));
testmemory(workr);
worki = (double*)malloc(nmax * sizeof(double));
testmemory(worki);
printf("pre covariance\n");
/*covariance function creation*/
covariance(covar, variogram, grid, n, cores);
printf("post covariance\n");
/*power spectrum*/
printf("pre fourt1\n");
fourt(covar, ireal, n, NDIM, 1, 0, workr, worki, cores);
printf("post fourt1\n");
/*organization of the input Gaussian white noise*/
printf("pre prebuild_gwn\n");
solver = 0;
prebuild_gwn(grid, n, realin, realization, solver, cores, seed);
printf("post prebuild_gwn\n");
printf("pre fourt2\n");
/*forward fourier transform of the GWN*/
fourt(realization, ireal, n, NDIM, 1, 0, workr, worki, cores);
printf("post fourt2\n");
printf("pre build_real\n");
/* build realization in spectral domain */
build_real(n, NTOT, covar, realization, ireal, cores);
printf("post build_real\n");
chunk_array_free(covar);
remove("covar.txt");
printf("pre fourt3\n");
/*backward fourier transform*/
fourt(realization, ireal, n, NDIM, 0, 1, workr, worki, cores);
printf("post fourt3\n");
chunk_array_free(ireal);
remove("ireal.txt");
free(workr);
free(worki);
printf("pre clean_real\n");
/*output realization*/
clean_real(realin, n, grid, realization, realout, cores);
printf("post clean_real\n");
}