-
Notifications
You must be signed in to change notification settings - Fork 0
/
daphnis.c
721 lines (676 loc) · 23.8 KB
/
daphnis.c
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
// declare functions
double min(double x, double y);
void swapArray(double *a, double *b, int n);
void add_boundary_velocity(int N, int M, double *vx, double *vy, double *S_vx, double *S_vy);
void add_boundary_source(int N, int M, double *p, int S);
void add_source(int N, int M, double *v, double *a, double dt);
void add_non_inertial_source(int N, int M, double *vx, double *vy, double dx, double dt);
void diffuse(int N, int M, double *p, double *p0, double D, double dx, double dt);
void advect(int N, int M, double *p, double *p0, double *vx, double *vy, double dx, double dt);
void new_advect(int N, int M, double *p, double *p0, double *vx, double *vy, double dx, double dt);
void new_advect_x_y(int N, int M, double *vx, double *vy, double *vx0, double *vy0, double *p, double *T_p, double dx, double dt);
void vel_step(int N, int M, double *vx, double *vy, double *vx0, double *vy0, double *ax, double *ay, double *S_vx, double *S_vy, double *p, double *T_p, double visc, double dx, double dt);
void dens_step(int N, int M, double *p, double *p0, double *vx, double *vy, double D, double dx, double dt);
void write_out(int N, int M, double *p);
void write_out_u(int N, int M, double *p);
void write_out_v(int N, int M, double *p);
double min(double x, double y)
{
if (x <= y)
{
return x;
}
else
{
return y;
}
}
void swapArray(double *a, double *b, int n)
{
for (int i = 0; i < n; i++)
{
double tmp = a[i];
a[i] = b[i];
b[i] = tmp;
}
// double *tmp;
// tmp = a;
// a = b;
// b = tmp;
}
int IX(int i, int j, int N)
{
return i + N * j;
}
void add_boundary_velocity(int N, int M, double *vx, double *vy, double *S_vx, double *S_vy)
{
int i0 = N / 2;
int j0 = M / 2;
for (int j = 0; j < M; j++)
{
if (j < j0)
{
vx[0 + N * j] = S_vx[j];
vy[0 + N * j] = S_vy[j];
//vx[1 + N*j] = S_vx[j];
}
else
{
vx[N - 1 + N * j] = S_vx[j];
vy[N - 1 + N * j] = S_vx[j];
//vx[N-2 + N*j] = S_vx[j];
}
}
// v=0 at Daphnis
vx[IX(i0, j0, N)] = 0;
vx[IX(i0 - 1, j0, N)] = 0;
vx[IX(i0, j0 - 1, N)] = 0;
vx[IX(i0 - 1, j0 - 1, N)] = 0;
vy[IX(i0, j0, N)] = 0;
vy[IX(i0 - 1, j0, N)] = 0;
vy[IX(i0, j0 - 1, N)] = 0;
vy[IX(i0 - 1, j0 - 1, N)] = 0;
}
void add_boundary_source(int N, int M, double *p, int S)
{
int i0 = N / 2;
int j0 = M / 2;
for (int j = 0; j < M; j++)
{
// if ((j0 - 1 - 4 <= j) && (j <= j0 + 4))
// {
// continue;
// }
// else
// {
// if (j < j0 - 1 - 4)
// {
// p[0 + N * j] = (double)S;
// //p[1 + N*j] = (double) S;
// //p[2 + N*j] = (double) S;
// //p[3 + N*j] = (double) S;
// }
// else
// {
// //p[N-4 + N*j] = (double) S;
// //p[N-3 + N*j] = (double) S;
// //p[N-2 + N*j] = (double) S;
// p[N - 1 + N * j] = (double)S;
// }
// }
if (j <= j0 - 1)
{
p[0 + N * j] = (double)S;
}
else
{
p[N - 1 + N * j] = (double)S;
}
p[IX(i0, j0, N)] = 0;
p[IX(i0 - 1, j0, N)] = 0;
p[IX(i0, j0 - 1, N)] = 0;
p[IX(i0 - 1, j0 - 1, N)] = 0;
// if (j < j0)
// {
// p[0 + N*j] = (double) S;
// //p[1 + N*j] = (double) S;
// //p[2 + N*j] = (double) S;
// //p[3 + N*j] = (double) S;
// }
// else
// {
// //p[N-4 + N*j] = (double) S;
// //p[N-3 + N*j] = (double) S;
// //p[N-2 + N*j] = (double) S;
// p[N-1 + N*j] = (double) S;
// }
}
}
void add_source(int N, int M, double *v, double *a, double dt)
{
// int i = (int) N/2;
// int j = (int) M/2;
// p[IX(i,j,N)] -= dt*S;
for (int i = 0; i < N * M; i++)
{
v[i] += dt * a[i];
//p[i] -= p[i]*S[i];
}
}
void add_non_inertial_source(int N, int M, double *vx, double *vy, double dx, double dt)
{
double G = 6.67E-20; //e-20
double saturn_mass = 5.683e26; //e26
double daphnis_radius = 136505;
double x, y;
double N0 = (double)(N - 1) / 2;
double M0 = (double)(M - 1) / 2;
double w = sqrt(G * saturn_mass / (daphnis_radius * daphnis_radius * daphnis_radius));
double c1 = 2 * w; // Coriolis
double c2 = w * w; // Centrifugal
for (int i = 0; i < N * M; i++)
{
x = dx * (i % N - N0);
y = dx * (i / N - M0);
vx[i] += dt * (-1 * c1 * vy[i] + c2 * x);
vy[i] += dt * (c1 * vx[i] + c2 * y);
}
}
void diffuse(int N, int M, double *p, double *p0, double D, double dx, double dt)
{
double a = D * dt / (dx * dx);
int n = 100;
for (int k = 0; k < n; k++)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
// boundary conditions
if (i == 0)
{
if (j == 0)
{
//p[IX(i,j,N)] = (p0[IX(i,j,N)] + a*(p[IX(N-1,j,N)]+p[IX(i+1,j,N)]+dx*p[IX(i,j+1,N)]))/(1+2*a+a*dx);
p[IX(i, j, N)] = (p0[IX(i, j, N)] + a * dx * (p[IX(i + 1, j, N)] + p[IX(i, j + 1, N)])) / (1 + 2 * a * dx);
}
else if (j == M - 1)
{
//p[IX(i,j,N)] = (p0[IX(i,j,N)] + a*(p[IX(N-1,j,N)]+p[IX(i+1,j,N)]+dx*p[IX(i,j-1,N)]))/(1+2*a+a*dx);
p[IX(i, j, N)] = (p0[IX(i, j, N)] + a * dx * (p[IX(i + 1, j, N)] + p[IX(i, j - 1, N)])) / (1 + 2 * a * dx);
}
else
{
//p[IX(i,j,N)] = (p0[IX(i,j,N)] + a*(p[IX(N-1,j,N)]+p[IX(i+1,j,N)]+p[IX(i,j-1,N)]+p[IX(i,j+1,N)]))/(1+4*a);
p[IX(i, j, N)] = (p0[IX(i, j, N)] + a * (dx * p[IX(i + 1, j, N)] + p[IX(i, j - 1, N)] + p[IX(i, j + 1, N)])) / (1 + 2 * a + a * dx);
}
}
else if (i == N - 1)
{
if (j == 0)
{
//p[IX(i,j,N)] = (p0[IX(i,j,N)] + a*(p[IX(i-1,j,N)]+p[IX(0,j,N)]+dx*p[IX(i,j+1,N)]))/(1+2*a+a*dx);
p[IX(i, j, N)] = (p0[IX(i, j, N)] + a * dx * (p[IX(i - 1, j, N)] + dx * p[IX(i, j + 1, N)])) / (1 + 2 * a * dx);
}
else if (j == M - 1)
{
//p[IX(i,j,N)] = (p0[IX(i,j,N)] + a*(p[IX(i-1,j,N)]+p[IX(0,j,N)]+dx*p[IX(i,j-1,N)]))/(1+2*a+a*dx);
p[IX(i, j, N)] = (p0[IX(i, j, N)] + a * dx * (p[IX(i - 1, j, N)] + dx * p[IX(i, j - 1, N)])) / (1 + 2 * a * dx);
}
else
{
//p[IX(i,j,N)] = (p0[IX(i,j,N)] + a*(p[IX(i-1,j,N)]+p[IX(0,j,N)]+p[IX(i,j-1,N)]+p[IX(i,j+1,N)]))/(1+4*a);
p[IX(i, j, N)] = (p0[IX(i, j, N)] + a * (dx * p[IX(i - 1, j, N)] + p[IX(i, j - 1, N)] + p[IX(i, j + 1, N)])) / (1 + 2 * a + a * dx);
}
}
else
{
if (j == 0)
{
p[IX(i, j, N)] = (p0[IX(i, j, N)] + a * (p[IX(i - 1, j, N)] + p[IX(i + 1, j, N)] + dx * p[IX(i, j + 1, N)])) / (1 + 2 * a + a * dx);
}
else if (j == M - 1)
{
p[IX(i, j, N)] = (p0[IX(i, j, N)] + a * (p[IX(i - 1, j, N)] + p[IX(i + 1, j, N)] + dx * p[IX(i, j - 1, N)])) / (1 + 2 * a + a * dx);
}
else
{
p[IX(i, j, N)] = (p0[IX(i, j, N)] + a * (p[IX(i - 1, j, N)] + p[IX(i + 1, j, N)] + p[IX(i, j - 1, N)] + p[IX(i, j + 1, N)])) / (1 + 4 * a);
}
}
}
}
}
}
void advect(int N, int M, double *p, double *p0, double *vx, double *vy, double dx, double dt)
{
double x, y, s0, s1, t1, t0;
int i0, j0, i1, j1;
for (int j = 0; j < M; j++)
{
for (int i = 0; i < N; i++)
{
x = i - dt * vx[IX(i, j, N)] / dx; // needs to be old v, currently v is constant though
y = j - dt * vy[IX(i, j, N)] / dx; // for small dx this should be okay
//printf("%i %lf %lf\n", j, vy[IX(i,j,N)], y);
// if x was initially < 0
while (x < 0)
{
// after this x will be somewhere between [0,N]
x += N;
}
// if x was initially > N
while (x > N)
{
// after this x will be somewhere between [0,N]
x -= N;
}
if (y < 0)
{
y = 0;
}
if (y > M - 1)
{
y = M - 2;
}
i0 = (int)x;
i1 = (int)i0 + 1;
if (i0 == N - 1)
{
i1 = 0;
}
j0 = (int)y;
j1 = (int)j0 + 1;
s1 = x - i0;
s0 = 1 - s1; // = i1-x
t1 = y - j0;
t0 = 1 - t1; // = t1-x
// if (i<5 && j==49)
// {
// printf("%i %i %i %i\n",i0,i1,j0,j1);
// }
p[IX(i, j, N)] = s0 * (t0 * p0[IX(i0, j0, N)] + t1 * p0[IX(i0, j1, N)]) + s1 * (t0 * p0[IX(i1, j0, N)] + t1 * p0[IX(i1, j1, N)]);
}
}
}
void new_advect(int N, int M, double *p, double *p0, double *vx, double *vy, double dx, double dt)
{
for (int j = 0; j < M; j++)
{
for (int i = 0; i < N; i++)
{
p[IX(i, j, N)] = 0;
}
}
double x, y, s0, s1, t1, t0;
int i0, j0, i1, j1;
for (int j = 0; j < M; j++)
{
for (int i = 0; i < N; i++)
{
//printf("%i/%i\n",(i+1)+(j)*N,M*N);
// x and y are the (double) gridpoints that (i,j) points to.
x = i + dt * vx[IX(i, j, N)] / dx; // needs to be old v, currently v is constant though
y = j + dt * vy[IX(i, j, N)] / dx; // for small dx this should be okay
//printf("%i %lf %lf\n", j, vy[IX(i,j,N)], y);
//printf("%lf %lf %lf %lf\n",x,y,dt,dx);
// if x was initially < 0
// while (x < 0)
// {
// // after this x will be somewhere between [0,N]
// x += N;
// }
// // if x was initially > N
// while (x > N)
// {
// // after this x will be somewhere between [0,N]
// x -= N;
// }
if (x < 0 || x > N - 1)
{
//printf("%i %i %lf %lf\n",i, j, x, y);
continue;
}
if (y < 0)
{
//y = 0;
continue;
}
if (y > M - 1)
{
//y = M-2;
continue;
}
i0 = (int)x;
i1 = (int)i0 + 1;
j0 = (int)y;
j1 = (int)j0 + 1;
if (i0 == N - 1)
{
i0 = N - 2;
i1 = N - 1;
}
if (j0 == M - 1)
{
j0 = M - 2;
j1 = M - 1;
}
s1 = x - i0;
s0 = 1 - s1; // = i1-x
t1 = y - j0;
t0 = 1 - t1; // = t1-x
// if (i == 0 || i == N-1)
// {
// printf("DEN %i %i %lf %0.15lf\n",i, j, x-(double)i, y-(double)j);
// }
double k = p0[IX(i, j, N)];
// if (j > 34)
// {
// printf("DEN: i=%i j=%i dx=%lf dy=%0.15lf vx=%0.15lf vy=%0.15lf\n",i, j, x-(double)i, y-(double)j, vx[IX(i,j,N)], vy[IX(i,j,N)]);
// }
p[IX(i0, j0, N)] += s0 * t0 * k;
p[IX(i0, j1, N)] += s0 * t1 * k;
p[IX(i1, j0, N)] += s1 * t0 * k;
p[IX(i1, j1, N)] += s1 * t1 * k;
}
}
}
void new_advect_x_y(int N, int M, double *vx, double *vy, double *vx0, double *vy0, double *p, double *T_p, double dx, double dt)
{
for (int j = 0; j < M; j++)
{
for (int i = 0; i < N; i++)
{
vx[IX(i, j, N)] = 0;
vy[IX(i, j, N)] = 0;
T_p[IX(i, j, N)] = 0;
}
}
double x, y, s0, s1, t1, t0;
int i0, j0, i1, j1;
for (int j = 0; j < M; j++)
{
for (int i = 0; i < N; i++)
{
// x and y are the (double) gridpoints that (i,j) points to.
x = i + dt * vx0[IX(i, j, N)] / dx; // needs to be old v, currently v is constant though
y = j + dt * vy0[IX(i, j, N)] / dx; // for small dx this should be okay
// if x was initially < 0
// while (x < 0)
// {
// // after this x will be somewhere between [0,N]
// x += N;
// }
// // if x was initially > N
// while (x > N)
// {
// // after this x will be somewhere between [0,N]
// x -= N;
// }
if (x < 0 || x > N - 1)
{
//printf("%i %i %lf %lf\n",i, j, x, y);
continue;
}
if (y < 0)
{
//y = 0;
continue;
}
if (y > M - 1)
{
//y = M-2;
continue;
}
i0 = (int)x;
i1 = (int)i0 + 1;
j0 = (int)y;
j1 = (int)j0 + 1;
if (i0 == N - 1)
{
i0 = N - 2;
i1 = N - 1;
}
if (j0 == M - 1)
{
j0 = M - 2;
j1 = M - 1;
}
s1 = x - i0;
s0 = 1 - s1; // = i1-x
t1 = y - j0;
t0 = 1 - t1; // = t1-x
//printf("VEL %i %i %lf %0.15lf\n",i, j, x-(double)i, y-(double)j);
double k1 = vx0[IX(i, j, N)];
double k2 = vy0[IX(i, j, N)];
double p1 = p[IX(i, j, N)];
/* total sums of m_i*u_i */
vx[IX(i0, j0, N)] += s0 * t0 * k1 * p1;
vx[IX(i0, j1, N)] += s0 * t1 * k1 * p1;
vx[IX(i1, j0, N)] += s1 * t0 * k1 * p1;
vx[IX(i1, j1, N)] += s1 * t1 * k1 * p1;
vy[IX(i0, j0, N)] += s0 * t0 * k2 * p1;
vy[IX(i0, j1, N)] += s0 * t1 * k2 * p1;
vy[IX(i1, j0, N)] += s1 * t0 * k2 * p1;
vy[IX(i1, j1, N)] += s1 * t1 * k2 * p1;
/* total sums of "masses", m_i */
T_p[IX(i0, j0, N)] += s0 * t0 * p1;
T_p[IX(i0, j1, N)] += s0 * t1 * p1;
T_p[IX(i1, j0, N)] += s1 * t0 * p1;
T_p[IX(i1, j1, N)] += s1 * t1 * p1;
}
}
for (int j = 0; j < M; j++)
{
for (int i = 0; i < N; i++)
{
double total_p = T_p[IX(i, j, N)];
if (total_p < 1)
{
vx[IX(i, j, N)] = vx0[IX(i, j, N)];
vy[IX(i, j, N)] = vy0[IX(i, j, N)];
}
else
{
vx[IX(i, j, N)] /= total_p;
vy[IX(i, j, N)] /= total_p;
}
}
}
}
void vel_step(int N, int M, double *vx, double *vy, double *vx0, double *vy0, double *ax, double *ay, double *S_vx, double *S_vy, double *p, double *T_p, double visc, double dx, double dt)
{
//write_out(N,M,vy);
add_source(N, M, vx, ax, dt);
add_source(N, M, vy, ay, dt);
//add_non_inertial_source(N, M, vx, vy, dx, dt);
swapArray(vx0, vx, N * M);
swapArray(vy0, vy, N * M);
diffuse(N, M, vx, vx0, visc, dx, dt);
diffuse(N, M, vy, vy0, visc, dx, dt);
swapArray(vx0, vx, N * M);
swapArray(vy0, vy, N * M);
new_advect_x_y(N, M, vx, vy, vx0, vy0, p, T_p, dx, dt);
add_boundary_velocity(N, M, vx, vy, S_vx, S_vy);
//printf("vel %0.15lf %0.15lf %0.15lf %0.15lf\n",vx[IX(49,24,N)],vy[IX(49,24,N)],vx0[IX(49,24,N)],vy0[IX(49,24,N)]);
}
void dens_step(int N, int M, double *p, double *p0, double *vx, double *vy, double D, double dx, double dt)
{
// p ALREADY has boundary source initially.
//printf("den %0.15lf %0.15lf\n",vx[IX(49,24,N)],vy[IX(49,24,N)]);
swapArray(p0, p, N * M); // swap p_old and p_new
diffuse(N, M, p, p0, D, dx, dt); // diffuse p_old iteratively and store in p_new
swapArray(p0, p, N * M); // swap p_old and p_new
new_advect(N, M, p, p0, vx, vy, dx, dt); // advect p_old and store in p_new
add_boundary_source(N, M, p, 100); // add source to p_new
}
void write_out(int N, int M, double *p)
{
FILE *output_file = fopen("E:/Warwick/saturn/daphnis_e10.txt", "a");
for (int j = 0; j < M; j++)
{
for (int i = 0; i < N; i++)
{
fprintf(output_file, "%0.10lf ", p[IX(i, j, N)]);
}
fprintf(output_file, "\n");
}
fclose(output_file);
}
void write_out_u(int N, int M, double *p)
{
FILE *output_file = fopen("C:/C Projects/daphnis_u.txt", "a");
for (int j = 0; j < M; j++)
{
for (int i = 0; i < N; i++)
{
fprintf(output_file, "%0.10lf ", p[IX(i, j, N)]);
}
fprintf(output_file, "\n");
}
fclose(output_file);
}
void write_out_v(int N, int M, double *p)
{
FILE *output_file = fopen("C:/C Projects/daphnis_v.txt", "a");
for (int j = 0; j < M; j++)
{
for (int i = 0; i < N; i++)
{
fprintf(output_file, "%0.10lf ", p[IX(i, j, N)]);
}
fprintf(output_file, "\n");
}
fclose(output_file);
}
int main()
{
// we are using KM and KG
double G = 6.67E-20; //e-20
double saturn_mass = 5.683e26; //e26
double daphnis_mass = 7.7e10; //e13
double daphnis_radius = 136505;
double C = sqrt(G * saturn_mass);
int N = 800; // N and M are # of gridpoints, L and W (calculated) are real lengths.
int M = 100; // width, Keepler Gap (Daphnis) is 35km. N=800,M=100,L=3200
double L = 3200; // width = M * L/N
double dx = L / N; // 4km is ideal
double dt = 100;
double D = 1e-12;
double visc = 1e-12;
int i0 = N / 2;
int j0 = M / 2;
double N0 = (double)(N - 1) / 2;
double M0 = (double)(M - 1) / 2;
double *p0 = (double *)malloc(sizeof(double) * N * M);
double *p = (double *)malloc(sizeof(double) * N * M);
double *S = (double *)malloc(sizeof(double) * N * M);
double *S_vx = (double *)malloc(sizeof(double) * M);
double *S_vy = (double *)malloc(sizeof(double) * M);
double *vx = (double *)malloc(sizeof(double) * N * M);
double *vy = (double *)malloc(sizeof(double) * N * M);
double *vx0 = (double *)malloc(sizeof(double) * N * M);
double *vy0 = (double *)malloc(sizeof(double) * N * M);
double *ax = (double *)malloc(sizeof(double) * N * M);
double *ay = (double *)malloc(sizeof(double) * N * M);
double *T_p = (double *)malloc(sizeof(double) * N * M);
double v0 = sqrt(G * saturn_mass / daphnis_radius);
// INITIAL CONDITIONS
for (int j = 0; j < M; j++)
{
for (int i = 0; i < N; i++)
{
int index = IX(i, j, N);
double i1 = i - N0;
double j1 = j - M0;
double x = i1 * dx; // x-distance from Daphnis (left is neg, right is pos)
double y = j1 * dx; // y-distance from Daphnis (up is neg, down is pos)
double y1 = daphnis_radius + j1 * dx; // y-distance from Saturn (always positive, force is pointing up so need to convert to negative)
double radius = sqrt(x * x + y1 * y1); // distance to Saturn
double v = C / sqrt(radius); // velocity
double vx1 = v * (y1 / radius) - v0; // x-velocity (in frame of Daphnis, subtracting v0)
//double vy1 = v * (-1*x / radius); // y-velocity
// R3 is |r|^3
double R3 = sqrt(x * x + y * y) * sqrt(x * x + y * y) * sqrt(x * x + y * y);
//printf("%i %lf\n",j,v);
if (i == 0)
{
S_vx[j] = vx1;
S_vy[j] = 0;//vy1;
}
vx[index] = vx1;
vy[index] = 0;//vy1;
vx0[index] = 0;
vy0[index] = 0;
p0[index] = 0;
p[index] = 100;
// if ((j0 - 1 - 4 <= j) && (j <= j0 + 4))
// {
// p[index] = 0;
// }
ax[index] = -1 * G * daphnis_mass * (x) / (R3); // F_g(Daphnis) in x
ay[index] = -1 * G * daphnis_mass * (y) / (R3); // F_g(Daphnis) in y
//ax[index] += -1 * G * saturn_mass * (x) / (radius*radius*radius); // F_g(Saturn) in x
//ay[index] += -1 * G * saturn_mass * (y1) / (radius*radius*radius); // F_g(Saturn) in y
//ay[index] += 1 * G * saturn_mass / (daphnis_radius*daphnis_radius); // F_fictitious (orbitting)
//double w = sqrt(G * saturn_mass / (daphnis_radius * daphnis_radius * daphnis_radius)); // = v/r
//double c1 = 2 * w; // Coriolis
//double c2 = w * w; // Centrifugal
//ax[index] += (-1 * c1 * vy[index] + c2 * x); // Coriolis + Centrifugal in x
//ay[index] += (c1 * vx[index] + c2 * y); // Coriolis + Centrifugal in y
//printf("%i %lf %lf %0.15lf %0.15lf\n", index, vx[index], vy[index], ax[index], ay[index]);
// if ((N % 2 == 0) && (M % 2 == 0))
// {
// if ((i == i0 && j == j0) || (i == i0 - 1 && j == j0) || (i == i0 && j == j0 - 1) || (i == i0 - 1 && j == j0 - 1))
// {
// ax[index] = 0;
// ay[index] = 0;
// }
// }
// else if ((N % 2 == 0) && (M % 2 != 0))
// {
// if ((i == i0 && j == j0) || (i == i0 - 1 && j == j0))
// {
// ax[index] = 0;
// ay[index] = 0;
// }
// }
// else if ((N % 2 != 0) && (M % 2 == 0))
// {
// if ((i == i0 && j == j0) || (i == i0 && j == j0 - 1))
// {
// ax[index] = 0;
// ay[index] = 0;
// }
// }
// else
// {
// if ((i == i0 && j == j0))
// {
// ax[index] = 0;
// ay[index] = 0;
// }
// }
// a=0 at Daphnis
ax[IX(i0, j0, N)] = 0;
ax[IX(i0 - 1, j0, N)] = 0;
ax[IX(i0, j0 - 1, N)] = 0;
ax[IX(i0 - 1, j0 - 1, N)] = 0;
ay[IX(i0, j0, N)] = 0;
ay[IX(i0 - 1, j0, N)] = 0;
ay[IX(i0, j0 - 1, N)] = 0;
ay[IX(i0 - 1, j0 - 1, N)] = 0;
}
}
// NxM total gridpoints, N columns, M rows. Meaning Mx(# of cycles) = # of lines
add_boundary_source(N, M, p, 100);
write_out(N, M, p);
//write_out_u(N, M, vx);
//write_out_v(N, M, vy);
int n = 100000;
for (int k = 0; k < n; k++)
{
vel_step(N, M, vx, vy, vx0, vy0, ax, ay, S_vx, S_vy, p, T_p, visc, dx, dt);
dens_step(N, M, p, p0, vx, vy, D, dx, dt);
if ((k + 1) % 100 == 0)
{
write_out(N, M, p);
//write_out_u(N, M, vx);
//write_out_v(N, M, vy);
}
printf("%i/%i\r", k + 1, n);
}
free(p0);
free(p);
free(S);
free(vx);
free(vy);
free(vx0);
free(vy0);
free(ax);
free(ay);
free(T_p);
}