This repository was archived by the owner on Jan 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.c
More file actions
111 lines (83 loc) · 2.88 KB
/
simulation.c
File metadata and controls
111 lines (83 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <math.h>
#include <stdio.h>
#include "globals.h"
#include "simulation.h"
#include "tic.h"
simulation_params * simulation__create(void)
{
simulation_params *sp = malloc(sizeof *sp);
return sp;
}
void simulation__destroy(simulation_params *sp)
{
free(sp);
}
double stable_dt_from_param(double dx, double dz, double v_max)
{
double mu = 5.; // Magic parameter
double h = fmax(dx, dz);
return h / (mu * v_max);
}
double stable_dt(velocity_model *model)
{
/* Returns a stable dT based in velocity model parameters */
return stable_dt_from_param(model->dx, model->dx, model->v_max);
}
SIMUL_STATUS isstable_from_param(double frequency,
double dx, double dz,
double v_min, double v_max,
double dt)
{
SIMUL_STATUS simul_status = SIMUL_OK;
double k = 5.; // Dispersive constant k
double mu = 5.; // Stability constant Mu
double h = fmax(dx, dz);
double f_max = ricker__cnt2cut(frequency);
/* First checks dispersion against grid cell size */
if(fmax(dx, dz) > (v_min / (k*f_max)))
{
fprintf(stderr, "Grid cell is too large, velocity model minimum ");
fprintf(stderr, "velocity is too low or source frequency is too high.");
fprintf(stderr, "fix one of those.\n");
fprintf(stderr, "h: %lf\tV_min: %lf\tFreq: %lf\n", h, v_min, f_max);
simul_status &= SIMUL_DISPERSIVE;
}
if(dt > (h / (mu * v_max)))
{
fprintf(stderr, "Simulation dT is too high, Grid size is too small or");
fprintf(stderr, "model maximum velocity is too low, fix one of those.\n");
fprintf(stderr, "dt: %lf\th: %lf\tV_max: %lf\n", dt, h, v_max);
simul_status &= SIMUL_UNSTABLE;
}
return simul_status;
}
SIMUL_STATUS isstable(ricker_source *source,
velocity_model *model,
simulation_params *simul)
{
/* Returns if a given source is stable under a velocity model. Checks for
* both, dispersion and stability.
*
* Returns:
* SIMUL_UNSTABLE | SIMUL_DISPERSIVE
*/
return isstable_from_param(source->wavelet->frequency,
model->dx, model->dz, model->v_min, model->v_max, simul->dt);
}
void simulation__inject_source(wavefield *w, velocity_model *m, ricker_source *s, simulation_params *simul, size_t it)
{
// Get position of injection in the flattened array
size_t pos = w->nx * s->z + s->x;
double vel_dt2 = powf(m->vel[pos] * simul->dt, 2);
w->grid[pos] += vel_dt2 * s->wavelet->trace[it];
}
void simulation__write(size_t it, wavefield *w, simulation_params *s, FILE *fd, int ticprt)
{
if( it % s->ntrec == 0) {
fwrite(w->grid, sizeof(double), w->nx * w->nz, fd);
if(ticprt) {
fprintf(stderr, "Iteration step: %7zu/%7zu -- ", it, s->steps);
tic();
}
}
}