-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatlc3.cpp
2619 lines (2245 loc) · 99.2 KB
/
atlc3.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
#include "atlc3.h"
//static const char *names[] = {"Vacuum", "Air", "PTFE", "duroid_5880", "Polyethelene", "Polystyrene", "PVC", "Epoxy_resin", "FR4 PCB", "Fibreglass_PCB", "duroid_6006", "duroid_6010", "one_hundred"};
static float Ers[] = {1.0, 1.0006, 2.1, 2.2, 2.33, 2.5, 3.3, 3.335, 3.7, 4.8, 6.15, 10.2, 100.0};
static std::uint32_t colours[]={0xffffff, 0xffcaca, 0x8235ef, 0x8e8e8e, 0xff00ff, 0xffff00, 0xefcc1a, 0xbc7f60, 0xdff788, 0x1aefb3, 0x696969, 0xdcdcdc, 0xd5a04d};
atlc3::atlc3()
: _coupler(false)
, _verbose_level(0)
{
_r = 1.9;
_cutoff = 0.0001;
_write_binary_field_imagesQ = true;
_write_bitmap_field_imagesQ = true;
_inputfile_filename = "test.bmp";
}
atlc3::~atlc3()
{
}
bool atlc3::setup_arrays(const matrix_rgb & img)
{
struct pixels pixels_found;
bool conductor_found;
int conductors = 0;
std::uint8_t red;
std::uint8_t green;
std::uint8_t blue;
std::uint32_t colour_mixture;
bool dielectric_found;
//int new_colour_in_image;
//data->dielectrics_in_bitmap=0;
pixels_found.red = 0;
pixels_found.green = 0;
pixels_found.blue = 0;
pixels_found.white = 0;
pixels_found.other_colour = 0;
_mat.create(img.rows(), img.cols(), 1);
for (std::int32_t row = 0; row < img.rows(); row++)
{
for (std::int32_t col = 0; col < img.cols(); col++)
{
const rgb& pixel = img.at(row, col);
atlc3_node& node = _mat.at(row, col);
blue = pixel.b;
green = pixel.g;
red = pixel.r;
dielectric_found = false;
conductor_found = false;
colour_mixture = 256 * 256 * red + 256 * green + blue;
if (colour_mixture == 0xff0000) /* +1V red */
{
node.cell_type = CONDUCTOR_PLUS_ONE_V;
node.v = 1.0;
node.er = METAL_ER;
conductor_found = true;
pixels_found.red++;
}
else if (colour_mixture == 0x00ff00) /* 0v green */
{
node.cell_type = CONDUCTOR_ZERO_V;
node.v = 0.0;
node.er = METAL_ER;
conductor_found = true;
pixels_found.green++;
}
else if (colour_mixture == 0x0000ff) /* -1V blue */
{
node.cell_type = CONDUCTOR_MINUS_ONE_V;
node.v = -1.0;
node.er = METAL_ER;
conductor_found = true;
pixels_found.blue++;
_coupler = true;
}
else /* A dielectric */
{
if (colour_mixture == 0xffffff) /* White */
{
pixels_found.white++; /* Vacuum */
}
else
{
pixels_found.other_colour++; /* Some other dielectric */
}
node.cell_type = DIELECTRIC;
node.v = 0.0;
for(std::uint32_t z = 0; z < NUMBER_OF_DIELECTRICS_DEFINED; ++z)
{
/* Check to see if the colour is one of the 10 known
about, without any need to define on the command line
*/
if (colour_mixture == colours[z])
{
node.er = Ers[z];
dielectric_found = true;
if(z != 0)
{
//non_vacuum_found = true;
//data->found_this_dielectric=Ers[z];
}
}
}
for(std::uint32_t i = 0; i < _er_list.size(); ++i)
{
if (_er_list[i].first == colour_mixture)
{
node.er = _er_list[i].second;
dielectric_found = true;
//non_vacuum_found = true;
//data->found_this_dielectric = _er_list[i].second;
}
}
}
if ((dielectric_found == false) && (conductor_found == false))
{
fprintf(stderr, "Error#7: The colour r=0x%x g=0x%x b=0x%x (0x%02x%02x%02x) exists at pixel %d,%d.\n", red, green, blue, red, green, blue, col, row);
fprintf(stderr, "However, atlc does not know how to interpret this colour. This is not a\n");
fprintf(stderr, "conductor (pure red, green or blue), nor is it one of the %d dielectrics that\n", NUMBER_OF_DIELECTRICS_DEFINED);
fprintf(stderr, "are predefined in Erdata.h, nor is a corresponding dielectric constant defined\n");
fprintf(stderr, "on the command line with the -d option. Sometimes this occurs when a\n");
fprintf(stderr, "graphics package is used to make the bitmap, but it performs some form of\n");
fprintf(stderr, "anti-aliasing or smooting. If this is the case, redraw the image turning such\n");
fprintf(stderr, "options off. If this is not the case then re-run atlc adding the -d option\n");
fprintf(stderr, "to define the relative permittivity of the dielectric\n\n");
fprintf(stderr, "e.g. atlc -d %02x%02x%02x=1.9 xx.bmp \n\n", red, green, blue);
fprintf(stderr, "if this colour has a permittivity of 1.9. If there are multiple undefined\n");
fprintf(stderr, "dielectrics, then there will need to be multiple copies of the -d option given.\n");
return false;
}
/* We need to keep a record of the number of dielectrics in the image,
and determine if they are defined on the command line, or if they are
defined on in the header file. */
if (dielectric_found == true)
{
if (_er_bitmap.count(colour_mixture) == 0)
{
pixels pix;
pix.other_colour = colour_mixture;
pix.red = red;
pix.green = green;
pix.blue = blue;
pix.epsilon = node.er;
_er_bitmap.emplace(colour_mixture, pix);
}
} /* end of if dielctric found */
}
}
/* The following prints a lot of data that is generally not wanted
but is when finding statistics of performance etc. */
if (_verbose_level >= 3)
{
printf("Red (+1 V conductor) pixels found = %8d \n", pixels_found.red);
printf("Green (0 V conductor) pixels found = %8d \n", pixels_found.green);
printf("Blue (-1 V conductor) pixels found = %8d \n", pixels_found.blue);
printf("White (vacuum dielectric) pixels found = %8d \n", pixels_found.white);
printf("Others (not vacuum dielectic) pixels found = %8d \n", pixels_found.other_colour);
printf("Width = %8d \n", img.cols());
printf("Height = %8d \n", img.rows());
printf("Pixels = %8d \n", img.cols() * img.rows());
printf("Number of Dielectrics found = %8d \n", (std::int32_t)_er_bitmap.size());
std::uint32_t non_metallic_pixels = img.cols() * img.rows() - pixels_found.red - pixels_found.green - pixels_found.blue;
printf("Number of non-metallic pixels = %8d \n", non_metallic_pixels);
//printf("filename = %30s \n", inputfile_name);
if (pixels_found.red > 0)
{
conductors += 1;
}
if (pixels_found.green > 0 )
{
conductors += 1;
}
if (pixels_found.blue > 0 )
{
conductors += 1;
}
printf("Number of Conductors = %d \n", conductors);
}
/* The following should not be necessary, but may be as a test */
/* I'd like to Miguel Berg for noticcing a servere bug, where the
indeces of w and h were transposed, leading to crashes on Windoze
XP */
for (std::int32_t row = 0; row < _mat.rows(); row++)
{
for (std::int32_t col = 0; col < _mat.cols(); col++)
{
atlc3_node& node = _mat.at(row, col);
if((node.v > 1.0) || (node.v < -1.0))
{
fprintf(stderr,"Sorry, something is wrong Vij[%d][%d]=%f in %s %d\n", col, row, node.v, __FILE__,__LINE__);
}
}
}
/* Check two conductors and not next to each other, creating a short */
//check_for_shorts();
return true;
}
bool atlc3::set_oddity_value()
{
/* Its easier to set the endge values first, as it
reduces the amount of checking needed in the main body.
There are only 11 cases here - 3 types of metal,
the four corners, and the four sides */
for (std::int32_t row = 0; row < _mat.rows(); row++)
{
for (std::int32_t col = 0; col < _mat.cols(); col++)
{
atlc3_node& node = _mat.at(row, col);
node.oddity = UNDEFINED_ODDITY; /* Stick it to some underfined status */
std::uint8_t c = node.cell_type; /* Cell type at point (i,j) */
/* The 3 metal cases can be quickly checked and the
oddity value assigned to a fixed value depending on
whether it's -1, 0 or +1 V */
if (c <= CONDUCTOR_PLUS_ONE_V) /* a metal */
{
node.oddity = c;
}
/* Now do the 4 courners */
else if ((col == 0) && (row == _mat.rows() - 1))
{
node.oddity = BOTTOM_LEFT_CORNER;
}
else if ((col == _mat.cols() - 1) && (row == _mat.rows() - 1))
{
node.oddity = BOTTOM_RIGHT_CORNER;
}
else if ((col == 0) && (row == 0))
{
node.oddity = TOP_LEFT_CORNER;
}
else if((col == _mat.cols() - 1) && (row == 0))
{
node.oddity = TOP_RIGHT_CORNER;
}
/* Now the four edges */
else if (col == 0)
{
node.oddity = ORDINARY_POINT_LEFT_EDGE;
}
else if (row == 0)
{
node.oddity = ORDINARY_POINT_TOP_EDGE;
}
else if (row == _mat.rows() - 1)
{
node.oddity = ORDINARY_POINT_BOTTOM_EDGE;
}
else if (col == _mat.cols() - 1)
{
node.oddity = ORDINARY_POINT_RIGHT_EDGE;
}
else if ((col == 0 || col == _mat.cols() - 1 || row == 0 || row == _mat.rows() - 1) && (node.oddity == UNDEFINED_ODDITY))
{
fprintf(stderr,"Internal error: one of the edge points (%d,%d) is still undefined\n", col, row);
fprintf(stderr, "ZZZZZZZZZZZZZ width=%d height=%d\n", _mat.cols(), _mat.rows());
fprintf(stderr,"Error set_oddity_value.c\n");
return false;
}
}
}
/* With the oddity values of all the edges now know, the centre
values can be attempted */
for (std::int32_t i = 1; i < _mat.cols() - 1; ++i)
{
for(std::int32_t j = 1; j < _mat.rows() - 1; ++j)
{
std::uint8_t c = _mat.at(j, i).cell_type; /* Cell type at point (i,j) */
std::uint8_t cl = _mat.at(j, i - 1).cell_type; /* Cell type to left of point (i,j) */
std::uint8_t cr = _mat.at(j, i + 1).cell_type; /* Cell type to right of point (i,j) */
std::uint8_t ca = _mat.at(j - 1, i).cell_type; /* Cell type above point (i,j) */
std::uint8_t cb = _mat.at(j + 1, i).cell_type; /* Cell type below point (i,j) */
float ERa = _mat.at(j - 1, i).er;
float ERb = _mat.at(j + 1, i).er;
float ERl = _mat.at(j, i - 1).er;
float ERr = _mat.at(j, i + 1).er;
//float er = _mat.at(j, i).er;
atlc3_node& node = _mat.at(j, i);
/* If the conductor is at a fixed v, it must stay there
so there is nothing to do with it */
if (c == CONDUCTOR_ZERO_V)
{
node.oddity = CONDUCTOR_ZERO_V;
}
else if (c== CONDUCTOR_PLUS_ONE_V)
{
node.oddity = CONDUCTOR_PLUS_ONE_V;
}
else if (c == CONDUCTOR_MINUS_ONE_V)
{
node.oddity = CONDUCTOR_MINUS_ONE_V;
}
else if (cr <= CONDUCTOR_PLUS_ONE_V && cb <= CONDUCTOR_PLUS_ONE_V)
{
node.oddity = METAL_BELOW_AND_RIGHT;
}
else if (cr <= CONDUCTOR_PLUS_ONE_V && ca <= CONDUCTOR_PLUS_ONE_V)
{
node.oddity = METAL_ABOVE_AND_RIGHT;
}
else if (cl <= CONDUCTOR_PLUS_ONE_V && cb <= CONDUCTOR_PLUS_ONE_V)
{
node.oddity = METAL_BELOW_AND_LEFT;
}
else if (cl <= CONDUCTOR_PLUS_ONE_V && ca <= CONDUCTOR_PLUS_ONE_V)
{
node.oddity = METAL_ABOVE_AND_LEFT;
}
else if (ca <= CONDUCTOR_PLUS_ONE_V)
{
node.oddity = METAL_ABOVE;
}
else if (cb <= CONDUCTOR_PLUS_ONE_V)
{
node.oddity= METAL_BELOW;
}
else if (cl <= CONDUCTOR_PLUS_ONE_V)
{
node.oddity = METAL_LEFT;
}
else if ( cr <= CONDUCTOR_PLUS_ONE_V )
{
node.oddity = METAL_RIGHT;
}
else if (ERb != ERa)
{
node.oddity = DIFFERENT_DIELECTRIC_VERTICALLY;
}
else if (ERl != ERr)
{
node.oddity= DIFFERENT_DIELECTRIC_HORIZONTALLY;
}
/*
else if ( ERa != er && ERr != er)
node.oddity= DIFFERENT_DIELECTRIC_ABOVE_AND_RIGHT;
else if ( ERa != er && ERl != er)
node.oddity= DIFFERENT_DIELECTRIC_ABOVE_AND_LEFT;
else if ( ERb != er && ERl != er)
node.oddity= DIFFERENT_DIELECTRIC_BELOW_AND_LEFT;
else if ( ERa != er )
node.oddity= DIFFERENT_DIELECTRIC_ABOVE;
else if ( ERb != er )
node.oddity= DIFFERENT_DIELECTRIC_BELOW;
else if ( ERl != er )
node.oddity= DIFFERENT_DIELECTRIC_LEFT;
else if ( ERr != er )
node.oddity= DIFFERENT_DIELECTRIC_RIGHT;
*/
else
{
node.oddity = ORDINARY_INTERIOR_POINT;
}
}/* end of for i=0 to width-1 */
} /* end of for j= 0 to height-1 */
return true;
}
void atlc3::do_fd_calculation()
{
#if 0
{
std::uint32_t count[256];
memset(count, 0, sizeof(count));
for(std::int32_t i = 0; i < _mat.cols() ; ++i)
{
for(std::int32_t j = 0; j < _mat.rows(); ++j)
{
count[_mat.at(j, i).oddity]++;
}
}
for (std::int32_t i = 0; i < 256; i++)
{
if (count[i] > 0)
{
printf("o:%d c:%d\n", i, count[i]);
}
}
}
#endif
/* The following 10 lines are for a single dielectric 2 conductor line */
if (!_coupler)
{
do_tl_calculation();
}
else
{
float L_plus_vacuum = 0.;
float C_plus = 0.;
float L_minus_vacuum = 0.;
float C_minus = 0.;
matrix_atlc mat;
mat.create(_mat.rows(), _mat.cols());
for(std::int32_t row = 0; row < _mat.rows() ; ++row)
{
for(std::int32_t col = 0; col < _mat.cols(); ++col)
{
mat.at(row, col).cell_type = _mat.at(row, col).cell_type;
}
}
do_coupler_calculation();
_coupler = false;
for(std::int32_t row = 0; row < _mat.rows() ; ++row)
{
for(std::int32_t col = 0; col < _mat.cols(); ++col)
{
if (mat.at(row, col).cell_type == CONDUCTOR_MINUS_ONE_V)
{
_mat.at(row, col).cell_type = CONDUCTOR_ZERO_V;
_mat.at(row, col).er = METAL_ER;
_mat.at(row, col).v = 0;
}
}
}
set_oddity_value();
do_tl_calculation();
L_plus_vacuum = _L_vacuum;
C_plus = _C;
for(std::int32_t row = 0; row < _mat.rows() ; ++row)
{
for(std::int32_t col = 0; col < _mat.cols(); ++col)
{
if (mat.at(row, col).cell_type == CONDUCTOR_MINUS_ONE_V)
{
_mat.at(row, col).cell_type = CONDUCTOR_PLUS_ONE_V;
_mat.at(row, col).er = METAL_ER;
_mat.at(row, col).v = 1;
}
else if (mat.at(row, col).cell_type == CONDUCTOR_PLUS_ONE_V)
{
_mat.at(row, col).cell_type = CONDUCTOR_ZERO_V;
_mat.at(row, col).er = METAL_ER;
_mat.at(row, col).v = 0;
}
}
}
set_oddity_value();
do_tl_calculation();
L_minus_vacuum = _L_vacuum;
C_minus = _C;
print_data_for_directional_couplers(L_plus_vacuum, C_plus, L_minus_vacuum, C_minus);
} /* end of if couplers */
}
void atlc3::do_tl_calculation()
{
float capacitance_old;
float capacitance;
float velocity_of_light_in_vacuum;
std::uint32_t count = 0;
float relative_permittivity;
float C_non_vacuum = 0;
float relative_permittivity_odd;
float relative_permittivity_even;
(void)relative_permittivity;
(void)relative_permittivity_odd;
(void)relative_permittivity_even;
velocity_of_light_in_vacuum = 1.0 / (sqrt(MU_0 * EPSILON_0)); /* around 3x10^8 m/s */
if(_verbose_level >= 2)
{
printf("Solving assuming a vacuum dielectric\n");
}
capacitance = VERY_LARGE; /* Can be anything large */
_dielectrics_to_consider_just_now = 1;
do /* Start a finite calculation */
{
capacitance_old = capacitance;
capacitance = finite_difference_single_threaded();
_C_vacuum = capacitance;
_C = capacitance;
_L_vacuum = MU_0 * EPSILON_0 / capacitance; /* Same as L in *ALL* cases */
_Zo_vacuum = sqrt(_L_vacuum / _C_vacuum); /* Standard formaul for Zo */
_C = capacitance;
if (_er_bitmap.size() == 1) /* Just get C by simple scaling of Er */
{
_Er = _er_bitmap.begin()->second.epsilon;
_C = capacitance * _Er; /* Scaled by the single dielectric constant */
}
else
{
_Er = 1.0;
}
_Zo = sqrt(_L_vacuum / _C); /* Standard formula for Zo */
_Zodd = sqrt(_L_vacuum / _C); /* Standard formula for Zo */
_velocity = 1.0 / pow(_L_vacuum * _C, 0.5);
_velocity_factor = _velocity / velocity_of_light_in_vacuum;
relative_permittivity = sqrt(_velocity_factor); /* ??? XXXXXX */
if (_verbose_level > 0) /* Only needed if intermediate results wanted. */
{
print_data_for_two_conductor_lines();
}
count++;
}
while (fabs((capacitance_old - capacitance) / capacitance_old) > _cutoff); /* end of FD loop */
if (_verbose_level >= 4)
{
printf("Total of %d iterations ( %d calls to finite_difference() )\n", ITERATIONS * count, count);
}
if ((_write_binary_field_imagesQ || _write_bitmap_field_imagesQ) && _er_bitmap.size() == 1)
{
write_fields();
}
if (_verbose_level == 0 && _er_bitmap.size() == 1)
{
print_data_for_two_conductor_lines();
}
if (_er_bitmap.size() > 1)
{
/* We know the capacitance and inductance for the air spaced line
as we calculated it above. Howerver, whilst the inductance
is independant of the dielectric, the capacitance is not, so this
has to be recalculated, taking care not to alter the inductance
at all */
if(_verbose_level >= 2)
{
printf("Now taking into account the permittivities of the different dielectrics for 2 conductors.\n");
}
_dielectrics_to_consider_just_now = 2; /* Any number > 1 */
capacitance = VERY_LARGE; /* Can be anything large */
do /* Start a finite calculation */
{
capacitance_old = capacitance;
capacitance = finite_difference_single_threaded();
_C = capacitance;
C_non_vacuum = capacitance;
_Zo = sqrt(_L_vacuum / C_non_vacuum); /* Standard formula for Zo */
_velocity = 1.0 / pow(_L_vacuum * C_non_vacuum, 0.5);
_velocity_factor = _velocity / velocity_of_light_in_vacuum;
relative_permittivity = sqrt(_velocity_factor); /* ??? XXXXXX */
_Er = _C / _C_vacuum;
if (_verbose_level > 0 ) /* Only needed if intermediate results wanted. */
{
print_data_for_two_conductor_lines();
}
}
while (fabs((capacitance_old - capacitance) / capacitance_old) > _cutoff); /* end of FD loop */
/* We must print the results now, but only bother if the verbose level was
not not incremented on the command line, otherwide there will be two duplicate
lines */
if (_verbose_level == 0)
{
print_data_for_two_conductor_lines();
}
if (_write_binary_field_imagesQ || _write_bitmap_field_imagesQ)
{
write_fields();
}
}
}
void atlc3::do_coupler_calculation()
{
float capacitance_old;
float capacitance;
float velocity_of_light_in_vacuum;
float relative_permittivity;
float relative_permittivity_odd;
float relative_permittivity_even;
(void)relative_permittivity;
(void)relative_permittivity_odd;
(void)relative_permittivity_even;
velocity_of_light_in_vacuum = 1.0 / (sqrt(MU_0 * EPSILON_0)); /* around 3x10^8 m/s */
/* The properties of a couplers will be computed in 2 or 4 stages
1) Compute the odd-mode impedance, assuming a vacuum dielectric, or
if there is just one dielectric, that one.
2) Compute the odd-mode impedance, taking into account the effect of
multiple dielectrics, IF NECESSARY
at this point, the negative voltages will be turned into positive ones.
3) Compute the even-mode impedance, assuming a vacuum dielectric, or
if there is just one dielectric, that one.
4) Compute the even-mode impedance, taking into account the effect of
multiple dielectrics, IF NECESSARY */
/* Stage 1 - compute the odd mode impedance assuming single dielectric */
_display = Z_ODD_SINGLE_DIELECTRIC;
_dielectrics_to_consider_just_now = 1;
capacitance = VERY_LARGE; /* Can be anything large */
if(_verbose_level >= 2)
{
printf("Solving assuming a vacuum dielectric to compute the odd-mode impedance\n");
}
do /* Start a finite difference calculation */
{
capacitance_old = capacitance;
capacitance = finite_difference_single_threaded();
_Codd_vacuum = capacitance;
_Codd = capacitance;
_Lodd_vacuum = MU_0 * EPSILON_0 / capacitance; /* Same as L in *ALL* cases */
_Zodd_vacuum = sqrt(_Lodd_vacuum / _Codd_vacuum); /* Standard formaul for Zodd */
if (_er_bitmap.size() == 1) /* Just get C by simple scaling of Er */
{
_Codd *= _er_bitmap.begin()->second.epsilon; /* Scaled by the single dielectric constant */
}
else
{
_Er = 1.0;
}
_Zodd = sqrt(_Lodd_vacuum / _Codd); /* Standard formula for Zo */
/* FPE trapdata->velocity_odd=1.0/pow(data->L_vacuum*data->Codd,0.5); */
_velocity_odd = 1.0 / pow(_Lodd_vacuum * _Codd, 0.5);
_velocity_factor_odd = _velocity_odd / velocity_of_light_in_vacuum;
relative_permittivity_odd = sqrt(_velocity_factor_odd); /* ??? XXXXXX */
_Er_odd = _Codd / _Codd_vacuum;
_Zdiff = 2.0 * _Zodd;
/* Print text if uses wants it */
if(_verbose_level >= 1)
{
print_data_for_directional_couplers();
}
} while (fabs((capacitance_old - capacitance) / capacitance_old) > _cutoff); /* end of FD loop */
/* display bitpamps/binary files if this is the last odd-mode computation */
if ((_write_binary_field_imagesQ || _write_bitmap_field_imagesQ) && _er_bitmap.size() == 1)
{
write_fields("odd.");
}
/* Stage 2 - compute the odd-mode impedance taking into account other dielectrics IF NECESSARY */
if (_er_bitmap.size() >1)
{
if (_verbose_level >= 2)
{
printf("Now taking into account the permittivities of the different dielectrics to compute Zodd.\n");
}
_display = Z_ODD_SINGLE_DIELECTRIC;
capacitance = VERY_LARGE; /* Can be anything large */
_dielectrics_to_consider_just_now = 2;
do /* Start a finite calculation */
{
capacitance_old = capacitance;
capacitance = finite_difference_single_threaded();
_Codd = capacitance;
_Zodd= sqrt(_Lodd_vacuum / _Codd); /* Standard formula for Zo */
_velocity_odd = 1.0 / pow(_L_vacuum * _C, 0.5);
_velocity_factor_odd = _velocity / velocity_of_light_in_vacuum;
relative_permittivity_odd = sqrt(_velocity_factor); /* ??? XXXXXX */
_Er_odd = _Codd / _Codd_vacuum;
_Zdiff = 2.0 * _Zodd;
if(_verbose_level >= 1)
{
print_data_for_directional_couplers();
}
} while (fabs((capacitance_old - capacitance) / capacitance_old) > _cutoff); /* end of FD loop */
if ((_write_binary_field_imagesQ || _write_bitmap_field_imagesQ) && _er_bitmap.size() != 1)
{
write_fields("odd.");
}
} /* end of stage 2 for couplers */
/* Stage 3 - compute the even-mode impedance assuming single dielectric */
/* Since we want the even mode impedance now, we swap all the -1V
metallic conductors for +1V */
swap_conductor_voltages();
_display = Z_EVEN_SINGLE_DIELECTRIC;
_dielectrics_to_consider_just_now = 1;
if(_verbose_level >= 2)
{
printf("Now assuming a vacuum dielectric to compute Zeven\n");
}
capacitance = VERY_LARGE; /* Can be anything large */
do /* Start a finite difference calculation */
{
capacitance_old = capacitance;
capacitance = finite_difference_single_threaded();
_Ceven_vacuum = capacitance;
_Ceven = capacitance;
_Leven_vacuum = MU_0 * EPSILON_0 / capacitance; /* Same as L in *ALL* cases */
_Zeven_vacuum = sqrt(_Leven_vacuum / _Ceven_vacuum); /* Standard formaul for Zodd */
if (_er_bitmap.size() == 1) /* Just get C by simple scaling of Er */
{
_Ceven *= _er_bitmap.begin()->second.epsilon; /* Scaled by the single dielectric constant */
}
else
{
_Er_even = 1.0;
}
_Zeven = sqrt(_Leven_vacuum / _Ceven); /* Standard formula for Zo */
_velocity_even = 1.0 / pow(_Leven_vacuum * _Ceven, 0.5);
_velocity_factor_even = _velocity_even / velocity_of_light_in_vacuum;
relative_permittivity_even = sqrt(_velocity_factor_even); /* ??? XXXXXX */
_Er_even = _Ceven / _Ceven_vacuum;
_Zcomm = _Zeven / 2.0;
_Zo = sqrt(_Zodd * _Zeven);
if (_verbose_level >= 1)
{
print_data_for_directional_couplers();
}
/* display bitpamps/binary files if this is the last even-mode computation */
} while (fabs((capacitance_old - capacitance) / capacitance_old) > _cutoff); /* end of FD loop */
if ((_write_binary_field_imagesQ || _write_bitmap_field_imagesQ) && _er_bitmap.size() == 1)
{
write_fields("even.", DONT_ZERO_ELEMENTS);
}
capacitance = VERY_LARGE; /* Can be anything large */
/* Stage 4 - compute the even-mode impedance assuming multiple dielectrics IF NECESSARY */
if (_er_bitmap.size() > 1)
{
_dielectrics_to_consider_just_now=2;
if (_verbose_level >= 2)
{
printf("Now taking into account the permittivities of the different dielectrics to compute Zeven\n");
}
do /* Start a finite calculation */
{
capacitance_old = capacitance;
capacitance = finite_difference_single_threaded();
_Ceven = capacitance;
_Zeven = sqrt(_Leven_vacuum / _Ceven); /* Standard formula for Zo */
_velocity_even = 1.0 / pow(_L_vacuum * _C, 0.5);
_velocity_factor_even = _velocity / velocity_of_light_in_vacuum;
relative_permittivity_even = sqrt(_velocity_factor); /* ??? XXXXXX */
_Er_even = _Ceven / _Ceven_vacuum;
_Zdiff = 2.0 * _Zodd;
_Zcomm = _Zeven / 2.0;
_Zo = sqrt(_Zeven * _Zodd);
if (_verbose_level >= 1)
{
print_data_for_directional_couplers();
}
} while (fabs((capacitance_old - capacitance) / capacitance_old) > _cutoff); /* end of FD loop */
if (_write_binary_field_imagesQ || _write_bitmap_field_imagesQ)
{
write_fields("even.", DONT_ZERO_ELEMENTS);
}
} /* end of stage 4 */
/* Print the results if the verbose level was 0 (no -v flag(s) ). */
if (_verbose_level == 0)
{
/* We need to print the data. The next function will only print if
the verbose_level is 1 or more, so I'll fix it at one. Then we print
the final results and exit. */
_verbose_level = 1;
_display = Z_EVEN_SINGLE_DIELECTRIC;
print_data_for_directional_couplers();
_verbose_level = 0;
}
}
float atlc3::finite_difference_single_threaded()
{
int number_of_iterations = 25;
float capacitance_per_metre;
float energy_per_metre;
/* The following might not look very neat, with a whole load of code being
written twice, when it would be posible to make it easier to read if the
'if(dielectrics_in_bitmap > 1)' was in an inner loop. However, the
following is almost certainly more efficient. It is not a good idea to
have any more than necessary in the inner loop.
The option to avoid the faster convergence algorithm has been didtched
too, as this was in an inner loop. The faster covergence method seems
to work fine, so there is no need to avoid using it */
/* Note, that while the number of intterations requested is set in the first
parameter to update_voltage_array, the actual number done is 4x higher, as
each computation id done in 4 directions */
//update_voltage_array(number_of_iterations, 0, _mat.cols() - 1, 0, _mat.rows() - 1);
#if 0
update_voltage_array_fast(number_of_iterations);
#else
if (_dielectrics_to_consider_just_now == 1)
{
update_voltage_array_fast_ignore_dielectric(number_of_iterations);
}
else
{
update_voltage_array_fast_dielectric(number_of_iterations);
}
#endif
//update_voltage_array_int(number_of_iterations);
//update_voltage_array_fast(number_of_iterations, 1, _mat.cols() - 2, 1, _mat.rows() - 2);
//matrix_rgb img_er;
//cvt_rgb_er(img_er);
//cv::Mat cvimger(img_er.rows(), img_er.cols(), CV_8UC3, img_er.data());
//cv::imshow("imger", cvimger);
//matrix_rgb img;
//cvt_rgb(img);
//cv::Mat cvimg(img.rows(), img.cols(), CV_8UC3, img.data());
//cv::imshow("img", cvimg);
//cv::waitKey(100);
/* Once the v distribution is found, the energy in the field may be
found. This can be shown to be Energy = 0.5 * integral(E.D) dV, when
integrated over a volume V, and D.E is the vector dot product of E and
D.
Energy per metre is 0.5 * D.E or (0.5*Epsilon)* E.E. Now E.E is given
by Ex^2 + Ey^2 (by definition of a dot product. */
energy_per_metre = 0.0;
for(std::int32_t i = 1; i < _mat.cols() - 1; ++i)
{
for(std::int32_t j = 1; j < _mat.rows() - 1; ++j)
{
energy_per_metre += find_energy_per_metre(i, j);
}
}
if (_coupler == false)
{
capacitance_per_metre = 2 * energy_per_metre;
}
else
{
capacitance_per_metre = energy_per_metre;
}
return (capacitance_per_metre);
}
/* The following function updates the v on the matrix V_to given data about the
oddity of the location i,j and the voltages in the matrix V_from. It does this for n interations
between rows jmin and jmax inclusive and between columns imain and imax inclusive */
void atlc3::update_voltage_array(int nmax, int imin, int imax, int jmin, int jmax)
{
int k, i, j, n;
unsigned char oddity_value;
float Va, Vb, Vl, Vr, ERa, ERb, ERl, ERr;
float Vnew, g;
float r = 1.9;
std::uint32_t width = _mat.cols();
std::uint32_t height = _mat.rows();
if (_dielectrics_to_consider_just_now == 1)
{
g = r;
}
else
{
g = 1.0;
}
for(n = 0; n < nmax; ++n)
{
for(k = 0; k < 4; ++k)
{