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
|
/*
Copyright 2017-2023 Ian Jauslin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "constants.cpp"
#include "io.h"
#include "navier-stokes.h"
#include "statistics.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
// compute solution as a function of time
int uk(
int K1,
int K2,
int N1,
int N2,
uint64_t nsteps,
double nu,
double delta,
double L,
_Complex double* u0,
_Complex double* g,
bool irreversible,
unsigned int algorithm,
uint64_t print_freq,
uint64_t starting_time,
unsigned int nthreads,
FILE* savefile
){
_Complex double* u;
_Complex double* tmp1;
_Complex double* tmp2;
_Complex double* tmp3;
uint64_t t;
fft_vect fft1;
fft_vect fft2;
fft_vect ifft;
int kx,ky;
ns_init_tmps(&u, &tmp1, &tmp2, &tmp3, &fft1, &fft2, &ifft, K1, K2, N1, N2, nthreads);
// copy initial condition
copy_u(u, u0, K1, K2);
// print column headers
printf("# 1:i 2:t ");
t=3;
for(kx=-K1;kx<=K1;kx++){
for (ky=-K2;ky<=K2;ky++){
printf(" %6lu:(%4d,%4d)r ",t,kx,ky);
t++;
printf(" %6lu:(%4d,%4d)i ",t,kx,ky);
t++;
}
}
// iterate
for(t=starting_time;nsteps==0 || t<starting_time+nsteps;t++){
if(algorithm==ALGORITHM_RK2){
ns_step_rk2(u, K1, K2, N1, N2, nu, delta, L, g, fft1, fft2, ifft, tmp1, tmp2, irreversible);
} else {
ns_step_rk4(u, K1, K2, N1, N2, nu, delta, L, g, fft1, fft2, ifft, tmp1, tmp2, tmp3, irreversible);
}
if(t%print_freq==0){
fprintf(stderr,"%lu % .8e ",t,t*delta);
printf("%8lu % .15e ",t,t*delta);
for(kx=-K1;kx<=K1;kx++){
for (ky=-K2;ky<=K2;ky++){
if (kx*kx+ky*ky<=1){
fprintf(stderr,"% .8e % .8e ",__real__ getval_sym(u,kx,ky,K2), __imag__ getval_sym(u, kx,ky,K2));
}
printf("% .15e % .15e ",__real__ getval_sym(u, kx,ky,K2), __imag__ getval_sym(u, kx,ky,K2));
}
}
fprintf(stderr,"\n");
printf("\n");
}
}
// save final entry to savefile
write_vec_bin(u, K1, K2, savefile);
ns_free_tmps(u, tmp1, tmp2, tmp3, fft1, fft2, ifft);
return(0);
}
// compute enstrophy, alpha as a function of time
int enstrophy(
int K1,
int K2,
int N1,
int N2,
uint64_t nsteps,
double nu,
double delta,
double L,
_Complex double* u0,
_Complex double* g,
bool irreversible,
unsigned int algorithm,
uint64_t print_freq,
uint64_t starting_time,
unsigned int nthreads,
FILE* savefile,
// for interrupt recovery
char* cmd_string,
char* params_string,
char* savefile_string
){
_Complex double* u;
_Complex double* tmp1;
_Complex double* tmp2;
_Complex double* tmp3;
double alpha, enstrophy;
double avg_a,avg_en,avg_en_x_a;
// index
uint64_t t;
fft_vect fft1;
fft_vect fft2;
fft_vect ifft;
ns_init_tmps(&u, &tmp1, &tmp2, &tmp3, &fft1, &fft2, &ifft, K1, K2, N1, N2, nthreads);
// copy initial condition
copy_u(u, u0, K1, K2);
// init running average
avg_a=0;
avg_en=0;
avg_en_x_a=0;
// special first case when starting_time is not a multiple of print_freq
uint64_t first_box = print_freq - (starting_time % print_freq);
// iterate
for(t=starting_time;nsteps==0 || t<starting_time+nsteps;t++){
if(algorithm==ALGORITHM_RK2){
ns_step_rk2(u, K1, K2, N1, N2, nu, delta, L, g, fft1, fft2, ifft, tmp1, tmp2, irreversible);
} else {
ns_step_rk4(u, K1, K2, N1, N2, nu, delta, L, g, fft1, fft2, ifft, tmp1, tmp2, tmp3, irreversible);
}
alpha=compute_alpha(u, K1, K2, g, L);
enstrophy=compute_enstrophy(u, K1, K2, L);
avg_a=average_step(alpha, avg_a, t, starting_time, print_freq, first_box);
avg_en=average_step(enstrophy, avg_en, t, starting_time, print_freq, first_box);
avg_en_x_a=average_step(enstrophy*alpha, avg_en_x_a, t, starting_time, print_freq, first_box);
if(t>starting_time && t%print_freq==0){
fprintf(stderr,"%lu % .8e % .8e % .8e % .8e % .8e % .8e % .8e\n",t,t*delta, avg_a, avg_en_x_a, avg_en, alpha, alpha*enstrophy, enstrophy);
printf("%8lu % .15e % .15e % .15e % .15e % .15e % .15e % .15e\n",t,t*delta, avg_a, avg_en_x_a, avg_en, alpha, alpha*enstrophy, enstrophy);
}
// catch abort signal
if (g_abort){
// print u to stderr if no savefile
if (savefile==NULL){
savefile=stderr;
}
break;
}
}
if(savefile!=NULL){
fprintf(savefile,"# Continue computation with\n");
// command to resume
fprintf(savefile,"#! ");
fprintf(savefile, cmd_string);
// params
// allocate buffer for params
if(params_string!=NULL) {
char* params=calloc(sizeof(char), strlen(params_string)+1);
strcpy(params, params_string);
remove_entry(params, "starting_time");
remove_entry(params, "init");
remove_entry(params, "nsteps");
fprintf(savefile," -p \"%s;starting_time=%lu;nsteps=%lu;init=file:%s\"", params, t+1, (nsteps+starting_time < t+1 ? 0 : nsteps+starting_time-t-1), savefile_string);
free(params);
}
fprintf(savefile," enstrophy\n");
// save final u to savefile
if(savefile==stderr || savefile==stdout){
write_vec(u, K1, K2, savefile);
} else {
write_vec_bin(u, K1, K2, savefile);
}
}
ns_free_tmps(u, tmp1, tmp2, tmp3, fft1, fft2, ifft);
return(0);
}
// compute solution as a function of time, but do not print anything (useful for debugging)
int quiet(
int K1,
int K2,
int N1,
int N2,
uint64_t nsteps,
double nu,
double delta,
double L,
uint64_t starting_time,
_Complex double* u0,
_Complex double* g,
bool irreversible,
unsigned int algorithm,
unsigned int nthreads,
FILE* savefile
){
_Complex double* u;
_Complex double* tmp1;
_Complex double* tmp2;
_Complex double* tmp3;
uint64_t t;
fft_vect fft1;
fft_vect fft2;
fft_vect ifft;
ns_init_tmps(&u, &tmp1, &tmp2, &tmp3, &fft1, &fft2, &ifft, K1, K2, N1, N2, nthreads);
// copy initial condition
copy_u(u, u0, K1, K2);
// iterate
for(t=starting_time;nsteps==0 || t<starting_time+nsteps;t++){
if(algorithm==ALGORITHM_RK2){
ns_step_rk2(u, K1, K2, N1, N2, nu, delta, L, g, fft1, fft2, ifft, tmp1, tmp2, irreversible);
} else {
ns_step_rk4(u, K1, K2, N1, N2, nu, delta, L, g, fft1, fft2, ifft, tmp1, tmp2, tmp3, irreversible);
}
}
// save final entry to savefile
write_vec(u, K1, K2, savefile);
ns_free_tmps(u, tmp1, tmp2, tmp3, fft1, fft2, ifft);
return(0);
}
// initialize vectors for computation
int ns_init_tmps(
_Complex double ** u,
_Complex double ** tmp1,
_Complex double ** tmp2,
_Complex double ** tmp3,
fft_vect* fft1,
fft_vect* fft2,
fft_vect* ifft,
int K1,
int K2,
int N1,
int N2,
unsigned int nthreads
){
// velocity field
*u=calloc(sizeof(_Complex double),K1*(2*K2+1)+K2);
// allocate tmp vectors for computation
*tmp1=calloc(sizeof(_Complex double),K1*(2*K2+1)+K2);
*tmp2=calloc(sizeof(_Complex double),K1*(2*K2+1)+K2);
*tmp3=calloc(sizeof(_Complex double),K1*(2*K2+1)+K2);
// init threads
fftw_init_threads();
fftw_plan_with_nthreads(nthreads);
// prepare vectors for fft
fft1->fft=fftw_malloc(sizeof(fftw_complex)*N1*N2);
fft1->fft_plan=fftw_plan_dft_2d(N1,N2, fft1->fft, fft1->fft, FFTW_FORWARD, FFTW_MEASURE);
fft2->fft=fftw_malloc(sizeof(fftw_complex)*N1*N2);
fft2->fft_plan=fftw_plan_dft_2d(N1,N2, fft2->fft, fft2->fft, FFTW_FORWARD, FFTW_MEASURE);
ifft->fft=fftw_malloc(sizeof(fftw_complex)*N1*N2);
ifft->fft_plan=fftw_plan_dft_2d(N1,N2, ifft->fft, ifft->fft, FFTW_BACKWARD, FFTW_MEASURE);
return 0;
}
// release vectors
int ns_free_tmps(
_Complex double* u,
_Complex double* tmp1,
_Complex double* tmp2,
_Complex double* tmp3,
fft_vect fft1,
fft_vect fft2,
fft_vect ifft
){
// free memory
fftw_destroy_plan(fft1.fft_plan);
fftw_destroy_plan(fft2.fft_plan);
fftw_destroy_plan(ifft.fft_plan);
fftw_free(fft1.fft);
fftw_free(fft2.fft);
fftw_free(ifft.fft);
fftw_cleanup_threads();
free(tmp3);
free(tmp2);
free(tmp1);
free(u);
return 0;
}
// copy u0 to u
int copy_u(
_Complex double* u,
_Complex double* u0,
int K1,
int K2
){
int i;
for(i=0;i<K1*(2*K2+1)+K2;i++){
u[i]=u0[i];
}
return 0;
}
// next time step
// RK 4 algorithm
int ns_step_rk4(
_Complex double* u,
int K1,
int K2,
int N1,
int N2,
double nu,
double delta,
double L,
_Complex double* g,
fft_vect fft1,
fft_vect fft2,
fft_vect ifft,
_Complex double* tmp1,
_Complex double* tmp2,
_Complex double* tmp3,
bool irreversible
){
int kx,ky;
// k1
ns_rhs(tmp1, u, K1, K2, N1, N2, nu, L, g, fft1, fft2, ifft, irreversible);
// add to output
for(kx=0;kx<=K1;kx++){
for(ky=(kx>0 ? -K2 : 1);ky<=K2;ky++){
tmp3[klookup_sym(kx,ky,K2)]=u[klookup_sym(kx,ky,K2)]+delta/6*tmp1[klookup_sym(kx,ky,K2)];
}
}
// u+h*k1/2
for(kx=0;kx<=K1;kx++){
for(ky=(kx>0 ? -K2 : 1);ky<=K2;ky++){
tmp2[klookup_sym(kx,ky,K2)]=u[klookup_sym(kx,ky,K2)]+delta/2*tmp1[klookup_sym(kx,ky,K2)];
}
}
// k2
ns_rhs(tmp1, tmp2, K1, K2, N1, N2, nu, L, g, fft1, fft2, ifft, irreversible);
// add to output
for(kx=0;kx<=K1;kx++){
for(ky=(kx>0 ? -K2 : 1);ky<=K2;ky++){
tmp3[klookup_sym(kx,ky,K2)]+=delta/3*tmp1[klookup_sym(kx,ky,K2)];
}
}
// u+h*k2/2
for(kx=0;kx<=K1;kx++){
for(ky=(kx>0 ? -K2 : 1);ky<=K2;ky++){
tmp2[klookup_sym(kx,ky,K2)]=u[klookup_sym(kx,ky,K2)]+delta/2*tmp1[klookup_sym(kx,ky,K2)];
}
}
// k3
ns_rhs(tmp1, tmp2, K1, K2, N1, N2, nu, L, g, fft1, fft2, ifft, irreversible);
// add to output
for(kx=0;kx<=K1;kx++){
for(ky=(kx>0 ? -K2 : 1);ky<=K2;ky++){
tmp3[klookup_sym(kx,ky,K2)]+=delta/3*tmp1[klookup_sym(kx,ky,K2)];
}
}
// u+h*k3
for(kx=0;kx<=K1;kx++){
for(ky=(kx>0 ? -K2 : 1);ky<=K2;ky++){
tmp2[klookup_sym(kx,ky,K2)]=u[klookup_sym(kx,ky,K2)]+delta*tmp1[klookup_sym(kx,ky,K2)];
}
}
// k4
ns_rhs(tmp1, tmp2, K1, K2, N1, N2, nu, L, g, fft1, fft2, ifft, irreversible);
// add to output
for(kx=0;kx<=K1;kx++){
for(ky=(kx>0 ? -K2 : 1);ky<=K2;ky++){
u[klookup_sym(kx,ky,K2)]=tmp3[klookup_sym(kx,ky,K2)]+delta/6*tmp1[klookup_sym(kx,ky,K2)];
}
}
return(0);
}
// RK 2 algorithm
int ns_step_rk2(
_Complex double* u,
int K1,
int K2,
int N1,
int N2,
double nu,
double delta,
double L,
_Complex double* g,
fft_vect fft1,
fft_vect fft2,
fft_vect ifft,
_Complex double* tmp1,
_Complex double* tmp2,
bool irreversible
){
int kx,ky;
// k1
ns_rhs(tmp1, u, K1, K2, N1, N2, nu, L, g, fft1, fft2, ifft, irreversible);
// u+h*k1/2
for(kx=0;kx<=K1;kx++){
for(ky=(kx>0 ? -K2 : 1);ky<=K2;ky++){
tmp2[klookup_sym(kx,ky,K2)]=u[klookup_sym(kx,ky,K2)]+delta/2*tmp1[klookup_sym(kx,ky,K2)];
}
}
// k2
ns_rhs(tmp1, tmp2, K1, K2, N1, N2, nu, L, g, fft1, fft2, ifft, irreversible);
// add to output
for(kx=0;kx<=K1;kx++){
for(ky=(kx>0 ? -K2 : 1);ky<=K2;ky++){
u[klookup_sym(kx,ky,K2)]+=delta*tmp1[klookup_sym(kx,ky,K2)];
}
}
return(0);
}
// right side of Irreversible/Reversible Navier-Stokes equation
int ns_rhs(
_Complex double* out,
_Complex double* u,
int K1,
int K2,
int N1,
int N2,
double nu,
double L,
_Complex double* g,
fft_vect fft1,
fft_vect fft2,
fft_vect ifft,
bool irreversible
){
int kx,ky;
int i;
double alpha;
// compute convolution term
ns_T(u,K1,K2,N1,N2,fft1,fft2,ifft);
if (irreversible) {
alpha=nu;
} else {
alpha=compute_alpha(u,K1,K2,g,L);
}
for(i=0; i<K1*(2*K2+1)+K2; i++){
out[i]=0;
}
for(kx=0;kx<=K1;kx++){
for(ky=(kx>0 ? -K2 : 1);ky<=K2;ky++){
out[klookup_sym(kx,ky,K2)]=-4*M_PI*M_PI/L/L*alpha*(kx*kx+ky*ky)*u[klookup_sym(kx,ky,K2)]+g[klookup_sym(kx,ky,K2)]+4*M_PI*M_PI/L/L/sqrt(kx*kx+ky*ky)*ifft.fft[klookup(kx,ky,N1,N2)];
}
}
return(0);
}
// convolution term in right side of convolution equation
int ns_T(
_Complex double* u,
int K1,
int K2,
int N1,
int N2,
fft_vect fft1,
fft_vect fft2,
fft_vect ifft
){
int kx,ky;
int i;
// F(px/|p|*u)*F(qy*|q|*u)
// init to 0
for(i=0; i<N1*N2; i++){
fft1.fft[i]=0;
fft2.fft[i]=0;
ifft.fft[i]=0;
}
// fill modes
for(kx=-K1;kx<=K1;kx++){
for(ky=-K2;ky<=K2;ky++){
if(kx!=0 || ky!=0){
fft1.fft[klookup(kx,ky,N1,N2)]=kx/sqrt(kx*kx+ky*ky)*getval_sym(u, kx,ky,K2)/N1;
fft2.fft[klookup(kx,ky,N1,N2)]=ky*sqrt(kx*kx+ky*ky)*getval_sym(u, kx,ky,K2)/N2;
}
}
}
// fft
fftw_execute(fft1.fft_plan);
fftw_execute(fft2.fft_plan);
// write to ifft
for(i=0;i<N1*N2;i++){
ifft.fft[i]=fft1.fft[i]*fft2.fft[i];
}
// F(py/|p|*u)*F(qx*|q|*u)
// init to 0
for(i=0; i<N1*N2; i++){
fft1.fft[i]=0;
fft2.fft[i]=0;
}
// fill modes
for(kx=-K1;kx<=K1;kx++){
for(ky=-K2;ky<=K2;ky++){
if(kx!=0 || ky!=0){
fft1.fft[klookup(kx,ky,N1,N2)]=ky/sqrt(kx*kx+ky*ky)*getval_sym(u, kx,ky,K2)/N1;
fft2.fft[klookup(kx,ky,N1,N2)]=kx*sqrt(kx*kx+ky*ky)*getval_sym(u, kx,ky,K2)/N2;
}
}
}
// fft
fftw_execute(fft1.fft_plan);
fftw_execute(fft2.fft_plan);
// write to ifft
for(i=0;i<N1*N2;i++){
ifft.fft[i]=ifft.fft[i]-fft1.fft[i]*fft2.fft[i];
}
// inverse fft
fftw_execute(ifft.fft_plan);
return(0);
}
// convolution term in right side of convolution equation, computed without fourier transform
int ns_T_nofft(
_Complex double* out,
_Complex double* u,
int K1,
int K2,
int N1,
int N2
){
int kx,ky;
int px,py;
int qx,qy;
// loop over K's (needs N1>=2*K1+1 and N2>=2*K2+1)
if (N1<2*K1+1 || N2<2*K2+1){
fprintf(stderr,"error: N1 and N2 need t be >= 2*K1+1 and 2*K2+1 respectively\n");
return(-1);
}
for(kx=-K1;kx<=K1;kx++){
for(ky=-K2;ky<=K2;ky++){
// init
out[klookup(kx,ky,N1,N2)]=0.;
for(px=-K1;px<=K1;px++){
for(py=-K2;py<=K2;py++){
qx=kx-px;
qy=ky-py;
// cutoff in q
if(qx>=-K1 && qx<=K1 && qy>=-K2 && qy<=K2 && qx*qx+qy*qy>0 && px*px+py*py>0){
out[klookup(kx,ky,N1,N2)]+=(-qx*py+qy*px)*sqrt(qx*qx+qy*qy)/sqrt(px*px+py*py)*getval_sym(u, px,py,K2)*getval_sym(u, qx,qy,K2);
}
}
}
}
}
return 0;
}
// compute alpha
double compute_alpha(
_Complex double* u,
int K1,
int K2,
_Complex double* g,
double L
){
_Complex double num=0;
double denom=0;
int kx,ky;
num=0.;
denom=0.;
for(kx=0;kx<=K1;kx++){
for(ky=(kx>0 ? -K2 : 1);ky<=K2;ky++){
num+=L*L/4/M_PI/M_PI*(kx*kx+ky*ky)*getval_sym(g, kx,ky,K2)*conj(getval_sym(u, kx,ky,K2));
denom+=__real__ (kx*kx+ky*ky)*(kx*kx+ky*ky)*getval_sym(u, kx,ky,K2)*conj(getval_sym(u, kx,ky,K2));
}
}
return __real__ num/denom;
}
// compute energy
double compute_energy(
_Complex double* u,
int K1,
int K2
){
int kx,ky;
double out=0.;
for(kx=0;kx<=K1;kx++){
for(ky=(kx>0 ? -K2 : 1);ky<=K2;ky++){
out+=__real__ (getval_sym(u, kx,ky,K2)*conj(getval_sym(u, kx,ky,K2)));
}
}
return 2*out;
}
// compute enstrophy
double compute_enstrophy(
_Complex double* u,
int K1,
int K2,
double L
){
int kx,ky;
double out=0.;
for(kx=0;kx<=K1;kx++){
for(ky=(kx>0 ? -K2 : 1);ky<=K2;ky++){
out+=__real__ (4*M_PI*M_PI/L/L*(kx*kx+ky*ky)*getval_sym(u, kx,ky,K2)*conj(getval_sym(u, kx,ky,K2)));
}
}
return 2*out;
}
// get index for kx,ky in array of size S
int klookup(
int kx,
int ky,
int S1,
int S2
){
return (kx>=0 ? kx : S1+kx)*S2 + (ky>=0 ? ky : S2+ky);
}
// get index for kx,ky in array of size K1,K2 in which only the terms with kx>=0 and if kx=0, ky>0 are stored
int klookup_sym(
int kx,
int ky,
int K2
){
if (kx<0) {
fprintf(stderr, "bug!: attempting to access a symmetrized vector at kx<0\n Contact Ian at ian.jauslin@rutgers.edu!\n");
exit(-1);
}
if (kx==0) {
if (ky<=0){
fprintf(stderr, "bug!: attempting to access a symmetrized vector at kx=0 and ky<=0\n Contact Ian at ian.jauslin@rutgers.edu!\n");
exit(-1);
}
return ky-1;
}
return K2+(kx-1)*(2*K2+1) + (ky>=0 ? ky : (2*K2+1)+ky);
}
// get u_{kx,ky} from a vector u in which only the values for kx>=0 are stored, assuming u_{-k}=u_k^*
_Complex double getval_sym(
_Complex double* u,
int kx,
int ky,
int K2
){
if(kx>0 || (kx==0 && ky>0)){
return u[klookup_sym(kx,ky,K2)];
}
else if(kx==0 && ky==0){
return 0;
} else {
return conj(u[klookup_sym(-kx,-ky,K2)]);
}
}
|