-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
1438 lines (1260 loc) · 72.5 KB
/
Copy pathscript.js
File metadata and controls
1438 lines (1260 loc) · 72.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
// --- START OF FILE script.js ---
(function() {
'use strict'; // Enforce stricter parsing and error handling
// --- Constants ---
const PI = Math.PI;
// const C_LIGHT = 299792458; // Speed of light (m/s) - Currently unused
// --- Module State (Application State) ---
let beamParams = {
w0_um: 100,
z0_mm: 0,
lambda_nm: 1064,
M2: 1.0,
n: 1.0, // Base refractive index n1
plotRangeZ_mm: 500,
z_min_mm: -20
};
let opticalElements = []; // { type, position_mm, property: {..}, id }
let latestPlotData = null; // Cache for canvas and export
let plotsInitialized = { w: false, r: false };
// --- DOM Element References ---
// Initialized in initDOMReferences()
let elementTypeSelect, elementPositionInput, addElementBtn,
propertyInputsContainer, propGroups, propInputs, opticsTableBody,
plotWDiv, plotRDiv, exportWBtn, exportRBtn, showElementsCheck,
showWaistsCheck, interactiveCanvasElement,
exportSetupBtn, importSetupBtn, importSetupInput;
// =========================================================================
// === UTILITIES ===========================================================
// =========================================================================
function debounce(func, wait, immediate) {
let timeout;
return function() {
const context = this, args = arguments;
const later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
// --- Complex Number Helpers ---
const complex = (re = 0, im = 0) => ({ re, im });
const complexAdd = (c1, c2) => complex(c1.re + c2.re, c1.im + c2.im);
const complexSub = (c1, c2) => complex(c1.re - c2.re, c1.im - c2.im);
const complexMul = (c1, c2) => complex(c1.re * c2.re - c1.im * c2.im, c1.re * c2.im + c1.im * c2.re);
const complexConj = (c) => complex(c.re, -c.im);
const complexMagSq = (c) => c.re * c.re + c.im * c.im;
const complexDiv = (c1, c2) => {
const denom = complexMagSq(c2);
if (denom === 0) return complex(Infinity, Infinity);
const num = complexMul(c1, complexConj(c2));
return complex(num.re / denom, num.im / denom);
};
const complexReciprocal = (c) => {
const denom = complexMagSq(c);
if (denom === 0) return complex(Infinity, Infinity);
const conj = complexConj(c);
return complex(conj.re / denom, conj.im / denom);
};
// --- Formatting Helpers ---
function formatElementType(type) {
return type ? type.replace(/_/g, ' ').replace(/\b\w/g, l => l.toUpperCase()) : "Unknown Type";
}
function formatElementProperties(element) {
const props = element.property;
if (!props) return 'Error: No properties';
try {
switch (element.type) {
case 'lens': return `f=${props.f_mm}mm`;
case 'mirror_spherical': return `R=${props.R_mm}mm`;
case 'mirror_flat': return `---`;
case 'slab_dielectric': return `n₂/n₁=${props.n_ratio?.toFixed(2)}, W=${props.width_mm}mm`;
case 'abcd_generic': return `A=${props.A}, B=${props.B_mm}mm, C=${props.C_perm?.toFixed(4)}/mm, D=${props.D}`;
default: return JSON.stringify(props);
}
} catch (e) {
console.error("Error formatting properties for element:", element, e);
return "Error: Invalid props";
}
}
function formatTableValue(value, minPrecision = 2, maxPrecision = 4, epsilon = 1e-9) {
const parsedValue = parseFloat(value);
if (isNaN(parsedValue)) return '---';
const zeroStringMinPrecision = (0.0).toFixed(minPrecision);
const formattedMin = parsedValue.toFixed(minPrecision);
if ((formattedMin === zeroStringMinPrecision || formattedMin === `-${zeroStringMinPrecision}`) && Math.abs(parsedValue) >= epsilon) {
return parsedValue.toFixed(maxPrecision);
} else {
return formattedMin;
}
}
function formatForCSV(value) {
if (value === null || value === undefined) return '""';
if (!isFinite(value)) return value > 0 ? '"Infinity"' : '"-Infinity"';
if (isNaN(value)) return '"NaN"';
return value.toFixed(6);
}
function downloadFile(filename, content, mimeType = 'text/plain;charset=utf-8;') {
const blob = new Blob([content], { type: mimeType });
const link = document.createElement("a");
if (link.download !== undefined) {
const url = URL.createObjectURL(blob);
link.setAttribute("href", url);
link.setAttribute("download", filename);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
} else {
alert("File download not supported by your browser.");
}
}
// =========================================================================
// === GAUSSIAN BEAM PHYSICS ===============================================
// =========================================================================
// This function is not currently used in the simulation path.
// If it were, M2 handling would need to be considered based on context.
function calculateQ(w_m, R_m, lambda_m) {
if (w_m <= 0) return complex(0, Infinity);
const term2 = lambda_m / (PI * w_m * w_m); // This is 1/zR for an M2=1 beam if lambda_m is physical.
const R_inv = (isFinite(R_m) && R_m !== 0) ? 1.0 / R_m : 0;
const q_inv = complex(R_inv, -term2);
return complexReciprocal(q_inv);
}
// Calculates w and R from a q-parameter and a lambda_arg.
// If q.im is zR_M2 and lambda_arg is lambda_physical_in_medium,
// then w_m returned is w_actual / sqrt(M2).
function calculateWR(q, lambda_arg) { // lambda_arg is physical wavelength in medium
if (complexMagSq(q) === 0) return { w_m: 0, R_m: 0 };
const q_inv = complexReciprocal(q);
const R_m = (q_inv.re === 0) ? Infinity : 1.0 / q_inv.re;
// q.im = zR_M2 (M2-affected Rayleigh range)
// lambda_arg = lambda_physical_in_medium = lambda_vac / n
// w_sq_term = -lambda_arg / (PI * q_inv.im)
// if q is at waist (q.re=0), q_inv.im = -1/q.im
// w_sq_term = lambda_arg * q.im / PI
// w_m = sqrt(lambda_arg * q.im / PI) -> this is w_actual / sqrt(M2)
const w_sq_term = -lambda_arg / (PI * q_inv.im);
const w_m = q_inv.im !== 0 ? Math.sqrt(Math.abs(w_sq_term)) : Infinity;
return { w_m, R_m };
}
// Calculates zR and theta from actual physical w0_m, lambda_vac, n_medium, and M2.
function calculateBeamDerivedParams(w0_m_actual, lambda_m_vac, n_medium, M2_factor) {
if (w0_m_actual <= 0 || !isFinite(w0_m_actual)) return { zR_m: 0, theta_rad: 0 };
const lambda_in_medium_m = lambda_m_vac / n_medium;
// zR_M2 = (PI * w0_actual^2) / (lambda_in_medium * M2_factor)
// = (PI * w0_actual^2 * n_medium) / (lambda_vac * M2_factor)
const zR_m = PI * w0_m_actual * w0_m_actual / (lambda_in_medium_m * M2_factor);
// theta_M2 = (lambda_in_medium * M2_factor) / (PI * w0_actual)
// = ( (lambda_vac / n_medium) * M2_factor) / (PI * w0_actual)
const theta_rad = (lambda_in_medium_m * M2_factor) / (PI * w0_m_actual);
return { zR_m, theta_rad };
}
// Finds new waist parameters (actual physical w0, z_waist_rel, zR_M2, theta_M2) from an incoming q_in.
// q_in.im is the M2-affected Rayleigh range.
function findWaistFromQ(q_in, lambda_m_vac, n_medium, M2_factor) {
const lambda_in_medium_m = lambda_m_vac / n_medium;
const z_waist_rel_m = -q_in.re; // Distance from current point to new waist
const q_waist = complex(0, q_in.im); // q-parameter at the new waist (q_waist.im is new zR_M2)
// calculateWR returns w_calc = w0_actual_new / sqrt(M2_factor)
const { w_m: w0_calc } = calculateWR(q_waist, lambda_in_medium_m);
// Correct to get the actual physical waist
const w0_actual_new_m = w0_calc * Math.sqrt(M2_factor);
// Calculate derived parameters using the actual physical waist
const { zR_m, theta_rad } = calculateBeamDerivedParams(w0_actual_new_m, lambda_m_vac, n_medium, M2_factor);
// Note: zR_m here should be equal to q_in.im, ensuring self-consistency.
return { w0_m: w0_actual_new_m, z_waist_rel_m, zR_m, theta_rad };
}
// =========================================================================
// === ABCD MATRIX LOGIC ===================================================
// =========================================================================
const identityMatrix = () => [[1, 0], [0, 1]];
const freeSpaceMatrix = (d_m) => [[1, d_m], [0, 1]];
const thinLensMatrix = (f_m) => f_m === 0 ? identityMatrix() : [[1, 0], [-1 / f_m, 1]];
const sphericalMirrorMatrix = (R_m) => R_m === 0 ? identityMatrix() : [[1, 0], [-2.0 / R_m, 1]];
const flatMirrorMatrix = () => identityMatrix();
const flatInterfaceMatrix = (n_from, n_to) => [[1, 0], [0, n_from / n_to]]; // ** NEW **
const dielectricSlabMatrix = (n1_base, n2_over_n1_ratio, width_m) => {
// This function is no longer used by the main simulation loop for slabs,
// but is kept for potential other uses or reference. The main loop
// now uses flatInterfaceMatrix and free-space propagation.
if (n2_over_n1_ratio <= 0) {
console.warn("Invalid n ratio for dielectric slab:", n2_over_n1_ratio);
return identityMatrix();
}
const B_eff_m = width_m / n2_over_n1_ratio;
return [[1, B_eff_m], [0, 1]];
};
const genericABCDMatrix = (A, B_m, C_per_m, D) => [[A, B_m], [C_per_m, D]];
function getElementMatrix(element, n1_base) {
const props = element.property;
switch (element.type) {
case 'lens': return thinLensMatrix(props.f_mm / 1000.0);
case 'mirror_spherical': return sphericalMirrorMatrix(props.R_mm / 1000.0);
case 'mirror_flat': return flatMirrorMatrix();
case 'slab_dielectric': return identityMatrix(); // Handled separately in the main loop
case 'abcd_generic': return genericABCDMatrix(props.A, props.B_mm / 1000.0, props.C_perm * 1000.0, props.D);
default:
console.warn("Unknown element type:", element.type);
return identityMatrix();
}
}
function multiplyMatrices(M1, M2) { // M = M2 * M1
const A = M2[0][0] * M1[0][0] + M2[0][1] * M1[1][0];
const B = M2[0][0] * M1[0][1] + M2[0][1] * M1[1][1];
const C = M2[1][0] * M1[0][0] + M2[1][1] * M1[1][0];
const D = M2[1][0] * M1[0][1] + M2[1][1] * M1[1][1];
return [[A, B], [C, D]];
}
function transformQ(q_in, M) { // q_out = (A*q_in + B) / (C*q_in + D)
const A = M[0][0], B = M[0][1], C = M[1][0], D = M[1][1];
const num = complexAdd(complexMul(complex(A, 0), q_in), complex(B, 0));
const den = complexAdd(complexMul(complex(C, 0), q_in), complex(D, 0));
if (complexMagSq(den) < 1e-16) {
console.warn("ABCD transformation denominator close to zero.", M, q_in);
return complex(q_in.re > 0 ? Infinity : -Infinity, 0);
}
return complexDiv(num, den);
}
// =========================================================================
// === SIMULATION CORE =====================================================
// =========================================================================
function runSimulation() {
//console.log("Running simulation..."); // Log start
// 1. Get Input Parameters & Convert to SI units (meters)
const w0_input_m = beamParams.w0_um / 1e6; // This is the actual physical waist
const z0_m = beamParams.z0_mm / 1000.0;
const lambda_vac_m = beamParams.lambda_nm / 1e9;
const M2_factor = beamParams.M2;
const n1_base_medium_idx = beamParams.n;
const plot_end_z_m = beamParams.plotRangeZ_mm / 1000.0;
// Effective wavelength in the base medium (physical wavelength)
const lambda_in_base_medium_m = lambda_vac_m / n1_base_medium_idx;
// --- Input Validation ---
if (w0_input_m <= 0 || lambda_vac_m <= 0 || M2_factor < 1.0 || n1_base_medium_idx < 1.0 || !isFinite(z0_m) || !isFinite(plot_end_z_m) || isNaN(w0_input_m) || isNaN(lambda_vac_m) || isNaN(M2_factor) || isNaN(n1_base_medium_idx) || isNaN(z0_m) || isNaN(plot_end_z_m)) {
console.error("Invalid initial beam parameters:", beamParams);
handleSimulationError("Invalid Initial Beam Parameters");
return; // Stop simulation
}
try {
// 2. Sort Elements by Position
opticalElements.sort((a, b) => a.position_mm - b.position_mm);
// 3. Initial Beam Setup & Derived Params (using actual physical w0_input_m)
const { zR_m: initial_zR_M2_m, theta_rad: initial_theta_M2_rad } = calculateBeamDerivedParams(w0_input_m, lambda_vac_m, n1_base_medium_idx, M2_factor);
const q_at_waist = complex(0, initial_zR_M2_m); // q.im is the M2-affected Rayleigh range
// 4. Determine Simulation Start/End
let min_element_pos_m = z0_m;
if (opticalElements.length > 0 && !isNaN(opticalElements[0].position_mm)) {
min_element_pos_m = Math.min(min_element_pos_m, opticalElements[0].position_mm / 1000.0);
}
const typical_zR_display = Math.max(initial_zR_M2_m, 0.01); // Avoid zero or negative zR for range calc
const user_start_m = (typeof beamParams.z_min_mm === 'number' && isFinite(beamParams.z_min_mm)) ? (beamParams.z_min_mm / 1000.0) : null;
const simulation_start_z_m = (user_start_m !== null) ? user_start_m : Math.min(0, z0_m - typical_zR_display * 2, min_element_pos_m - typical_zR_display * 0.5);
const simulation_end_z_m = plot_end_z_m;
// 5. Calculate Initial q
const dist_from_waist_to_start = simulation_start_z_m - z0_m;
let q_current = complexAdd(q_at_waist, complex(dist_from_waist_to_start, 0));
let z_current_m = simulation_start_z_m;
// 6. Prepare Data Storage
const tableData = [];
const plotData = { z: [], w: [], R: [], elementMarkers: [], waistMarkers: [] };
const N_POINTS_PER_SEGMENT = 100;
let last_z_plotted_mm = simulation_start_z_m * 1000 - 1;
tableData.push({
opticType: "Input Beam", position_mm: beamParams.z0_mm, rel_pos_mm: null,
waist_um: beamParams.w0_um, waist_pos_mm: beamParams.z0_mm,
zR_mm: initial_zR_M2_m * 1000.0, theta_mrad: initial_theta_M2_rad * 1000.0, id: 'initial'
});
plotData.waistMarkers.push({ z: z0_m, w: w0_input_m, label: `Waist 0 (${(z0_m * 1000).toFixed(1)}mm)` });
// 7. Propagate through System
let previous_element_pos_m = z0_m;
opticalElements.forEach((element, index) => {
const element_pos_m = element.position_mm / 1000.0;
if (isNaN(element_pos_m)) {
console.warn(`Skipping element ${index + 1} due to invalid position: ${element.position_mm}`);
return;
}
const dist_to_element_m = element_pos_m - z_current_m;
// A. Propagate free space *before* the element
if (dist_to_element_m > 1e-12) {
for (let i = 1; i <= N_POINTS_PER_SEGMENT; i++) {
const z_step_rel = dist_to_element_m * (i / N_POINTS_PER_SEGMENT);
const q_step = complexAdd(q_current, complex(z_step_rel, 0));
const { w_m: w_calc, R_m } = calculateWR(q_step, lambda_in_base_medium_m);
const w_actual_at_z_m = w_calc * Math.sqrt(M2_factor);
const z_abs_m = z_current_m + z_step_rel;
const z_abs_mm = z_abs_m * 1000.0;
if (z_abs_mm > last_z_plotted_mm + 1e-9 && z_abs_m <= simulation_end_z_m + 1e-9) {
plotData.z.push(z_abs_mm);
plotData.w.push(w_actual_at_z_m * 1e6);
plotData.R.push(isFinite(R_m) ? R_m * 1000.0 : (R_m > 0 ? Infinity : -Infinity));
last_z_plotted_mm = z_abs_mm;
}
}
q_current = complexAdd(q_current, complex(dist_to_element_m, 0));
z_current_m = element_pos_m;
} else {
if (dist_to_element_m < -1e-12) {
console.warn(`Simulation jump: Element ${index + 1} at ${element_pos_m * 1000}mm is before current beam position ${z_current_m * 1000}mm. Advancing position.`);
}
z_current_m = element_pos_m;
}
// B. Apply the element's transformation
if (element.type === 'slab_dielectric') {
// ** NEW: DETAILED SLAB HANDLING **
const props = element.property;
const W_m = (props && typeof props.width_mm === 'number') ? (props.width_mm / 1000.0) : 0.0;
const n_ratio = (props && typeof props.n_ratio === 'number') ? props.n_ratio : 1.0;
if (W_m > 0 && n_ratio > 0) {
const n2_medium_idx = n1_base_medium_idx * n_ratio;
const lambda_in_slab_medium_m = lambda_vac_m / n2_medium_idx;
// 1. Enter the slab interface
const M_enter = flatInterfaceMatrix(n1_base_medium_idx, n2_medium_idx);
q_current = transformQ(q_current, M_enter);
plotData.elementMarkers.push({ z: z_current_m, label: `Slab ${index + 1} Start` });
// 2. Propagate *through* the slab, plotting points inside
for (let i = 1; i <= N_POINTS_PER_SEGMENT; i++) {
const z_step_rel = W_m * (i / N_POINTS_PER_SEGMENT);
const q_step = complexAdd(q_current, complex(z_step_rel, 0));
// Use the SLAB's internal wavelength for this calculation
const { w_m: w_calc, R_m } = calculateWR(q_step, lambda_in_slab_medium_m);
const w_actual_at_z_m = w_calc * Math.sqrt(M2_factor);
const z_abs_m = z_current_m + z_step_rel;
const z_abs_mm = z_abs_m * 1000.0;
if (z_abs_mm > last_z_plotted_mm + 1e-9 && z_abs_m <= simulation_end_z_m + 1e-9) {
plotData.z.push(z_abs_mm);
plotData.w.push(w_actual_at_z_m * 1e6);
plotData.R.push(isFinite(R_m) ? R_m * 1000.0 : (R_m > 0 ? Infinity : -Infinity));
last_z_plotted_mm = z_abs_mm;
}
}
// Update q and z to be at the exit face of the slab
q_current = complexAdd(q_current, complex(W_m, 0));
z_current_m += W_m;
// 3. Exit the slab interface
const M_exit = flatInterfaceMatrix(n2_medium_idx, n1_base_medium_idx);
q_current = transformQ(q_current, M_exit);
plotData.elementMarkers.push({ z: z_current_m, label: `Slab ${index + 1} End` });
} else {
// If slab has zero width or invalid ratio, treat as identity.
plotData.elementMarkers.push({ z: z_current_m, label: `${formatElementType(element.type)} ${index + 1}` });
}
} else {
// ** ORIGINAL HANDLING FOR OTHER ELEMENTS **
const M_element = getElementMatrix(element, n1_base_medium_idx);
q_current = transformQ(q_current, M_element);
plotData.elementMarkers.push({ z: element_pos_m, label: `${formatElementType(element.type)} ${index + 1}` });
}
// C. Calculate output beam parameters after the element interaction
const { w0_m: w0_actual_new_m, z_waist_rel_m: z_waist_rel_new_m, zR_m: zR_M2_new_m, theta_rad: theta_M2_new_rad } = findWaistFromQ(q_current, lambda_vac_m, n1_base_medium_idx, M2_factor);
const waist_abs_pos_m = z_current_m + z_waist_rel_new_m;
// D. Add element data to the table
const rel_pos_mm = (element_pos_m - previous_element_pos_m) * 1000.0;
tableData.push({
opticType: formatElementType(element.type), position_mm: element.position_mm, rel_pos_mm: rel_pos_mm,
properties: formatElementProperties(element),
waist_um: w0_actual_new_m * 1e6,
waist_pos_mm: waist_abs_pos_m * 1000.0,
zR_mm: zR_M2_new_m * 1000.0, theta_mrad: theta_M2_new_rad * 1000.0, id: element.id
});
// E. Add waist markers for plots
plotData.waistMarkers.push({ z: waist_abs_pos_m, w: w0_actual_new_m, label: `Waist ${index + 1}` });
// F. Update position tracker for next relative calculation
previous_element_pos_m = element_pos_m;
});
// 8. Propagate Final Segment
const final_dist_m = simulation_end_z_m - z_current_m;
if (final_dist_m > 1e-12) {
for (let i = 1; i <= N_POINTS_PER_SEGMENT; i++) {
const z_step_rel = final_dist_m * (i / N_POINTS_PER_SEGMENT);
const q_step = complexAdd(q_current, complex(z_step_rel, 0));
const { w_m: w_calc, R_m } = calculateWR(q_step, lambda_in_base_medium_m);
const w_actual_at_z_m = w_calc * Math.sqrt(M2_factor);
const z_abs_m = z_current_m + z_step_rel;
const z_abs_mm = z_abs_m * 1000.0;
if (z_abs_mm > last_z_plotted_mm + 1e-9 && z_abs_m <= simulation_end_z_m + 1e-9) {
plotData.z.push(z_abs_mm);
plotData.w.push(w_actual_at_z_m * 1e6);
plotData.R.push(isFinite(R_m) ? R_m * 1000.0 : (R_m > 0 ? Infinity : -Infinity));
last_z_plotted_mm = z_abs_mm;
}
}
}
// 9. Store and Update UI
latestPlotData = { ...plotData };
updateTable(tableData);
updatePlots(plotData);
if (typeof CanvasController !== 'undefined' && CanvasController.draw) {
CanvasController.draw(latestPlotData);
} else {
console.warn("CanvasController not ready for drawing.");
}
} catch (error) {
console.error("Error during simulation calculation:", error);
handleSimulationError(`Calculation Error: ${error.message}`);
} finally {
// console.log("Simulation finished."); // Log end
}
}
function handleSimulationError(message) {
alert(message);
// Clear plots
if (plotWDiv && typeof Plotly !== 'undefined') Plotly.purge(plotWDiv);
if (plotRDiv && typeof Plotly !== 'undefined') Plotly.purge(plotRDiv);
plotsInitialized.w = false;
plotsInitialized.r = false;
// Clear or show error in table
if (opticsTableBody) {
opticsTableBody.innerHTML = `<tr><td colspan="9" style="color: red; text-align: center;">Error: ${message}</td></tr>`;
// Attempt to redraw with current data for user fixing
try {
const currentTableData = [{
opticType: "Input Beam", position_mm: beamParams.z0_mm, rel_pos_mm: null,
waist_um: beamParams.w0_um, waist_pos_mm: beamParams.z0_mm,
zR_mm: null, theta_mrad: null, id: 'initial'
}, ...opticalElements.map((el, index) => ({
opticType: formatElementType(el.type), position_mm: el.position_mm, rel_pos_mm: '---',
properties: formatElementProperties(el),
waist_um: null, waist_pos_mm: null, zR_mm: null, theta_mrad: null, id: el.id
}))];
updateTable(currentTableData);
} catch (e) {
console.error("Error trying to redraw table during error handling:", e);
opticsTableBody.innerHTML = `<tr><td colspan="9" style="color: red; text-align: center;">Error: ${message}. Failed to redraw table.</td></tr>`;
}
}
// Clear canvas data
if (typeof CanvasController !== 'undefined' && CanvasController.draw) {
CanvasController.draw(null); // Clear canvas
}
latestPlotData = null;
}
// =========================================================================
// === UI UPDATE FUNCTIONS =================================================
// =========================================================================
function updateTable(data) {
if (!opticsTableBody) return;
opticsTableBody.innerHTML = ''; // Clear existing rows
const initialBeamData = data.find(item => item.id === 'initial');
if (initialBeamData) {
addTableRow(initialBeamData, 0); // Index 0 for initial beam
}
const elementData = data.filter(item => item.id !== 'initial');
// Ensure elements are sorted by their actual position *before* adding rows
elementData.sort((a, b) => a.position_mm - b.position_mm);
elementData.forEach((item, index) => {
addTableRow(item, index + 1); // Index starts from 1 for elements
});
}
function addTableRow(item, index) {
if (!opticsTableBody) return;
const row = opticsTableBody.insertRow();
row.insertCell().textContent = item.opticType;
// --- Position Cell (z0 or element position) ---
const posCell = row.insertCell();
if (item.id === 'initial') {
posCell.appendChild(createTableInput('initial', 'z0_mm', item.position_mm, 'any', null, "Position of the initial beam waist (z₀) (mm)."));
} else {
const posInput = createTableInput(item.id, 'position_mm', item.position_mm, 'any', null, `Position of this '${formatElementType(item.opticType)}' (mm).`);
posInput.dataset.index = index;
posCell.appendChild(posInput);
}
// --- Relative Position Cell ---
const relPosCell = row.insertCell();
if (item.id === 'initial') {
relPosCell.textContent = '---';
} else {
const relPosInput = document.createElement('input');
relPosInput.type = 'number';
relPosInput.classList.add('rel-pos-input');
const relPosValue = parseFloat(item.rel_pos_mm);
if (item.rel_pos_mm !== null && !isNaN(relPosValue)) {
const dummyInput = createTableInput('dummy', 'rel_pos_mm', relPosValue, 'any');
relPosInput.value = relPosValue.toFixed(dummyInput.precision);
} else {
relPosInput.value = '';
}
relPosInput.step = 'any';
relPosInput.dataset.id = item.id;
relPosInput.dataset.index = index;
relPosInput.title = "Distance from the previous element/waist (mm). Edit to update absolute position.";
relPosInput.addEventListener('change', handleTableRelPosEdit);
relPosCell.appendChild(relPosInput);
}
// --- Properties Cell ---
const propCell = row.insertCell();
propCell.dataset.id = item.id;
propCell.innerHTML = '';
if (item.id === 'initial') {
propCell.appendChild(document.createTextNode('λ='));
propCell.appendChild(createTableInput('initial', 'lambda_nm', beamParams.lambda_nm, 'any', 1, "Wavelength of the light in vacuum."));
propCell.appendChild(document.createTextNode('nm'));
propCell.appendChild(document.createElement('br'));
propCell.appendChild(document.createTextNode('M²='));
propCell.appendChild(createTableInput('initial', 'M2', beamParams.M2, 0.1, 1.0, "Beam quality factor (M² ≥ 1). Represents deviation from ideal Gaussian."));
propCell.appendChild(document.createTextNode(', n₁='));
propCell.appendChild(createTableInput('initial', 'n', beamParams.n, 0.01, 1.0, "Refractive index of the base medium (propagates between elements)."));
propCell.appendChild(document.createElement('br'));
propCell.appendChild(document.createTextNode('Plot End Z='));
propCell.appendChild(createTableInput('initial', 'plotRangeZ_mm', beamParams.plotRangeZ_mm, 'any', null, "Max z-position for plots (mm)."));
propCell.appendChild(document.createTextNode('mm'));
propCell.appendChild(document.createElement('br'));
propCell.appendChild(document.createTextNode('Plot Start Z='));
propCell.appendChild(createTableInput('initial', 'z_min_mm', beamParams.z_min_mm, 'any', null, "Start z-position for plots (mm)."));
propCell.appendChild(document.createTextNode('mm'));
} else {
const element = opticalElements.find(el => el.id === item.id);
if (element && element.property) {
try {
switch (element.type) {
case 'lens':
propCell.appendChild(document.createTextNode('f='));
propCell.appendChild(createTableInput(item.id, 'f_mm', element.property.f_mm, 'any', null, "Focal length (mm). Positive for converging, negative for diverging."));
propCell.appendChild(document.createTextNode('mm'));
break;
case 'mirror_spherical':
propCell.appendChild(document.createTextNode('R='));
propCell.appendChild(createTableInput(item.id, 'R_mm', element.property.R_mm, 'any', null, "Radius of curvature (mm). R>0 concave (f>0), R<0 convex (f<0)."));
propCell.appendChild(document.createTextNode('mm'));
break;
case 'mirror_flat': propCell.textContent = '---'; break;
case 'slab_dielectric':
propCell.appendChild(document.createTextNode('n₂/n₁='));
propCell.appendChild(createTableInput(item.id, 'n_ratio', element.property.n_ratio, 0.01, 0.01, "Ratio of slab's refractive index (n₂) to surrounding medium's index (n₁)."));
propCell.appendChild(document.createTextNode(', W='));
propCell.appendChild(createTableInput(item.id, 'width_mm', element.property.width_mm, 0.1, 0, "Physical thickness of the slab (mm)."));
propCell.appendChild(document.createTextNode('mm'));
break;
case 'abcd_generic':
propCell.appendChild(document.createTextNode('A='));
propCell.appendChild(createTableInput(item.id, 'A', element.property.A, 0.1));
propCell.appendChild(document.createTextNode(', B='));
propCell.appendChild(createTableInput(item.id, 'B_mm', element.property.B_mm, 'any'));
propCell.appendChild(document.createTextNode('mm'));
propCell.appendChild(document.createElement('br'));
propCell.appendChild(document.createTextNode('C='));
propCell.appendChild(createTableInput(item.id, 'C_perm', element.property.C_perm, 0.0001));
propCell.appendChild(document.createTextNode('/mm, D='));
propCell.appendChild(createTableInput(item.id, 'D', element.property.D, 0.1));
break;
default: propCell.textContent = formatElementProperties(element);
}
} catch (e) {
console.error("Error creating property input for element:", element, e);
propCell.textContent = "Error";
}
} else {
propCell.textContent = item.properties || '---';
if (!element) console.warn("Could not find element for table row:", item.id);
}
}
// --- Waist (µm) Cell ---
const waistCell = row.insertCell();
if (item.id === 'initial') {
waistCell.appendChild(createTableInput('initial', 'w0_um', item.waist_um, 0.1, 0.1, "Initial beam waist radius (1/e² intensity radius) at z₀"));
} else {
waistCell.textContent = formatTableValue(item.waist_um, 1, 4);
}
// --- Waist Pos, Rayleigh, Divergence Cells ---
row.insertCell().textContent = formatTableValue(item.waist_pos_mm, 2, 4);
// --- Modified Rayleigh Range Cell (Editable for Input Beam) ---
const zRCell = row.insertCell();
if (item.id === 'initial') {
// Make Rayleigh range editable for the input beam
zRCell.appendChild(createTableInput('initial', 'zR_mm_input', item.zR_mm, 0.1, 0.001, "Rayleigh Range (mm). Editing this updates the Waist Radius."));
} else {
zRCell.textContent = formatTableValue(item.zR_mm, 2, 4);
}
row.insertCell().textContent = formatTableValue(item.theta_mrad, 2, 4);
// --- Action Cell ---
const actionCell = row.insertCell();
if (item.id !== 'initial') {
const removeBtn = document.createElement('button');
removeBtn.textContent = 'Remove';
removeBtn.classList.add('remove-btn');
removeBtn.dataset.id = item.id;
removeBtn.addEventListener('click', handleRemoveElement);
actionCell.appendChild(removeBtn);
} else {
actionCell.textContent = '---';
}
}
function createTableInput(id, propertyName, value, step, minValue = null, description = '') {
const input = document.createElement('input');
input.type = 'number';
const allowsDecimals = step === 'any' || (typeof step === 'number' && step < 1 && step > 0);
let precision;
switch (propertyName) {
case 'M2': precision = 1; break;
case 'n': case 'n_ratio': precision = 2; break;
case 'C_perm': precision = 4; break;
case 'w0_um': case 'lambda_nm': precision = 1; break;
case 'zR_mm_input': precision = 2; break; // New case for Rayleigh Input
default: precision = allowsDecimals ? 2 : 0; break;
}
input.precision = precision;
let parsedValue = parseFloat(value);
if (isNaN(parsedValue)) {
if (propertyName === 'M2' || propertyName === 'n') parsedValue = 1.0;
else if (propertyName === 'lambda_nm') parsedValue = beamParams.lambda_nm || 1064;
else if (propertyName === 'plotRangeZ_mm') parsedValue = beamParams.plotRangeZ_mm || 500;
else if (propertyName === 'z_min_mm') parsedValue = beamParams.z_min_mm ?? -20;
else parsedValue = 0;
}
if (minValue !== null && parsedValue < minValue) {
parsedValue = minValue;
}
input.value = parsedValue.toFixed(precision);
input.step = step === null ? 'any' : String(step); // Ensure step is string for 'any'
if (minValue !== null) input.min = String(minValue);
input.dataset.id = id;
input.dataset.property = propertyName;
if (description) input.title = description;
input.addEventListener('change', handleTableEdit);
return input;
}
function updatePlots(plotData) {
if (!plotWDiv || !plotRDiv || typeof Plotly === 'undefined') return;
if (!plotData || !plotData.z || plotData.z.length === 0) {
Plotly.purge(plotWDiv);
Plotly.purge(plotRDiv);
plotsInitialized.w = false;
plotsInitialized.r = false;
return;
}
const showElements = showElementsCheck ? showElementsCheck.checked : true;
const showWaists = showWaistsCheck ? showWaistsCheck.checked : true;
const largeFinite = 1e9;
// --- Calculate Full Data Ranges ---
const allZValues = plotData.z;
let minZ = allZValues.length > 0 ? Math.min(...allZValues) : (beamParams.z0_mm - 100);
let maxZ = Math.max(allZValues.length > 0 ? Math.max(...allZValues) : (beamParams.z0_mm + 100), beamParams.plotRangeZ_mm);
minZ = isNaN(minZ) ? -100 : minZ;
maxZ = isNaN(maxZ) ? 500 : maxZ;
if (minZ >= maxZ) maxZ = minZ + 100; // Ensure maxZ > minZ
const xRangeBuffer = (maxZ - minZ) * 0.05 || 10;
const fullXRange = [minZ - xRangeBuffer, maxZ + xRangeBuffer];
const allWValues = plotData.w.filter(isFinite);
let maxAbsW = allWValues.length > 0 ? Math.max(...allWValues.map(Math.abs)) : (beamParams.w0_um || 100);
maxAbsW = (maxAbsW <= 0 || isNaN(maxAbsW)) ? 100 : maxAbsW; // Ensure positive, non-NaN maxAbsW
const wRangeBuffer = maxAbsW * 0.10 || 1;
const fullWRangeY = [-maxAbsW - wRangeBuffer, maxAbsW + wRangeBuffer];
const plotR_vals = plotData.R.map(r => (!isFinite(r)) ? (r > 0 ? largeFinite : -largeFinite) : r);
let minR_calc = plotR_vals.length > 0 ? Math.min(...plotR_vals) : -1000;
let maxR_calc = plotR_vals.length > 0 ? Math.max(...plotR_vals) : 1000;
minR_calc = isNaN(minR_calc) ? -1000 : minR_calc;
maxR_calc = isNaN(maxR_calc) ? 1000 : maxR_calc;
if (minR_calc >= maxR_calc) maxR_calc = minR_calc + 1000; // Ensure maxR > minR
let rRangeBuffer = (maxR_calc - minR_calc) * 0.10;
if (Math.abs(minR_calc) >= largeFinite || Math.abs(maxR_calc) >= largeFinite) rRangeBuffer = Math.max(100, Math.abs(maxR_calc - minR_calc) * 0.02);
if (maxR_calc === minR_calc) rRangeBuffer = Math.max(100, Math.abs(maxR_calc * 0.2)); // Handle case where all R values are the same
rRangeBuffer = isFinite(rRangeBuffer) ? rRangeBuffer : 100; // Fallback if rRangeBuffer is not finite
const fullRRangeY = [minR_calc - rRangeBuffer, maxR_calc + rRangeBuffer];
// --- Determine Plot Ranges (Preserve Zoom) ---
const targetWRangeX = plotsInitialized.w ? (plotWDiv.layout?.xaxis?.range || fullXRange) : fullXRange;
const targetWRangeY = plotsInitialized.w ? (plotWDiv.layout?.yaxis?.range || fullWRangeY) : fullWRangeY;
const targetRRangeX = plotsInitialized.r ? (plotRDiv.layout?.xaxis?.range || targetWRangeX) : targetWRangeX;
const targetRRangeY = plotsInitialized.r ? (plotRDiv.layout?.yaxis?.range || fullRRangeY) : fullRRangeY;
// --- Waist Plot ---
const traceW = { x: plotData.z, y: plotData.w, mode: 'lines', name: 'W', line: { color: 'blue' } };
const traceW_neg = { x: plotData.z, y: plotData.w.map(w_val => -w_val), mode: 'lines', name: '-W', line: { color: 'blue' }, showlegend: false };
const layoutW = {
xaxis: { title: 'z (mm)', range: targetWRangeX, automargin: true, titlefont: { size: 10 } },
yaxis: { title: 'w (µm)', range: targetWRangeY, automargin: true, titlefont: { size: 10 } },
margin: { l: 35, r: 10, t: 5, b: 25 }, shapes: [], annotations: [], hovermode: 'x unified', showlegend: false
};
if (showElements && plotData.elementMarkers) {
const yRangeW = layoutW.yaxis.range; // Use y-axis range from the current plot
plotData.elementMarkers.forEach(m => {
if (!m || isNaN(m.z)) return; // m.z is in meters
const xPosMm = m.z * 1000; // Convert to mm for plotting
if (xPosMm >= targetWRangeX[0] && xPosMm <= targetWRangeX[1]) {
layoutW.shapes.push({ type: 'line', x0: xPosMm, y0: yRangeW[0], x1: xPosMm, y1: yRangeW[1], line: { color: 'red', width: 1, dash: 'dot' } });
layoutW.annotations.push({ x: xPosMm, y: yRangeW[1] * 0.98, text: m.label || '', showarrow: false, xanchor: 'left', yanchor: 'top', font: { size: 8, color: 'red' } });
}
});
}
if (showWaists && plotData.waistMarkers) {
//const xRangeW = layoutW.xaxis.range; // Not needed if yRangeW is used for conditional check
plotData.waistMarkers.forEach(m => { // m.z and m.w are in meters
if (!m || isNaN(m.z) || isNaN(m.w)) return;
const xPosMm = m.z * 1000; // Convert to mm
if (xPosMm >= targetWRangeX[0] && xPosMm <= targetWRangeX[1]) {
const wMarkUm = m.w * 1e6; // Convert waist marker w to um
layoutW.shapes.push({ type: 'line', x0: xPosMm, y0: -wMarkUm, x1: xPosMm, y1: wMarkUm, line: { color: 'green', width: 1.5, dash: 'solid' } });
layoutW.annotations.push({ x: xPosMm, y: 0, text: (m.label || '') + ` (${xPosMm.toFixed(1)}mm)`, showarrow: true, arrowhead: 2, ax: -20, ay: -20, font: { size: 8, color: 'green' } });
}
});
}
try { Plotly.react(plotWDiv, [traceW, traceW_neg], layoutW); plotsInitialized.w = true; }
catch (e) { console.error("Error rendering Waist plot:", e); Plotly.purge(plotWDiv); plotsInitialized.w = false; }
// --- RoC Plot ---
const traceR = { x: plotData.z, y: plotR_vals, mode: 'lines', name: 'R', line: { color: 'purple' } };
const layoutR = {
xaxis: { title: 'z (mm)', range: targetRRangeX, automargin: true, titlefont: { size: 10 } },
yaxis: { title: 'R (mm)', range: targetRRangeY, automargin: true, titlefont: { size: 10 } },
margin: { l: 35, r: 10, t: 5, b: 25 }, shapes: [], annotations: [], hovermode: 'x unified', showlegend: false
};
if (showElements && plotData.elementMarkers) {
const yRangeR = layoutR.yaxis.range; // Use y-axis range from current plot
plotData.elementMarkers.forEach(m => {
if (!m || isNaN(m.z)) return; // m.z is in meters
const xPosMm = m.z * 1000; // Convert to mm
if (xPosMm >= targetRRangeX[0] && xPosMm <= targetRRangeX[1]) {
layoutR.shapes.push({ type: 'line', x0: xPosMm, y0: yRangeR[0], x1: xPosMm, y1: yRangeR[1], line: { color: 'red', width: 1, dash: 'dot' } });
// Annotations for RoC plot might be too busy, often omitted
}
});
}
try { Plotly.react(plotRDiv, [traceR], layoutR); plotsInitialized.r = true; }
catch (e) { console.error("Error rendering RoC plot:", e); Plotly.purge(plotRDiv); plotsInitialized.r = false; }
}
// =========================================================================
// === CSV EXPORT ==========================================================
// =========================================================================
function downloadCSV(filename, dataRows) {
if (!dataRows || dataRows.length <= 1) {
alert("No data available to export."); return;
}
const csvContent = dataRows.map(row => row.join(",")).join("\n");
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const link = document.createElement("a");
if (link.download !== undefined) {
const url = URL.createObjectURL(blob);
link.setAttribute("href", url);
link.setAttribute("download", filename);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
} else {
alert("CSV download not supported by your browser.");
}
}
function handleExportW() {
if (!latestPlotData || !latestPlotData.z || !latestPlotData.w) { alert("Simulation data not available yet."); return; }
const rows = [["z_mm", "w_um"]];
for (let i = 0; i < latestPlotData.z.length; i++) {
if (latestPlotData.z[i] !== undefined && latestPlotData.w[i] !== undefined) {
rows.push([formatForCSV(latestPlotData.z[i]), formatForCSV(latestPlotData.w[i])]);
}
}
downloadCSV("beam_waist_data.csv", rows);
}
function handleExportR() {
if (!latestPlotData || !latestPlotData.z || !latestPlotData.R) { alert("Simulation data not available yet."); return; }
const rows = [["z_mm", "R_mm"]];
for (let i = 0; i < latestPlotData.z.length; i++) {
if (latestPlotData.z[i] !== undefined && latestPlotData.R[i] !== undefined) {
rows.push([formatForCSV(latestPlotData.z[i]), formatForCSV(latestPlotData.R[i])]);
}
}
downloadCSV("beam_roc_data.csv", rows);
}
// =========================================================================
// === XML EXPORT/IMPORT ===================================================
// =========================================================================
function handleExportSetup() {
console.log("Exporting setup...");
try {
const xmlDoc = document.implementation.createDocument(null, "opticalSystem", null);
const root = xmlDoc.documentElement;
const beamNode = xmlDoc.createElement("beamParameters");
for (const key in beamParams) {
if (Object.hasOwnProperty.call(beamParams, key)) {
beamNode.setAttribute(key, String(beamParams[key]));
}
}
root.appendChild(beamNode);
const elementsNode = xmlDoc.createElement("elements");
opticalElements.forEach(element => {
const elementNode = xmlDoc.createElement("element");
elementNode.setAttribute("type", element.type);
elementNode.setAttribute("position_mm", String(element.position_mm));
// ID is not saved, it's recreated on import for uniqueness.
if (element.property && Object.keys(element.property).length > 0) {
const propertyNode = xmlDoc.createElement("property");
for (const propKey in element.property) {
if (Object.hasOwnProperty.call(element.property, propKey)) {
propertyNode.setAttribute(propKey, String(element.property[propKey]));
}
}
elementNode.appendChild(propertyNode);
}
elementsNode.appendChild(elementNode);
});
root.appendChild(elementsNode);
const serializer = new XMLSerializer();
const xmlString = '<?xml version="1.0" encoding="UTF-8"?>\n' + serializer.serializeToString(xmlDoc);
downloadFile("gaussian_beam_setup.xml", xmlString, 'application/xml;charset=utf-8;');
console.log("Setup exported successfully.");
} catch (error) {
console.error("Error exporting setup:", error);
alert("Error exporting setup. See console for details.");
}
}
function handleImportSetupTrigger() {
if (importSetupInput) {
importSetupInput.click();
} else {
console.error("Import file input not found.");
alert("Import feature error: File input element missing.");
}
}
function handleImportSetupFileSelect(event) {
const file = event.target.files[0];
if (!file) {
console.log("No file selected for import.");
return;
}
if (!file.name.toLowerCase().endsWith('.xml')) {
alert("Invalid file type. Please select an XML file (.xml).");
event.target.value = null;
return;
}
console.log("Importing setup from:", file.name);
const reader = new FileReader();
reader.onload = function(e) {
const xmlString = e.target.result;
try {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, "application/xml");
const parserError = xmlDoc.getElementsByTagName("parsererror");
if (parserError.length > 0) {
console.error("XML Parsing Error:", parserError[0].textContent);
throw new Error("Failed to parse XML file. Check format and validity.");
}
const root = xmlDoc.documentElement;
if (!root || root.tagName !== 'opticalSystem') {
throw new Error("Invalid XML format: Missing <opticalSystem> root element.");
}
const beamNode = root.querySelector("beamParameters");
if (!beamNode) {
throw new Error("Invalid XML format: Missing <beamParameters> element.");
}
const importedBeamParams = {};
const expectedBeamParams = ['w0_um', 'z0_mm', 'lambda_nm', 'M2', 'n', 'plotRangeZ_mm'];
let missingBeamParam = null;
for (const key of expectedBeamParams) {
const value = beamNode.getAttribute(key);
if (value === null) {
missingBeamParam = key;
break;
}
const numValue = parseFloat(value);
if (isNaN(numValue)) {
throw new Error(`Invalid value for beam parameter '${key}': "${value}". Must be a number.`);
}
let clampedValue = numValue;
switch (key) {
case 'w0_um': clampedValue = Math.max(0.1, numValue); break;
case 'lambda_nm': clampedValue = Math.max(1, numValue); break;
case 'M2': clampedValue = Math.max(1.0, numValue); break;
case 'n': clampedValue = Math.max(1.0, numValue); break;
}
if (clampedValue !== numValue) console.warn(`Imported beam param ${key} value clamped from ${numValue} to ${clampedValue}`);
importedBeamParams[key] = clampedValue;
}
if (missingBeamParam) {
throw new Error(`Invalid XML format: Missing required beam parameter attribute '${missingBeamParam}'.`);
}
const elementsNode = root.querySelector("elements");
if (!elementsNode) {
throw new Error("Invalid XML format: Missing <elements> element.");
}
const importedElements = [];
const elementNodes = elementsNode.querySelectorAll("element");
elementNodes.forEach((elNode, index) => {
const type = elNode.getAttribute("type");
const posStr = elNode.getAttribute("position_mm");
if (!type) throw new Error(`Element ${index + 1}: Missing 'type' attribute.`);
if (posStr === null) throw new Error(`Element ${index + 1} (type ${type}): Missing 'position_mm' attribute.`);
const position_mm = parseFloat(posStr);
if (isNaN(position_mm)) throw new Error(`Element ${index + 1} (type ${type}): Invalid 'position_mm' value "${posStr}".`);
const element = {
type: type,