-
Notifications
You must be signed in to change notification settings - Fork 626
/
meepgeom.cpp
3050 lines (2701 loc) · 116 KB
/
meepgeom.cpp
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2005-2024 Massachusetts Institute of Technology
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2, or (at your option)
% any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details. %
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software Foundation,
% Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <algorithm>
#include <vector>
#include "meepgeom.hpp"
#include "meep_internals.hpp"
namespace meep_geom {
#define master_printf meep::master_printf
/***************************************************************/
/* global variables for default material */
/***************************************************************/
material_data vacuum_material_data;
material_type vacuum = &vacuum_material_data;
void set_default_material(material_type _default_material) {
if (default_material != NULL) {
if (default_material == _default_material) return;
material_free((material_type)default_material);
default_material = NULL;
}
if (_default_material != NULL) {
material_type new_material = new material_data();
new_material->copy_from(*_default_material);
default_material = (void *)new_material;
}
}
void unset_default_material(void) {
if (default_material != NULL) {
material_free((material_type)default_material);
default_material = NULL;
}
}
bool susceptibility_equal(const susceptibility &s1, const susceptibility &s2) {
return (vector3_equal(s1.sigma_diag, s2.sigma_diag) &&
vector3_equal(s1.sigma_offdiag, s2.sigma_offdiag) && vector3_equal(s1.bias, s2.bias) &&
s1.frequency == s2.frequency && s1.gamma == s2.gamma && s1.alpha == s2.alpha &&
s1.noise_amp == s2.noise_amp && s1.drude == s2.drude &&
s1.saturated_gyrotropy == s2.saturated_gyrotropy && s1.is_file == s2.is_file);
}
bool susceptibility_list_equal(const susceptibility_list &s1, const susceptibility_list &s2) {
if (s1.size() != s2.size()) return false;
for (size_t i = 0; i < s1.size(); ++i) {
if (!susceptibility_equal(s1[i], s2[i])) return false;
}
return true;
}
bool medium_struct_equal(const medium_struct *m1, const medium_struct *m2) {
return (vector3_equal(m1->epsilon_diag, m2->epsilon_diag) &&
cvector3_equal(m1->epsilon_offdiag, m2->epsilon_offdiag) &&
vector3_equal(m1->mu_diag, m2->mu_diag) &&
cvector3_equal(m1->mu_offdiag, m2->mu_offdiag) &&
vector3_equal(m1->E_chi2_diag, m2->E_chi2_diag) &&
vector3_equal(m1->E_chi3_diag, m2->E_chi3_diag) &&
vector3_equal(m1->H_chi2_diag, m2->H_chi2_diag) &&
vector3_equal(m1->D_conductivity_diag, m2->D_conductivity_diag) &&
vector3_equal(m1->B_conductivity_diag, m2->B_conductivity_diag) &&
susceptibility_list_equal(m1->E_susceptibilities, m2->E_susceptibilities) &&
susceptibility_list_equal(m1->H_susceptibilities, m2->H_susceptibilities));
}
bool material_grid_equal(const material_data *m1, const material_data *m2) {
// a rigorous method for comapring material grids
int n1, n2;
n1 = m1->grid_size.x * m1->grid_size.y * m1->grid_size.z;
n2 = m2->grid_size.x * m2->grid_size.y * m2->grid_size.z;
if (n1 != n2) return false;
for (int i = 0; i < n1; i++)
if (m1->epsilon_data[i] != m2->epsilon_data[i]) return false;
return (medium_struct_equal(&(m1->medium), &(m2->medium)) &&
medium_struct_equal(&(m1->medium_1), &(m2->medium_1)) &&
medium_struct_equal(&(m1->medium_2), &(m2->medium_2)));
}
// garbage collection for material structures: called to deallocate memory
// allocated for susceptibilities in user-defined materials.
// TODO
void material_gc(material_type m) {
if (!m || m->which_subclass != material_data::MATERIAL_USER) return;
m->medium.E_susceptibilities.clear();
m->medium.H_susceptibilities.clear();
m->medium_1.E_susceptibilities.clear();
m->medium_1.H_susceptibilities.clear();
m->medium_2.E_susceptibilities.clear();
m->medium_2.H_susceptibilities.clear();
}
void material_free(material_type m) {
if (!m) return;
m->medium.E_susceptibilities.clear();
m->medium.H_susceptibilities.clear();
m->medium_1.E_susceptibilities.clear();
m->medium_1.H_susceptibilities.clear();
m->medium_2.E_susceptibilities.clear();
m->medium_2.H_susceptibilities.clear();
// NOTE: We do not delete the user_data field here since it is an opaque/void
// object so will assume that the caller keeps track of its lifetime.
delete[] m->epsilon_data;
m->epsilon_data = NULL;
delete[] m->weights;
m->weights = NULL;
delete m;
}
bool material_type_equal(const material_type m1, const material_type m2) {
if (m1 == m2) return true;
if (m1->which_subclass != m2->which_subclass) return false;
switch (m1->which_subclass) {
case material_data::MATERIAL_FILE:
case material_data::PERFECT_METAL: return true;
case material_data::MATERIAL_USER:
return m1->user_func == m2->user_func && m1->user_data == m2->user_data;
case material_data::MATERIAL_GRID:
case material_data::MEDIUM: return medium_struct_equal(&(m1->medium), &(m2->medium));
default: return false;
}
}
/***************************************************************/
/***************************************************************/
/***************************************************************/
/* rotate A by a unitary (real) rotation matrix R:
RAR = transpose(R) * A * R
*/
void sym_matrix_rotate(symm_matrix *RAR, const symm_matrix *A_, const double R[3][3]) {
int i, j;
double A[3][3], AR[3][3];
A[0][0] = A_->m00;
A[1][1] = A_->m11;
A[2][2] = A_->m22;
A[0][1] = A[1][0] = A_->m01;
A[0][2] = A[2][0] = A_->m02;
A[1][2] = A[2][1] = A_->m12;
for (i = 0; i < 3; ++i)
for (j = 0; j < 3; ++j)
AR[i][j] = A[i][0] * R[0][j] + A[i][1] * R[1][j] + A[i][2] * R[2][j];
for (i = 0; i < 3; ++i)
for (j = i; j < 3; ++j)
A[i][j] = R[0][i] * AR[0][j] + R[1][i] * AR[1][j] + R[2][i] * AR[2][j];
RAR->m00 = A[0][0];
RAR->m11 = A[1][1];
RAR->m22 = A[2][2];
RAR->m01 = A[0][1];
RAR->m02 = A[0][2];
RAR->m12 = A[1][2];
}
/* Set Vinv = inverse of V, where both V and Vinv are real-symmetric matrices.*/
void sym_matrix_invert(symm_matrix *Vinv, const symm_matrix *V) {
double m00 = V->m00, m11 = V->m11, m22 = V->m22;
double m01 = V->m01, m02 = V->m02, m12 = V->m12;
if (m01 == 0.0 && m02 == 0.0 && m12 == 0.0) {
/* optimize common case of a diagonal matrix: */
Vinv->m00 = 1.0 / m00;
Vinv->m11 = 1.0 / m11;
Vinv->m22 = 1.0 / m22;
Vinv->m01 = Vinv->m02 = Vinv->m12 = 0.0;
}
else {
double detinv;
/* compute the determinant: */
detinv = m00 * m11 * m22 - m02 * m11 * m02 + 2.0 * m01 * m12 * m02 - m01 * m01 * m22 -
m12 * m12 * m00;
if (detinv == 0.0) meep::abort("singular 3x3 matrix");
detinv = 1.0 / detinv;
Vinv->m00 = detinv * (m11 * m22 - m12 * m12);
Vinv->m11 = detinv * (m00 * m22 - m02 * m02);
Vinv->m22 = detinv * (m11 * m00 - m01 * m01);
Vinv->m02 = detinv * (m01 * m12 - m11 * m02);
Vinv->m01 = detinv * (m12 * m02 - m01 * m22);
Vinv->m12 = detinv * (m01 * m02 - m00 * m12);
}
}
/* Returns whether or not V is positive-definite. */
int sym_matrix_positive_definite(symm_matrix *V) {
double det2, det3;
double m00 = V->m00, m11 = V->m11, m22 = V->m22;
#if defined(WITH_HERMITIAN_EPSILON)
scalar_complex m01 = V->m01, m02 = V->m02, m12 = V->m12;
det2 = m00 * m11 - CSCALAR_NORMSQR(m01);
det3 = det2 * m22 - m11 * CSCALAR_NORMSQR(m02) - CSCALAR_NORMSQR(m12) * m00 +
2.0 * ((m01.re * m12.re - m01.im * m12.im) * m02.re +
(m01.re * m12.im + m01.im * m12.re) * m02.im);
#else /* real matrix */
double m01 = V->m01, m02 = V->m02, m12 = V->m12;
det2 = m00 * m11 - m01 * m01;
det3 = det2 * m22 - m02 * m11 * m02 + 2.0 * m01 * m12 * m02 - m12 * m12 * m00;
#endif /* real matrix */
return (m00 > 0.0 && det2 > 0.0 && det3 > 0.0);
}
/***************************************************************/
/***************************************************************/
/***************************************************************/
static meep::ndim dim = meep::D3;
void set_dimensions(int dims) {
if (dims == CYLINDRICAL) { dim = meep::Dcyl; }
else { dim = meep::ndim(dims - 1); }
}
vector3 vec_to_vector3(const meep::vec &pt) {
vector3 v3;
switch (pt.dim) {
case meep::D1:
v3.x = 0;
v3.y = 0;
v3.z = pt.z();
break;
case meep::D2:
v3.x = pt.x();
v3.y = pt.y();
v3.z = 0;
break;
case meep::D3:
v3.x = pt.x();
v3.y = pt.y();
v3.z = pt.z();
break;
case meep::Dcyl:
v3.x = pt.r();
v3.y = 0;
v3.z = pt.z();
break;
}
return v3;
}
meep::vec vector3_to_vec(const vector3 v3) {
switch (dim) {
case meep::D1: return meep::vec(v3.z);
case meep::D2: return meep::vec(v3.x, v3.y);
case meep::D3: return meep::vec(v3.x, v3.y, v3.z);
case meep::Dcyl: return meep::veccyl(v3.x, v3.z);
default: meep::abort("unknown dimensionality in vector3_to_vec");
}
}
geom_box gv2box(const meep::volume &v) {
geom_box box;
box.low = vec_to_vector3(v.get_min_corner());
box.high = vec_to_vector3(v.get_max_corner());
return box;
}
bool is_material_grid(material_type mt) {
return (mt->which_subclass == material_data::MATERIAL_GRID);
}
bool is_material_grid(void *md) { return is_material_grid((material_type)md); }
bool is_variable(material_type mt) {
return (mt->which_subclass == material_data::MATERIAL_USER) ||
(mt->which_subclass == material_data::MATERIAL_GRID);
}
bool is_variable(void *md) { return is_variable((material_type)md); }
bool is_file(material_type md) { return (md->which_subclass == material_data::MATERIAL_FILE); }
bool is_file(void *md) { return is_file((material_type)md); }
bool is_medium(material_type md, medium_struct **m) {
if (md->which_subclass == material_data::MEDIUM) {
*m = &(md->medium);
return true;
};
return false;
}
bool is_medium(void *md, medium_struct **m) { return is_medium((material_type)md, m); }
bool is_metal(meep::field_type ft, const material_type *material) {
material_data *md = *material;
if (ft == meep::E_stuff) switch (md->which_subclass) {
case material_data::MEDIUM:
case material_data::MATERIAL_GRID:
return (md->medium.epsilon_diag.x < 0 || md->medium.epsilon_diag.y < 0 ||
md->medium.epsilon_diag.z < 0);
case material_data::PERFECT_METAL: return true;
default: meep::abort("unknown material type"); return false;
}
else
switch (md->which_subclass) {
case material_data::MEDIUM:
case material_data::MATERIAL_GRID:
return (md->medium.mu_diag.x < 0 || md->medium.mu_diag.y < 0 || md->medium.mu_diag.z < 0);
case material_data::PERFECT_METAL:
return false; // is an electric conductor, but not a magnetic conductor
default: meep::abort("unknown material type"); return false;
}
}
bool has_offdiag(const medium_struct *material) {
if ((material->epsilon_offdiag.x.re != 0) || /* account for offdiagonal components */
(material->epsilon_offdiag.y.re != 0) || (material->epsilon_offdiag.z.re != 0) ||
(material->epsilon_offdiag.x.im != 0) || (material->epsilon_offdiag.y.im != 0) ||
(material->epsilon_offdiag.z.im != 0))
return true;
else
return false;
}
// computes the vector-Jacobian product of the gradient of the matgrid_val function v
// with the Jacobian of the to_geom_box_coords function for geometric_object o
vector3 to_geom_object_coords_VJP(vector3 v, const geometric_object *o) {
if (!o) { meep::abort("must pass a geometric_object to to_geom_object_coords_VJP.\n"); }
switch (o->which_subclass) {
default: {
vector3 po = {0, 0, 0};
return po;
}
case geometric_object::SPHERE: {
number radius = o->subclass.sphere_data->radius;
return vector3_scale(0.5 / radius, v);
}
/* case geometric_object::CYLINDER:
NOT YET IMPLEMENTED */
case geometric_object::BLOCK: {
vector3 size = o->subclass.block_data->size;
if (size.x != 0.0) v.x /= size.x;
if (size.y != 0.0) v.y /= size.y;
if (size.z != 0.0) v.z /= size.z;
return matrix3x3_transpose_vector3_mult(o->subclass.block_data->projection_matrix, v);
}
/* case geometric_object::PRISM:
NOT YET IMPLEMENTED */
}
}
meep::vec material_grid_grad(vector3 p, material_data *md, const geometric_object *o) {
if (!is_material_grid(md)) { meep::abort("Invalid material grid detected.\n"); }
meep::vec gradient(zero_vec(dim));
double *data = md->weights;
int nx = md->grid_size.x;
int ny = md->grid_size.y;
int nz = md->grid_size.z;
double rx = p.x;
double ry = p.y;
double rz = p.z;
int stride = 1;
int x1, y1, z1, x2, y2, z2;
double dx, dy, dz;
bool signflip_dx = false, signflip_dy = false, signflip_dz = false;
meep::map_coordinates(rx, ry, rz, nx, ny, nz, x1, y1, z1, x2, y2, z2, dx, dy, dz,
false /* do_fabs */);
if (dx != fabs(dx)) {
dx = fabs(dx);
signflip_dx = true;
}
if (dy != fabs(dy)) {
dy = fabs(dy);
signflip_dy = true;
}
if (dz != fabs(dz)) {
dz = fabs(dz);
signflip_dz = true;
}
/* define a macro to give us data(x,y,z) on the grid,
in row-major order: */
#define D(x, y, z) (data[(((x)*ny + (y)) * nz + (z)) * stride])
double du_dx =
(signflip_dx ? -1.0 : 1.0) *
(((-D(x1, y1, z1) + D(x2, y1, z1)) * (1.0 - dy) + (-D(x1, y2, z1) + D(x2, y2, z1)) * dy) *
(1.0 - dz) +
((-D(x1, y1, z2) + D(x2, y1, z2)) * (1.0 - dy) + (-D(x1, y2, z2) + D(x2, y2, z2)) * dy) *
dz);
double du_dy = (signflip_dy ? -1.0 : 1.0) * ((-(D(x1, y1, z1) * (1.0 - dx) + D(x2, y1, z1) * dx) +
(D(x1, y2, z1) * (1.0 - dx) + D(x2, y2, z1) * dx)) *
(1.0 - dz) +
(-(D(x1, y1, z2) * (1.0 - dx) + D(x2, y1, z2) * dx) +
(D(x1, y2, z2) * (1.0 - dx) + D(x2, y2, z2) * dx)) *
dz);
double du_dz = (signflip_dz ? -1.0 : 1.0) *
(-((D(x1, y1, z1) * (1.0 - dx) + D(x2, y1, z1) * dx) * (1.0 - dy) +
(D(x1, y2, z1) * (1.0 - dx) + D(x2, y2, z1) * dx) * dy) +
((D(x1, y1, z2) * (1.0 - dx) + D(x2, y1, z2) * dx) * (1.0 - dy) +
(D(x1, y2, z2) * (1.0 - dx) + D(x2, y2, z2) * dx) * dy));
#undef D
// [du_dx,du_dy,du_dz] is the gradient ∇u with respect to the transformed coordinate
// r1 of the matgrid_val function but what we want is the gradient of u(g(r2)) with
// respect to r2 where g(r2) is the to_geom_object_coords function (in libctl/utils/geom.c).
// computing this quantity involves using the chain rule and thus the vector-Jacobian product
// ∇u J where J is the Jacobian matrix of g.
vector3 grad_u;
grad_u.x = du_dx * nx;
grad_u.y = du_dy * ny;
grad_u.z = du_dz * nz;
if (o != NULL) {
vector3 grad_u_J = to_geom_object_coords_VJP(grad_u, o);
gradient.set_direction(meep::X, grad_u_J.x);
gradient.set_direction(meep::Y, grad_u_J.y);
gradient.set_direction(meep::Z, grad_u_J.z);
}
else {
gradient.set_direction(meep::X,
geometry_lattice.size.x == 0 ? 0 : grad_u.x / geometry_lattice.size.x);
gradient.set_direction(meep::Y,
geometry_lattice.size.y == 0 ? 0 : grad_u.y / geometry_lattice.size.y);
gradient.set_direction(meep::Z,
geometry_lattice.size.z == 0 ? 0 : grad_u.z / geometry_lattice.size.z);
}
return gradient;
}
void map_lattice_coordinates(double &px, double &py, double &pz) {
px = geometry_lattice.size.x == 0 ? 0 : 0.5 + (px - geometry_center.x) / geometry_lattice.size.x;
py = geometry_lattice.size.y == 0 ? 0 : 0.5 + (py - geometry_center.y) / geometry_lattice.size.y;
pz = geometry_lattice.size.z == 0 ? 0 : 0.5 + (pz - geometry_center.z) / geometry_lattice.size.z;
}
meep::vec matgrid_grad(vector3 p, geom_box_tree tp, int oi, material_data *md) {
if (md->material_grid_kinds == material_data::U_MIN ||
md->material_grid_kinds == material_data::U_PROD)
meep::abort("%s:%i:matgrid_grad does not support overlapping grids with U_MIN or U_PROD\n",
__FILE__, __LINE__);
meep::vec gradient(zero_vec(dim));
int matgrid_val_count = 0;
// iterate through object tree at current point
if (tp) {
do {
gradient +=
material_grid_grad(to_geom_box_coords(p, &tp->objects[oi]),
(material_data *)tp->objects[oi].o->material, tp->objects[oi].o);
if (md->material_grid_kinds == material_data::U_DEFAULT) break;
++matgrid_val_count;
tp = geom_tree_search_next(p, tp, &oi);
} while (tp && is_material_grid((material_data *)tp->objects[oi].o->material));
}
// perhaps there is no object tree and the default material is a material grid
if (!tp && is_material_grid(default_material)) {
map_lattice_coordinates(p.x, p.y, p.z);
gradient =
material_grid_grad(p, (material_data *)default_material, NULL /* geometric_object *o */);
++matgrid_val_count;
}
if (md->material_grid_kinds == material_data::U_MEAN)
gradient = gradient * 1.0 / matgrid_val_count;
return gradient;
}
double material_grid_val(vector3 p, material_data *md) {
// given the relative location, p, interpolate the material grid point.
if (!is_material_grid(md)) { meep::abort("Invalid material grid detected.\n"); }
return meep::linear_interpolate(p.x, p.y, p.z, md->weights, md->grid_size.x, md->grid_size.y,
md->grid_size.z, 1);
}
static double tanh_projection(double u, double beta, double eta) {
if (beta == 0) return u;
if (u == eta) return 0.5; // avoid NaN when beta is Inf
double tanh_beta_eta = tanh(beta * eta);
return (tanh_beta_eta + tanh(beta * (u - eta))) / (tanh_beta_eta + tanh(beta * (1 - eta)));
}
double matgrid_val(vector3 p, geom_box_tree tp, int oi, material_data *md) {
double uprod = 1.0, umin = 1.0, usum = 0.0, udefault = 0.0, u;
int matgrid_val_count = 0;
// iterate through object tree at current point
if (tp) {
do {
u = material_grid_val(to_geom_box_coords(p, &tp->objects[oi]),
(material_data *)tp->objects[oi].o->material);
if (md->material_grid_kinds == material_data::U_DEFAULT) {
udefault = u;
break;
}
if (u < umin) umin = u;
uprod *= u;
usum += u;
++matgrid_val_count;
tp = geom_tree_search_next(p, tp, &oi);
} while (tp && is_material_grid((material_data *)tp->objects[oi].o->material));
}
// perhaps there is no object tree and the default material is a material grid
if (!tp && is_material_grid(default_material)) {
map_lattice_coordinates(p.x, p.y, p.z);
u = material_grid_val(p, (material_data *)default_material);
if (matgrid_val_count == 0) udefault = u;
if (u < umin) umin = u;
uprod *= u;
usum += u;
++matgrid_val_count;
}
return (md->material_grid_kinds == material_data::U_MIN
? umin
: (md->material_grid_kinds == material_data::U_PROD
? uprod
: (md->material_grid_kinds == material_data::U_MEAN ? usum / matgrid_val_count
: udefault)));
}
static void cinterp_tensors(vector3 diag_in_1, cvector3 offdiag_in_1, vector3 diag_in_2,
cvector3 offdiag_in_2, vector3 *diag_out, cvector3 *offdiag_out,
double u) {
/* convenience routine to interpolate material tensors with real and imaginary components */
diag_out->x = diag_in_1.x + u * (diag_in_2.x - diag_in_1.x);
diag_out->y = diag_in_1.y + u * (diag_in_2.y - diag_in_1.y);
diag_out->z = diag_in_1.z + u * (diag_in_2.z - diag_in_1.z);
offdiag_out->x.re = offdiag_in_1.x.re + u * (offdiag_in_2.x.re - offdiag_in_1.x.re);
offdiag_out->x.im = offdiag_in_1.x.im + u * (offdiag_in_2.x.im - offdiag_in_1.x.im);
offdiag_out->y.re = offdiag_in_1.y.re + u * (offdiag_in_2.y.re - offdiag_in_1.y.re);
offdiag_out->y.im = offdiag_in_1.y.im + u * (offdiag_in_2.y.im - offdiag_in_1.y.im);
offdiag_out->z.re = offdiag_in_1.z.re + u * (offdiag_in_2.z.re - offdiag_in_1.z.re);
offdiag_out->z.im = offdiag_in_1.z.im + u * (offdiag_in_2.z.im - offdiag_in_1.z.im);
}
static void interp_tensors(vector3 diag_in_1, vector3 offdiag_in_1, vector3 diag_in_2,
vector3 offdiag_in_2, vector3 *diag_out, vector3 *offdiag_out,
double u) {
/* convenience routine to interpolate material tensors with all real components */
diag_out->x = diag_in_1.x + u * (diag_in_2.x - diag_in_1.x);
diag_out->y = diag_in_1.y + u * (diag_in_2.y - diag_in_1.y);
diag_out->z = diag_in_1.z + u * (diag_in_2.z - diag_in_1.z);
offdiag_out->x = offdiag_in_1.x + u * (offdiag_in_2.x - offdiag_in_1.x);
offdiag_out->y = offdiag_in_1.y + u * (offdiag_in_2.y - offdiag_in_1.y);
offdiag_out->z = offdiag_in_1.z + u * (offdiag_in_2.z - offdiag_in_1.z);
}
// return material of the point p from the material grid
void epsilon_material_grid(material_data *md, double u) {
// NOTE: assume p lies on normalized grid within (0,1)
if (!(md->weights)) meep::abort("material params were not initialized!");
medium_struct *mm = &(md->medium);
medium_struct *m1 = &(md->medium_1);
medium_struct *m2 = &(md->medium_2);
// Linearly interpolate dc epsilon values
cinterp_tensors(m1->epsilon_diag, m1->epsilon_offdiag, m2->epsilon_diag, m2->epsilon_offdiag,
&mm->epsilon_diag, &mm->epsilon_offdiag, u);
// Interpolate resonant strength from d.p.
vector3 zero_vec;
zero_vec.x = zero_vec.y = zero_vec.z = 0;
for (size_t i = 0; i < m1->E_susceptibilities.size(); i++) {
// iterate through medium1 sus list first
interp_tensors(zero_vec, zero_vec, m1->E_susceptibilities[i].sigma_diag,
m1->E_susceptibilities[i].sigma_offdiag, &mm->E_susceptibilities[i].sigma_diag,
&mm->E_susceptibilities[i].sigma_offdiag, (1 - u));
}
for (size_t i = 0; i < m2->E_susceptibilities.size(); i++) {
// iterate through medium2 sus list next
size_t j = i + m1->E_susceptibilities.size();
interp_tensors(zero_vec, zero_vec, m2->E_susceptibilities[i].sigma_diag,
m2->E_susceptibilities[i].sigma_offdiag, &mm->E_susceptibilities[j].sigma_diag,
&mm->E_susceptibilities[j].sigma_offdiag, u);
}
// Linearly interpolate electric conductivity
vector3 zero_offdiag;
interp_tensors(m1->D_conductivity_diag, zero_vec, m2->D_conductivity_diag, zero_vec,
&mm->D_conductivity_diag, &zero_offdiag, u);
// Add damping factor if we have dispersion.
// This prevents instabilities when interpolating between sus. profiles.
if ((m1->E_susceptibilities.size() + m2->E_susceptibilities.size()) > 0.0) {
// calculate mean harmonic frequency
double omega_mean = 0;
for (const susceptibility &m1_sus : m1->E_susceptibilities) {
omega_mean += m1_sus.frequency;
}
for (const susceptibility &m2_sus : m2->E_susceptibilities) {
omega_mean += m2_sus.frequency;
}
omega_mean = omega_mean / (m1->E_susceptibilities.size() + m2->E_susceptibilities.size());
// assign interpolated, nondimensionalized conductivity term
// TODO: dampen the lorentzians to improve stability
// mm->D_conductivity_diag.x = mm->D_conductivity_diag.y = mm->D_conductivity_diag.z = u*(1-u) *
// omega_mean;
md->trivial = false;
}
double fake_damping = u * (1 - u) * (md->damping);
mm->D_conductivity_diag.x += fake_damping;
mm->D_conductivity_diag.y += fake_damping;
mm->D_conductivity_diag.z += fake_damping;
// set the trivial flag
if (md->damping != 0) md->trivial = false;
}
// return material of the point p from the file (assumed already read)
void epsilon_file_material(material_data *md, vector3 p) {
set_default_material(md);
if (md->which_subclass != material_data::MATERIAL_FILE)
meep::abort("epsilon-input-file only works with a type=file default-material");
if (!(md->epsilon_data)) return;
medium_struct *mm = &(md->medium);
double rx =
geometry_lattice.size.x == 0 ? 0 : 0.5 + (p.x - geometry_center.x) / geometry_lattice.size.x;
double ry =
geometry_lattice.size.y == 0 ? 0 : 0.5 + (p.y - geometry_center.y) / geometry_lattice.size.y;
double rz =
geometry_lattice.size.z == 0 ? 0 : 0.5 + (p.z - geometry_center.z) / geometry_lattice.size.z;
mm->epsilon_diag.x = mm->epsilon_diag.y = mm->epsilon_diag.z =
meep::linear_interpolate(rx, ry, rz, md->epsilon_data, md->epsilon_dims[0],
md->epsilon_dims[1], md->epsilon_dims[2], 1);
mm->epsilon_offdiag.x.re = mm->epsilon_offdiag.y.re = mm->epsilon_offdiag.z.re = 0;
}
/***********************************************************************/
geom_epsilon::geom_epsilon(geometric_object_list g, material_type_list mlist,
const meep::volume &v) {
// Copy the geometry
int length = g.num_items;
geometry.num_items = length;
geometry.items = new geometric_object[length];
for (int i = 0; i < length; i++) {
geometric_object_copy(&g.items[i], &geometry.items[i]);
geometry.items[i].material = new material_data();
static_cast<material_data *>(geometry.items[i].material)
->copy_from(*(material_data *)(g.items[i].material));
}
extra_materials = mlist;
current_pol = NULL;
FOR_DIRECTIONS(d) FOR_SIDES(b) { cond[d][b].prof = NULL; }
if (meep::am_master()) {
int num_print = meep::verbosity > 2
? geometry.num_items
: std::min(geometry.num_items, meep::verbosity > 0 ? 10 : 0);
for (int i = 0; i < geometry.num_items; ++i) {
if (i < num_print) display_geometric_object_info(5, geometry.items[i]);
medium_struct *mm;
if (is_medium(geometry.items[i].material, &mm)) {
mm->check_offdiag_im_zero_or_abort();
if (i < num_print)
master_printf("%*sdielectric constant epsilon diagonal "
"= (%g,%g,%g)\n",
5 + 5, "", mm->epsilon_diag.x, mm->epsilon_diag.y, mm->epsilon_diag.z);
}
}
if (num_print < geometry.num_items && meep::verbosity > 0)
master_printf("%*s...(+ %d objects not shown)...\n", 5, "", geometry.num_items - num_print);
}
geom_fix_object_list(geometry);
geom_box box = gv2box(v);
geometry_tree = create_geom_box_tree0(geometry, box);
if (meep::verbosity > 2 && meep::am_master()) {
master_printf("Geometric-object bounding-box tree:\n");
display_geom_box_tree(5, geometry_tree);
int tree_depth, tree_nobjects;
geom_box_tree_stats(geometry_tree, &tree_depth, &tree_nobjects);
master_printf("Geometric object tree has depth %d "
"and %d object nodes (vs. %d actual objects)\n",
tree_depth, tree_nobjects, geometry.num_items);
}
restricted_tree = geometry_tree;
}
// copy constructor
geom_epsilon::geom_epsilon(const geom_epsilon &geps1) {
// Copy the geometry
int length = geps1.geometry.num_items;
geometry.num_items = length;
geometry.items = new geometric_object[length];
for (int i = 0; i < length; i++) {
geometric_object_copy(&geps1.geometry.items[i], &geometry.items[i]);
geometry.items[i].material = new material_data();
static_cast<material_data *>(geometry.items[i].material)
->copy_from(*(material_data *)(geps1.geometry.items[i].material));
}
geometry_tree = geps1.geometry_tree;
restricted_tree = geps1.restricted_tree;
extra_materials = geps1.extra_materials;
current_pol = NULL;
FOR_DIRECTIONS(d) FOR_SIDES(b) { cond[d][b].prof = geps1.cond[d][b].prof; }
}
geom_epsilon::~geom_epsilon() {
int length = geometry.num_items;
for (int i = 0; i < length; i++) {
material_free((material_type)geometry.items[i].material);
geometric_object_destroy(geometry.items[i]);
}
delete[] geometry.items;
unset_volume();
destroy_geom_box_tree(geometry_tree);
FOR_DIRECTIONS(d) FOR_SIDES(b) {
if (cond[d][b].prof) delete[] cond[d][b].prof;
}
}
void geom_epsilon::set_cond_profile(meep::direction dir, meep::boundary_side side, double L,
double dx, double (*P)(int, double *, void *), void *data,
double R) {
if (cond[dir][side].prof) delete[] cond[dir][side].prof;
int N = int(L / dx + 0.5);
cond[dir][side].L = L;
cond[dir][side].N = N;
double *prof = cond[dir][side].prof = new double[N + 1];
double umin = 0, umax = 1, esterr;
int errflag;
double prof_int =
adaptive_integration(P, &umin, &umax, 1, data, 1e-9, 1e-4, 50000, &esterr, &errflag);
double prefac = (-log(R)) / (4 * L * prof_int);
for (int i = 0; i <= N; ++i) {
double u = double(i) / N;
prof[i] = prefac * P(1, &u, data);
}
}
void geom_epsilon::unset_volume(void) {
if (restricted_tree != geometry_tree) {
destroy_geom_box_tree(restricted_tree);
restricted_tree = geometry_tree;
}
}
void geom_epsilon::set_volume(const meep::volume &v) {
unset_volume();
geom_box box = gv2box(v);
if (!restricted_tree) restricted_tree = create_geom_box_tree0(geometry, box);
}
static void material_epsmu(meep::field_type ft, material_type material, symm_matrix *epsmu,
symm_matrix *epsmu_inv) {
material_data *md = material;
if (ft == meep::E_stuff || ft == meep::D_stuff) switch (md->which_subclass) {
case material_data::MEDIUM:
case material_data::MATERIAL_FILE:
case material_data::MATERIAL_USER:
case material_data::MATERIAL_GRID:
epsmu->m00 = md->medium.epsilon_diag.x;
epsmu->m11 = md->medium.epsilon_diag.y;
epsmu->m22 = md->medium.epsilon_diag.z;
epsmu->m01 = md->medium.epsilon_offdiag.x.re;
epsmu->m02 = md->medium.epsilon_offdiag.y.re;
epsmu->m12 = md->medium.epsilon_offdiag.z.re;
sym_matrix_invert(epsmu_inv, epsmu);
break;
case material_data::PERFECT_METAL:
epsmu->m00 = -meep::infinity;
epsmu->m11 = -meep::infinity;
epsmu->m22 = -meep::infinity;
epsmu_inv->m00 = -0.0;
epsmu_inv->m11 = -0.0;
epsmu_inv->m22 = -0.0;
epsmu->m01 = epsmu->m02 = epsmu->m12 = 0.0;
epsmu_inv->m01 = epsmu_inv->m02 = epsmu_inv->m12 = 0.0;
break;
default: meep::abort("unknown material type");
}
else
switch (md->which_subclass) {
case material_data::MEDIUM:
case material_data::MATERIAL_FILE:
case material_data::MATERIAL_USER:
case material_data::MATERIAL_GRID:
epsmu->m00 = md->medium.mu_diag.x;
epsmu->m11 = md->medium.mu_diag.y;
epsmu->m22 = md->medium.mu_diag.z;
epsmu->m01 = md->medium.mu_offdiag.x.re;
epsmu->m02 = md->medium.mu_offdiag.y.re;
epsmu->m12 = md->medium.mu_offdiag.z.re;
sym_matrix_invert(epsmu_inv, epsmu);
break;
case material_data::PERFECT_METAL:
epsmu->m00 = 1.0;
epsmu->m11 = 1.0;
epsmu->m22 = 1.0;
epsmu_inv->m00 = 1.0;
epsmu_inv->m11 = 1.0;
epsmu_inv->m22 = 1.0;
epsmu->m01 = epsmu->m02 = epsmu->m12 = 0.0;
epsmu_inv->m01 = epsmu_inv->m02 = epsmu_inv->m12 = 0.0;
break;
default: meep::abort("unknown material type");
}
}
// the goal of this routine is to fill in the 'medium' field
// within the material structure as appropriate for the
// material properties at r.
void geom_epsilon::get_material_pt(material_type &material, const meep::vec &r) {
vector3 p = vec_to_vector3(r);
boolean inobject;
material =
(material_type)material_of_unshifted_point_in_tree_inobject(p, restricted_tree, &inobject);
material_data *md = material;
switch (md->which_subclass) {
// material grid: interpolate onto user specified material grid to get properties at r
case material_data::MATERIAL_GRID:
double u;
int oi;
geom_box_tree tp;
tp = geom_tree_search(p, restricted_tree, &oi);
// interpolate and project onto material grid
u = tanh_projection(matgrid_val(p, tp, oi, md) + this->u_p, md->beta, md->eta);
// interpolate material from material grid point
epsilon_material_grid(md, u);
return;
// material read from file: interpolate to get properties at r
case material_data::MATERIAL_FILE:
if (md->epsilon_data)
epsilon_file_material(md, p);
else
material = (material_type)default_material;
return;
// material specified by user-supplied function: call user
// function to get properties at r.
// Note that we initialize the medium to vacuum, so that
// the user's function only needs to fill in whatever is
// different from vacuum.
case material_data::MATERIAL_USER:
md->medium = medium_struct();
md->user_func(p, md->user_data, &(md->medium));
md->medium.check_offdiag_im_zero_or_abort();
return;
// position-independent material or metal: there is nothing to do
case material_data::MEDIUM:
case material_data::PERFECT_METAL: return;
default: meep::abort("unknown material type");
};
}
// returns trace of the tensor diagonal
double geom_epsilon::chi1p1(meep::field_type ft, const meep::vec &r) {
symm_matrix chi1p1, chi1p1_inv;
#ifdef DEBUG
vector3 p = vec_to_vector3(r);
if (p.x < restricted_tree->b.low.x || p.y < restricted_tree->b.low.y ||
p.z < restricted_tree->b.low.z || p.x > restricted_tree->b.high.x ||
p.y > restricted_tree->b.high.y || p.z > restricted_tree->b.high.z)
meep::abort("invalid point (%g,%g,%g)\n", p.x, p.y, p.z);
#endif
material_type material;
get_material_pt(material, r);
material_epsmu(ft, material, &chi1p1, &chi1p1_inv);
material_gc(material);
return (chi1p1.m00 + chi1p1.m11 + chi1p1.m22) / 3;
}
/* Find frontmost object in v, along with the constant material behind it.
Returns false if material behind the object is not constant.
Requires moderately horrifying logic to figure things out properly,
stolen from MPB. */
static bool get_front_object(const meep::volume &v, geom_box_tree geometry_tree, vector3 &pcenter,
const geometric_object **o_front, vector3 &shiftby_front,
material_type &mat_front, material_type &mat_behind) {
vector3 p;
const geometric_object *o1 = 0, *o2 = 0;
vector3 shiftby1 = {0, 0, 0}, shiftby2 = {0, 0, 0};
geom_box pixel;
material_type mat1 = vacuum, mat2 = vacuum;
int id1 = -1, id2 = -1;
const int num_neighbors[3] = {3, 5, 9};
const int neighbors[3][9][3] = {{{0, 0, 0},
{0, 0, -1},
{0, 0, 1},
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}},
{{0, 0, 0},
{-1, -1, 0},
{1, 1, 0},
{-1, 1, 0},
{1, -1, 0},
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}},
{{0, 0, 0},
{1, 1, 1},
{1, 1, -1},
{1, -1, 1},
{1, -1, -1},
{-1, 1, 1},
{-1, 1, -1},
{-1, -1, 1},
{-1, -1, -1}}};
pixel = gv2box(v);
pcenter = p = vec_to_vector3(v.center());
double d1, d2, d3;
d1 = (pixel.high.x - pixel.low.x) * 0.5;
d2 = (pixel.high.y - pixel.low.y) * 0.5;
d3 = (pixel.high.z - pixel.low.z) * 0.5;
int dimension_index = meep::number_of_directions(dim) - 1;
for (int i = 0; i < num_neighbors[dimension_index]; ++i) {
const geometric_object *o;
material_type mat;
vector3 q, shiftby;
int id;
q.x = p.x + neighbors[dimension_index][i][0] * d1;
q.y = p.y + neighbors[dimension_index][i][1] * d2;
q.z = p.z + neighbors[dimension_index][i][2] * d3;
o = object_of_point_in_tree(q, geometry_tree, &shiftby, &id);
if ((id == id1 && vector3_equal(shiftby, shiftby1)) ||
(id == id2 && vector3_equal(shiftby, shiftby2)))
continue;
mat = (material_type)default_material;
if (o) {
material_data *md = (material_data *)o->material;
if (md->which_subclass != material_data::MATERIAL_FILE) mat = md;
}
if (id1 == -1) {
o1 = o;
shiftby1 = shiftby;
id1 = id;
mat1 = mat;
}
else if (id2 == -1 ||
((id >= id1 && id >= id2) && (id1 == id2 || material_type_equal(mat1, mat2)))) {
o2 = o;
shiftby2 = shiftby;
id2 = id;
mat2 = mat;
}
else if (!(id1 < id2 && (id1 == id || material_type_equal(mat1, mat))) &&
!(id2 < id1 && (id2 == id || material_type_equal(mat2, mat))))
return false;
}
// CHECK(id1 > -1, "bug in object_of_point_in_tree?");
if (id2 == -1) { /* only one nearby object/material */
id2 = id1;