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/ran2.c

81 lines
1.9 KiB
C

#include <time.h>
#include "genlib.h"
#include "log.h"
#define IM1 2147483563
#define IM2 2147483399
#define AM (1.0 / IM1)
#define IMM1 (IM1 - 1)
#define IA1 40014
#define IA2 40692
#define IQ1 53668
#define IQ2 52774
#define IR1 12211
#define IR2 3791
#define NTAB 32
#define NDIV (1 + IMM1 / NTAB)
#define EPS 1.2e-7
#define RNMX (1.0 - EPS)
double ran2(long* idum, long* idum2, long* iy, long iv[NTAB]) {
double* used_ram_t0 = malloc(sizeof(double));
getVirtualMemUsed(used_ram_t0);
clock_t t = clock();
int j;
long k;
double temp;
log_info("RESULT = in progress");
if (*idum <= 0) {
if (-(*idum) < 1)
*idum = 1;
else
*idum = -(*idum);
*idum2 = (*idum);
for (j = NTAB + 7; j >= 0; j--) {
k = (*idum) / IQ1;
*idum = IA1 * (*idum - k * IQ1) - k * IR1;
if (*idum < 0)
*idum += IM1;
if (j < NTAB)
iv[j] = *idum;
}
*iy = iv[0];
}
k = (*idum) / IQ1;
*idum = IA1 * (*idum - k * IQ1) - k * IR1;
if (*idum < 0)
*idum += IM1;
k = *idum2 / IQ2;
*idum2 = IA2 * (*idum2 - k * IQ2) - k * IR2;
if (*idum2 < 0)
*idum2 += IM2;
j = (*iy) / NDIV;
*iy = iv[j] - (*idum2);
iv[j] = *idum;
if (*iy < 1)
(*iy) += IMM1;
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
double* used_ram_tf = malloc(sizeof(double));
getVirtualMemUsed(used_ram_tf);
if ((temp = AM * (*iy)) > RNMX) {
log_info("RESULT = success, ELAPSED = %f, DIF USED VIRTUAL MEM = %5.1f MB", time_taken, *used_ram_tf - *used_ram_t0);
return (RNMX);
}
else {
log_info("RESULT = success, temp = %f, ELAPSED = %f seconds, DIF USED VIRTUAL MEM = %5.1f MB", temp, time_taken, *used_ram_tf - *used_ram_t0);
return temp;
}
free(used_ram_t0);
free(used_ram_tf);
}