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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
"""
TPD/TPR Simulator and Dataset Generation for Heterogeneous Catalysis.

Implements 6 temperature-programmed desorption/reaction mechanisms:
  1. FirstOrder      - first-order desorption (Polanyi-Wigner, n=1)
  2. SecondOrder     - second-order/recombinative desorption (n=2)
  3. LH_Surface      - Langmuir-Hinshelwood bimolecular surface reaction
  4. MvK             - Mars-van Krevelen lattice oxygen mechanism
  5. FirstOrderCovDep - first-order with coverage-dependent activation energy
  6. DiffLimited     - diffusion-limited desorption from porous materials

Each mechanism is solved as an ODE with a linear temperature ramp T = T0 + beta*t.
Supports multi-heating-rate generation (analogous to multi-scan-rate CVs in EC).

Data format mirrors the electrochemistry pipeline for compatibility.
"""

import os
import sys
import json
import argparse
import numpy as np
from tqdm import tqdm
from multiprocessing import Pool, cpu_count

import scipy.integrate


# =============================================================================
# Mechanism registry
# =============================================================================

TPD_MECHANISM_LIST = [
    'FirstOrder', 'SecondOrder', 'LH_Surface', 'MvK',
    'FirstOrderCovDep', 'DiffLimited',
    'TwoSite', 'ZerothOrder', 'PrecursorMediated',
    'Dissociative', 'EleyRideal', 'BimolecularCovDep', 'ActivatedAdsorption',
]
TPD_MECHANISM_TO_ID = {m: i for i, m in enumerate(TPD_MECHANISM_LIST)}

TPD_MECHANISM_PARAMS = {
    'FirstOrder': {
        'names': ['Ed', 'log10(nu)', 'theta_0'],
        'dim': 3,
    },
    'SecondOrder': {
        'names': ['Ed', 'log10(nu)', 'theta_0'],
        'dim': 3,
    },
    'LH_Surface': {
        'names': ['Ea', 'log10(nu)', 'theta_A0', 'theta_B0'],
        'dim': 4,
    },
    'MvK': {
        'names': ['Ea_red', 'Ea_reox', 'log10(nu_red)', 'theta_O0'],
        'dim': 4,
    },
    'FirstOrderCovDep': {
        'names': ['Ed0', 'alpha_cov', 'log10(nu)', 'theta_0'],
        'dim': 4,
    },
    'DiffLimited': {
        'names': ['Ed', 'log10(nu)', 'log10(D0)', 'E_diff', 'theta_0'],
        'dim': 5,
    },
    'TwoSite': {
        'names': ['Ed1', 'Ed2', 'log10(nu)', 'f_site1', 'theta_0'],
        'dim': 5,
    },
    'ZerothOrder': {
        'names': ['Ed', 'log10(nu)', 'n_layers'],
        'dim': 3,
    },
    'PrecursorMediated': {
        'names': ['Ed', 'Ep', 'log10(nu)', 'theta_0'],
        'dim': 4,
    },
    'Dissociative': {
        'names': ['Ed0', 'log10(nu)', 'delta', 'theta_0'],
        'dim': 4,
    },
    'EleyRideal': {
        'names': ['Ed', 'log10(nu_des)', 'Ea_ER', 'log10(k_ER)', 'theta_0'],
        'dim': 5,
    },
    'BimolecularCovDep': {
        'names': ['Ea', 'log10(nu)', 'omega', 'theta_A0', 'theta_B0'],
        'dim': 5,
    },
    'ActivatedAdsorption': {
        'names': ['Ed', 'log10(nu_des)', 'Ea_ads', 'log10(nu_ads)', 'theta_0'],
        'dim': 5,
    },
}


# =============================================================================
# Simulation constants
# =============================================================================

# We work in dimensionless temperature units: T_dimless = T_physical / T_ref.
# Activation energies are also dimensionless: Ed_dimless = Ed_physical / (R * T_ref).
# With T_ref = 1 K conceptually, T values are just Kelvin and Ed = Ea/(R) in K.
# In practice we use T in Kelvin directly and Ed in units of K (= Ea/R).
# This avoids carrying R everywhere.

T_REF = 1.0  # Reference temperature (K); energies in K (= Ea/R)
N_POINTS_DEFAULT = 500  # time steps per TPD curve


# =============================================================================
# ODE Simulators
# =============================================================================

def run_tpd_first_order(Ed, nu, theta_0, beta, T_start, T_end,
                        n_points=N_POINTS_DEFAULT):
    """
    First-order desorption: d(theta)/dt = -nu * theta * exp(-Ed / T(t))
    where T(t) = T_start + beta * t.

    Parameters
    ----------
    Ed : float
        Dimensionless desorption energy (= Ea / R, in Kelvin).
    nu : float
        Pre-exponential factor (s^-1).
    theta_0 : float
        Initial fractional surface coverage [0, 1].
    beta : float
        Heating rate (K/s).
    T_start, T_end : float
        Temperature ramp range (K).
    n_points : int
        Number of output points.

    Returns
    -------
    dict with 'temperature', 'rate', 'time', 'coverage', 'params'
    """
    t_end = (T_end - T_start) / beta
    t_eval = np.linspace(0, t_end, n_points)

    def rhs(t, y):
        theta = y[0]
        T = T_start + beta * t
        if T < 1.0:
            T = 1.0
        rate = -nu * theta * np.exp(-Ed / T)
        return [rate]

    sol = scipy.integrate.solve_ivp(
        rhs, [0, t_end], [theta_0], t_eval=t_eval,
        method='BDF', rtol=1e-8, atol=1e-10, max_step=t_end / 50,
    )

    theta = sol.y[0]
    temperature = T_start + beta * sol.t
    rate = nu * np.maximum(theta, 0) * np.exp(-Ed / np.maximum(temperature, 1.0))

    return {
        'temperature': temperature.astype(np.float32),
        'rate': rate.astype(np.float32),
        'time': sol.t.astype(np.float32),
        'coverage': theta.astype(np.float32),
        'params': {
            'mechanism': 'FirstOrder',
            'Ed': float(Ed),
            'nu': float(nu),
            'theta_0': float(theta_0),
            'beta': float(beta),
            'T_start': float(T_start),
            'T_end': float(T_end),
        },
    }


def run_tpd_second_order(Ed, nu, theta_0, beta, T_start, T_end,
                         n_points=N_POINTS_DEFAULT):
    """
    Second-order (recombinative) desorption:
        d(theta)/dt = -nu * theta^2 * exp(-Ed / T(t))

    Peak position shifts to lower T with increasing theta_0 (key diagnostic).
    """
    t_end = (T_end - T_start) / beta
    t_eval = np.linspace(0, t_end, n_points)

    def rhs(t, y):
        theta = y[0]
        T = T_start + beta * t
        if T < 1.0:
            T = 1.0
        rate = -nu * theta ** 2 * np.exp(-Ed / T)
        return [rate]

    sol = scipy.integrate.solve_ivp(
        rhs, [0, t_end], [theta_0], t_eval=t_eval,
        method='BDF', rtol=1e-8, atol=1e-10, max_step=t_end / 50,
    )

    theta = sol.y[0]
    temperature = T_start + beta * sol.t
    rate = nu * np.maximum(theta, 0) ** 2 * np.exp(-Ed / np.maximum(temperature, 1.0))

    return {
        'temperature': temperature.astype(np.float32),
        'rate': rate.astype(np.float32),
        'time': sol.t.astype(np.float32),
        'coverage': theta.astype(np.float32),
        'params': {
            'mechanism': 'SecondOrder',
            'Ed': float(Ed),
            'nu': float(nu),
            'theta_0': float(theta_0),
            'beta': float(beta),
            'T_start': float(T_start),
            'T_end': float(T_end),
        },
    }


def run_tpd_lh_surface(Ea, nu, theta_A0, theta_B0, beta, T_start, T_end,
                       n_points=N_POINTS_DEFAULT):
    """
    Langmuir-Hinshelwood bimolecular surface reaction:
        A(ads) + B(ads) -> products
        d(theta_A)/dt = -nu * theta_A * theta_B * exp(-Ea / T)
        d(theta_B)/dt = -nu * theta_A * theta_B * exp(-Ea / T)
        rate = nu * theta_A * theta_B * exp(-Ea / T)
    """
    t_end = (T_end - T_start) / beta
    t_eval = np.linspace(0, t_end, n_points)

    def rhs(t, y):
        theta_A, theta_B = y
        T = T_start + beta * t
        if T < 1.0:
            T = 1.0
        r = -nu * theta_A * theta_B * np.exp(-Ea / T)
        return [r, r]

    sol = scipy.integrate.solve_ivp(
        rhs, [0, t_end], [theta_A0, theta_B0], t_eval=t_eval,
        method='BDF', rtol=1e-8, atol=1e-10, max_step=t_end / 50,
    )

    theta_A = sol.y[0]
    theta_B = sol.y[1]
    temperature = T_start + beta * sol.t
    rate = nu * np.maximum(theta_A, 0) * np.maximum(theta_B, 0) * \
           np.exp(-Ea / np.maximum(temperature, 1.0))

    return {
        'temperature': temperature.astype(np.float32),
        'rate': rate.astype(np.float32),
        'time': sol.t.astype(np.float32),
        'coverage': np.stack([theta_A, theta_B], axis=-1).astype(np.float32),
        'params': {
            'mechanism': 'LH_Surface',
            'Ea': float(Ea),
            'nu': float(nu),
            'theta_A0': float(theta_A0),
            'theta_B0': float(theta_B0),
            'beta': float(beta),
            'T_start': float(T_start),
            'T_end': float(T_end),
        },
    }


def run_tpd_mvk(Ea_red, Ea_reox, nu_red, theta_O0, beta, T_start, T_end,
                 n_points=N_POINTS_DEFAULT):
    """
    Mars-van Krevelen lattice oxygen mechanism:
        Reduction:   rate_red   = nu_red * theta_O * exp(-Ea_red / T)
        Reoxidation: rate_reox  = nu_reox * (1 - theta_O) * exp(-Ea_reox / T)
        d(theta_O)/dt = rate_reox - rate_red
        Observable rate = rate_red (consumption of lattice oxygen)

    nu_reox is fixed at nu_red * 0.1 to reduce parameter count while
    keeping the two-process competition that creates the distinctive MvK
    peak shapes.
    """
    nu_reox = nu_red * 0.1
    t_end = (T_end - T_start) / beta
    t_eval = np.linspace(0, t_end, n_points)

    def rhs(t, y):
        theta_O = y[0]
        T = T_start + beta * t
        if T < 1.0:
            T = 1.0
        r_red = nu_red * theta_O * np.exp(-Ea_red / T)
        r_reox = nu_reox * (1.0 - theta_O) * np.exp(-Ea_reox / T)
        return [r_reox - r_red]

    sol = scipy.integrate.solve_ivp(
        rhs, [0, t_end], [theta_O0], t_eval=t_eval,
        method='BDF', rtol=1e-8, atol=1e-10, max_step=t_end / 50,
    )

    theta_O = sol.y[0]
    temperature = T_start + beta * sol.t
    rate = nu_red * np.maximum(theta_O, 0) * \
           np.exp(-Ea_red / np.maximum(temperature, 1.0))

    return {
        'temperature': temperature.astype(np.float32),
        'rate': rate.astype(np.float32),
        'time': sol.t.astype(np.float32),
        'coverage': theta_O.astype(np.float32),
        'params': {
            'mechanism': 'MvK',
            'Ea_red': float(Ea_red),
            'Ea_reox': float(Ea_reox),
            'nu_red': float(nu_red),
            'nu_reox': float(nu_reox),
            'theta_O0': float(theta_O0),
            'beta': float(beta),
            'T_start': float(T_start),
            'T_end': float(T_end),
        },
    }


def run_tpd_first_order_covdep(Ed0, alpha_cov, nu, theta_0, beta, T_start, T_end,
                                n_points=N_POINTS_DEFAULT):
    """
    First-order desorption with coverage-dependent activation energy:
        Ed(theta) = Ed0 + alpha_cov * theta
        d(theta)/dt = -nu * theta * exp(-Ed(theta) / T(t))

    alpha_cov > 0: repulsive interactions (peak sharpens, shifts to lower T).
    alpha_cov < 0: attractive interactions (peak broadens, island breakup).

    Parameters
    ----------
    Ed0 : float
        Zero-coverage desorption energy (K, = Ea0/R).
    alpha_cov : float
        Coverage-dependence coefficient (K). Positive = repulsive, negative = attractive.
    nu : float
        Pre-exponential factor (s^-1).
    theta_0 : float
        Initial fractional surface coverage [0, 1].
    beta : float
        Heating rate (K/s).
    """
    t_end = (T_end - T_start) / beta
    t_eval = np.linspace(0, t_end, n_points)

    def rhs(t, y):
        theta = y[0]
        T = T_start + beta * t
        if T < 1.0:
            T = 1.0
        Ed_eff = Ed0 + alpha_cov * theta
        rate = -nu * theta * np.exp(-Ed_eff / T)
        return [rate]

    sol = scipy.integrate.solve_ivp(
        rhs, [0, t_end], [theta_0], t_eval=t_eval,
        method='BDF', rtol=1e-8, atol=1e-10, max_step=t_end / 50,
    )

    theta = sol.y[0]
    temperature = T_start + beta * sol.t
    Ed_eff = Ed0 + alpha_cov * np.maximum(theta, 0)
    rate = nu * np.maximum(theta, 0) * np.exp(-Ed_eff / np.maximum(temperature, 1.0))

    return {
        'temperature': temperature.astype(np.float32),
        'rate': rate.astype(np.float32),
        'time': sol.t.astype(np.float32),
        'coverage': theta.astype(np.float32),
        'params': {
            'mechanism': 'FirstOrderCovDep',
            'Ed0': float(Ed0),
            'alpha_cov': float(alpha_cov),
            'nu': float(nu),
            'theta_0': float(theta_0),
            'beta': float(beta),
            'T_start': float(T_start),
            'T_end': float(T_end),
        },
    }


def run_tpd_diff_limited(Ed, nu, D0, E_diff, theta_0, beta, T_start, T_end,
                          n_points=N_POINTS_DEFAULT, n_shells=20):
    """
    Diffusion-limited desorption from a porous/layered material.

    Models a 1D spherical particle with n_shells concentric shells.
    Surface desorption follows first-order kinetics; replenishment of the
    surface layer is limited by intra-particle diffusion with an
    Arrhenius-type diffusivity D(T) = D0 * exp(-E_diff / T).

    This produces characteristic broadened, asymmetric peaks with long
    high-temperature tails that traditional Redhead/Kissinger methods
    cannot fit — the apparent activation energy depends on particle size
    and diffusivity.

    Parameters
    ----------
    Ed : float
        Surface desorption energy (K, = Ea/R).
    nu : float
        Pre-exponential factor for desorption (s^-1).
    D0 : float
        Diffusion pre-exponential (s^-1, dimensionless Fourier units).
    E_diff : float
        Diffusion activation energy (K, = Ea_diff/R).
    theta_0 : float
        Initial uniform loading in all shells [0, 1].
    beta : float
        Heating rate (K/s).
    n_shells : int
        Number of radial shells for the discretized diffusion.
    """
    t_end = (T_end - T_start) / beta
    t_eval = np.linspace(0, t_end, n_points)

    # Radial grid: shells at r_i/R = (i+0.5)/n_shells, i=0..n_shells-1
    # Shell 0 = center, shell n_shells-1 = surface
    dr = 1.0 / n_shells
    r = np.array([(i + 0.5) * dr for i in range(n_shells)])
    r_face = np.array([i * dr for i in range(n_shells + 1)])

    # Shell volumes (spherical): V_i = 4/3 pi (r_face[i+1]^3 - r_face[i]^3)
    vol = (4.0 / 3.0) * np.pi * (r_face[1:] ** 3 - r_face[:-1] ** 3)

    y0 = np.full(n_shells, theta_0)

    def rhs(t, y):
        T = T_start + beta * t
        if T < 1.0:
            T = 1.0

        D = D0 * np.exp(-E_diff / T)
        dydt = np.zeros(n_shells)

        # Diffusion between adjacent shells (spherical coordinates)
        for i in range(n_shells - 1):
            area = 4.0 * np.pi * r_face[i + 1] ** 2
            flux = D * area * (y[i] - y[i + 1]) / dr
            dydt[i] -= flux / vol[i]
            dydt[i + 1] += flux / vol[i + 1]

        # Surface desorption from outermost shell only
        k_des = nu * np.exp(-Ed / T)
        dydt[-1] -= k_des * y[-1]

        return dydt

    sol = scipy.integrate.solve_ivp(
        rhs, [0, t_end], y0, t_eval=t_eval,
        method='BDF', rtol=1e-8, atol=1e-10, max_step=t_end / 50,
    )

    theta_all = sol.y  # [n_shells, n_points]
    temperature = T_start + beta * sol.t

    # Observable rate = surface desorption rate
    theta_surf = np.maximum(theta_all[-1], 0)
    rate = nu * theta_surf * np.exp(-Ed / np.maximum(temperature, 1.0))

    return {
        'temperature': temperature.astype(np.float32),
        'rate': rate.astype(np.float32),
        'time': sol.t.astype(np.float32),
        'coverage': theta_surf.astype(np.float32),
        'params': {
            'mechanism': 'DiffLimited',
            'Ed': float(Ed),
            'nu': float(nu),
            'D0': float(D0),
            'E_diff': float(E_diff),
            'theta_0': float(theta_0),
            'beta': float(beta),
            'T_start': float(T_start),
            'T_end': float(T_end),
            'n_shells': n_shells,
        },
    }


# =============================================================================
# Simulation dispatch
# =============================================================================

def _run_single_tpd(params):
    """Run a single TPD simulation, dispatching to the correct mechanism."""
    mech = params['mechanism']
    beta = params['beta']
    T_start = params['T_start']
    T_end = params['T_end']
    n_points = params.get('n_points', N_POINTS_DEFAULT)

    if mech == 'FirstOrder':
        return run_tpd_first_order(
            params['Ed'], params['nu'], params['theta_0'],
            beta, T_start, T_end, n_points,
        )
    elif mech == 'SecondOrder':
        return run_tpd_second_order(
            params['Ed'], params['nu'], params['theta_0'],
            beta, T_start, T_end, n_points,
        )
    elif mech == 'LH_Surface':
        return run_tpd_lh_surface(
            params['Ea'], params['nu'], params['theta_A0'], params['theta_B0'],
            beta, T_start, T_end, n_points,
        )
    elif mech == 'MvK':
        return run_tpd_mvk(
            params['Ea_red'], params['Ea_reox'], params['nu_red'],
            params['theta_O0'], beta, T_start, T_end, n_points,
        )
    elif mech == 'FirstOrderCovDep':
        return run_tpd_first_order_covdep(
            params['Ed0'], params['alpha_cov'], params['nu'],
            params['theta_0'], beta, T_start, T_end, n_points,
        )
    elif mech == 'DiffLimited':
        return run_tpd_diff_limited(
            params['Ed'], params['nu'], params['D0'], params['E_diff'],
            params['theta_0'], beta, T_start, T_end, n_points,
        )
    else:
        raise ValueError(f"Unknown TPD mechanism: {mech}")


# =============================================================================
# Parameter sampling
# =============================================================================

def _sample_common_tpd_params(rng):
    """Sample common TPD experiment parameters."""
    T_start = rng.uniform(300, 400)
    T_end = rng.uniform(900, 1200)
    return T_start, T_end


def _estimate_T_peak(Ed, log10_nu, beta):
    """Redhead estimate of peak temperature for first-order TPD.
    Solves Ed/T_peak^2 ≈ (nu/beta) * exp(-Ed/T_peak) iteratively.
    Good enough for rejection sampling."""
    # Initial guess: T_peak ≈ Ed / (ln(nu*Ed/beta) - ln(T_peak^2))
    # Simplified: T_peak ≈ Ed / (log10_nu * ln(10) - ln(Ed/beta))
    ln_nu = log10_nu * np.log(10)
    T_est = Ed / (ln_nu - np.log(max(Ed / max(beta, 0.01), 1.0)))
    T_est = np.clip(T_est, 100, 2000)
    # One Newton step
    for _ in range(3):
        exp_term = np.exp(-Ed / max(T_est, 1.0))
        f = Ed / (T_est ** 2) - (10 ** log10_nu / max(beta, 0.01)) * exp_term
        df = -2 * Ed / (T_est ** 3) - (10 ** log10_nu / max(beta, 0.01)) * exp_term * Ed / (T_est ** 2)
        if abs(df) > 1e-30:
            T_est = T_est - f / df
        T_est = np.clip(T_est, 100, 2000)
    return T_est


def _sample_Ed_nu_beta(rng, T_start, T_end, max_attempts=50):
    """Sample (Ed, nu, beta) ensuring the estimated peak temperature
    falls within the measurement window [T_start+margin, T_end-margin]."""
    T_lo = T_start + 0.15 * (T_end - T_start)
    T_hi = T_end - 0.10 * (T_end - T_start)

    for _ in range(max_attempts):
        log10_nu = rng.uniform(12, 16)
        log10_beta = rng.uniform(-0.5, 1.5)
        beta = 10 ** log10_beta

        # Sample T_peak target, then back-solve for Ed
        T_peak_target = rng.uniform(T_lo, T_hi)
        ln_nu = log10_nu * np.log(10)
        # From Redhead: Ed ≈ T_peak * (ln(nu*T_peak/beta) - ln(T_peak))
        #             ≈ T_peak * (ln_nu + ln(T_peak) - ln(beta) - 3.64)
        Ed = T_peak_target * (ln_nu + np.log(T_peak_target) - np.log(beta) - 3.64)

        if Ed < 3000 or Ed > 50000:
            continue

        T_est = _estimate_T_peak(Ed, log10_nu, beta)
        if T_lo <= T_est <= T_hi:
            return Ed, 10 ** log10_nu, beta

    # Fallback: conservative parameters that always produce a mid-range peak
    beta = 10 ** rng.uniform(0.0, 1.0)
    T_peak_target = rng.uniform(T_lo, T_hi)
    log10_nu = 13.0
    Ed = T_peak_target * (13.0 * np.log(10) + np.log(T_peak_target) - np.log(beta) - 3.64)
    return Ed, 10 ** log10_nu, beta


def sample_first_order_params(rng):
    """Sample parameters for first-order desorption."""
    T_start, T_end = _sample_common_tpd_params(rng)
    Ed, nu, beta = _sample_Ed_nu_beta(rng, T_start, T_end)
    theta_0 = rng.uniform(0.1, 1.0)
    return {
        'mechanism': 'FirstOrder',
        'Ed': float(Ed), 'nu': float(nu), 'theta_0': float(theta_0),
        'beta': float(beta), 'T_start': float(T_start), 'T_end': float(T_end),
    }


def sample_second_order_params(rng):
    """Sample parameters for second-order desorption."""
    T_start, T_end = _sample_common_tpd_params(rng)
    Ed, nu, beta = _sample_Ed_nu_beta(rng, T_start, T_end)
    theta_0 = rng.uniform(0.1, 1.0)
    return {
        'mechanism': 'SecondOrder',
        'Ed': float(Ed), 'nu': float(nu), 'theta_0': float(theta_0),
        'beta': float(beta), 'T_start': float(T_start), 'T_end': float(T_end),
    }


def sample_lh_surface_params(rng):
    """Sample parameters for LH bimolecular surface reaction."""
    T_start, T_end = _sample_common_tpd_params(rng)
    Ea, nu, beta = _sample_Ed_nu_beta(rng, T_start, T_end)
    theta_A0 = rng.uniform(0.1, 1.0)
    theta_B0 = rng.uniform(0.1, 1.0)
    return {
        'mechanism': 'LH_Surface',
        'Ea': float(Ea), 'nu': float(nu),
        'theta_A0': float(theta_A0), 'theta_B0': float(theta_B0),
        'beta': float(beta), 'T_start': float(T_start), 'T_end': float(T_end),
    }


def sample_mvk_params(rng):
    """Sample parameters for Mars-van Krevelen mechanism."""
    T_start, T_end = _sample_common_tpd_params(rng)
    Ea_red, nu_red, beta = _sample_Ed_nu_beta(rng, T_start, T_end)
    # Reoxidation energy: sample independently but also constrain to reasonable range
    Ea_reox, _, _ = _sample_Ed_nu_beta(rng, T_start, T_end)
    theta_O0 = rng.uniform(0.5, 1.0)
    return {
        'mechanism': 'MvK',
        'Ea_red': float(Ea_red), 'Ea_reox': float(Ea_reox),
        'nu_red': float(nu_red), 'theta_O0': float(theta_O0),
        'beta': float(beta), 'T_start': float(T_start), 'T_end': float(T_end),
    }


def sample_first_order_covdep_params(rng):
    """Sample parameters for coverage-dependent first-order desorption.

    Ed(theta) = Ed0 + alpha_cov * theta.  alpha_cov > 0 means repulsive
    lateral interactions (CO on metals); alpha_cov < 0 means attractive
    interactions (island formation, H-bonding).
    """
    T_start, T_end = _sample_common_tpd_params(rng)
    Ed0, nu, beta = _sample_Ed_nu_beta(rng, T_start, T_end)
    # Signed alpha: [-0.35, +0.35]*Ed0
    alpha_cov = rng.uniform(-0.35, 0.35) * Ed0
    theta_0 = rng.uniform(0.3, 1.0)
    return {
        'mechanism': 'FirstOrderCovDep',
        'Ed0': float(Ed0), 'alpha_cov': float(alpha_cov),
        'nu': float(nu), 'theta_0': float(theta_0),
        'beta': float(beta), 'T_start': float(T_start), 'T_end': float(T_end),
    }


def sample_diff_limited_params(rng):
    """Sample parameters for diffusion-limited desorption.

    The key physics: if E_diff is comparable to Ed, diffusion is the
    rate-limiting step and the TPD peak broadens with a long tail.
    If E_diff << Ed, diffusion is fast and the curve looks like standard
    first-order.  We sample to ensure a range of diffusion-limitation
    regimes.
    """
    T_start, T_end = _sample_common_tpd_params(rng)
    Ed, nu, beta = _sample_Ed_nu_beta(rng, T_start, T_end)

    # D0: dimensionless diffusion pre-exponential.
    # In Fourier number units (D*t/R^2), D0 ~ 1e2 to 1e6 gives a range
    # from strongly diffusion-limited to nearly surface-kinetics-limited.
    log10_D0 = rng.uniform(2.0, 6.0)
    D0 = 10 ** log10_D0

    # E_diff: diffusion activation energy.
    # Ratio E_diff/Ed ~ 0.3 to 0.9 covers weakly to strongly limited regimes.
    E_diff = rng.uniform(0.3, 0.9) * Ed

    theta_0 = rng.uniform(0.3, 1.0)
    return {
        'mechanism': 'DiffLimited',
        'Ed': float(Ed), 'nu': float(nu),
        'D0': float(D0), 'E_diff': float(E_diff),
        'theta_0': float(theta_0),
        'beta': float(beta), 'T_start': float(T_start), 'T_end': float(T_end),
    }


def sample_tpd_params(rng, mechanism=None):
    """Sample TPD parameters, optionally for a specific mechanism."""
    if mechanism is None:
        mechanism = rng.choice(TPD_MECHANISM_LIST)

    samplers = {
        'FirstOrder': sample_first_order_params,
        'SecondOrder': sample_second_order_params,
        'LH_Surface': sample_lh_surface_params,
        'MvK': sample_mvk_params,
        'FirstOrderCovDep': sample_first_order_covdep_params,
        'DiffLimited': sample_diff_limited_params,
    }
    return samplers[mechanism](rng)


# =============================================================================
# Noise
# =============================================================================

def _add_noise(signal, rng, noise_range=(0.001, 0.02)):
    """Add Gaussian noise to a signal. Returns (noisy_signal, sigma_noise).
    Clamps result to >= 0 since desorption/reaction rates are non-negative."""
    sigma_noise = rng.uniform(*noise_range)
    peak = np.max(np.abs(signal)) + 1e-20
    noise = sigma_noise * peak * rng.standard_normal(signal.shape)
    noisy = signal + noise.astype(signal.dtype)
    np.maximum(noisy, 0, out=noisy)
    return noisy, float(sigma_noise)


# =============================================================================
# Multi-heating-rate sampling
# =============================================================================

def _sample_heating_rates(rng, n_rates, log_beta_range=(-0.5, 1.5)):
    """Sample log-spaced heating rates spanning the given range."""
    lo, hi = log_beta_range
    if n_rates == 1:
        return np.array([10 ** rng.uniform(lo, hi)])
    anchors = np.linspace(lo, hi, n_rates)
    jitter = (hi - lo) / (n_rates - 1) * 0.3
    log_betas = np.array([rng.uniform(a - jitter, a + jitter) for a in anchors])
    log_betas = np.clip(log_betas, lo, hi)
    log_betas.sort()
    return 10 ** log_betas


# =============================================================================
# Dataset generation
# =============================================================================

def generate_sample_single(idx, outdir, seed, mechanism=None,
                           n_heating_rates=1, add_noise=True):
    """
    Generate and save a single TPD sample (single or multi-heating-rate).

    When n_heating_rates > 1, the same kinetic parameters are simulated at
    multiple heating rates and saved together.
    """
    rng = np.random.default_rng(seed + idx)

    try:
        params = sample_tpd_params(rng, mechanism=mechanism)
        actual_mechanism = params['mechanism']
        mechanism_id = TPD_MECHANISM_TO_ID[actual_mechanism]

        if n_heating_rates <= 1:
            result = _run_single_tpd(params)
            rate = result['rate'].copy()
            sigma_noise = 0.0
            if add_noise:
                rate, sigma_noise = _add_noise(rate, rng)

            save_params = dict(params)
            save_params['sigma_noise'] = sigma_noise

            np.savez_compressed(
                os.path.join(outdir, f"sample_{idx:06d}.npz"),
                temperature=result['temperature'],
                rate=rate,
                time=result['time'],
                params=save_params,
                mechanism_id=np.int32(mechanism_id),
            )
            meta = {
                'idx': idx, 'success': True,
                'mechanism': actual_mechanism,
                'mechanism_id': int(mechanism_id),
                'n_time': len(result['time']),
                'beta': float(params['beta']),
                'n_heating_rates': 1,
                'sigma_noise': sigma_noise,
            }
            return meta

        # Multi-heating-rate: same kinetic params, different heating rates
        heating_rates = _sample_heating_rates(rng, n_heating_rates)

        temperatures, rates, times = [], [], []
        for beta in heating_rates:
            p = dict(params)
            p['beta'] = float(beta)
            result = _run_single_tpd(p)
            temp = result['temperature'].copy()
            rate = result['rate'].copy()
            if add_noise:
                rate, _ = _add_noise(rate, rng)
            temperatures.append(temp)
            rates.append(rate)
            times.append(result['time'].copy())

        # Pad to same length
        max_t = max(len(t) for t in temperatures)
        n_hr = len(heating_rates)
        temp_arr = np.zeros((n_hr, max_t), dtype=np.float32)
        rate_arr = np.zeros((n_hr, max_t), dtype=np.float32)
        time_arr = np.zeros((n_hr, max_t), dtype=np.float32)
        lengths = np.zeros(n_hr, dtype=np.int32)

        for i in range(n_hr):
            t_len = len(temperatures[i])
            temp_arr[i, :t_len] = temperatures[i]
            rate_arr[i, :t_len] = rates[i]
            time_arr[i, :t_len] = times[i]
            lengths[i] = t_len

        save_params = dict(params)
        # Remove the single beta; heating_rates array is saved separately
        save_params.pop('beta', None)

        np.savez_compressed(
            os.path.join(outdir, f"sample_{idx:06d}.npz"),
            temperature=temp_arr,
            rate=rate_arr,
            time=time_arr,
            heating_rates=heating_rates.astype(np.float32),
            lengths=lengths,
            params=save_params,
            mechanism_id=np.int32(mechanism_id),
            n_heating_rates=np.int32(n_heating_rates),
        )

        meta = {
            'idx': idx, 'success': True,
            'mechanism': actual_mechanism,
            'mechanism_id': int(mechanism_id),
            'n_time_max': int(max_t),
            'heating_rates': [float(b) for b in heating_rates],
            'n_heating_rates': n_heating_rates,
        }
        return meta

    except Exception as e:
        return {
            'idx': idx,
            'success': False,
            'error': str(e),
        }


def _worker_generate(args):
    """Worker function for multiprocessing (must be at module level)."""
    idx, outdir, seed, mechanism, n_heating_rates, add_noise = args
    return generate_sample_single(idx, outdir, seed, mechanism,
                                  n_heating_rates, add_noise)


def generate_dataset(
    n_samples=1000,
    outdir="data_tpd/raw",
    seed=42,
    n_workers=None,
    mechanism=None,
    multi_mechanism=False,
    n_per_mechanism=None,
    n_heating_rates=1,
    add_noise=True,
):
    """Generate a dataset of TPD simulations."""
    os.makedirs(outdir, exist_ok=True)

    if n_workers is None:
        n_workers = max(1, cpu_count() - 1)

    if multi_mechanism:
        if n_per_mechanism is None:
            n_per_mechanism = n_samples
        total = n_per_mechanism * len(TPD_MECHANISM_LIST)
        print(f"Generating multi-mechanism TPD dataset: "
              f"{n_per_mechanism} per mechanism x {len(TPD_MECHANISM_LIST)} = {total}")
        args_list = []
        for mech_idx, mech in enumerate(TPD_MECHANISM_LIST):
            offset = mech_idx * n_per_mechanism
            for i in range(n_per_mechanism):
                args_list.append((offset + i, outdir, seed, mech,
                                  n_heating_rates, add_noise))
        n_samples = total
    else:
        args_list = [(i, outdir, seed, mechanism, n_heating_rates, add_noise)
                     for i in range(n_samples)]

    n_workers = min(n_workers, n_samples)

    print(f"Generating {n_samples} TPD samples...")
    print(f"Output directory: {outdir}")
    print(f"Heating rates per sample: {n_heating_rates}")
    print(f"Using {n_workers} worker(s)")

    metadata = []

    if n_workers == 1:
        for args in tqdm(args_list, desc="Generating samples"):
            meta = _worker_generate(args)
            metadata.append(meta)
            if not meta['success']:
                print(f"\nSample {meta['idx']} failed: {meta.get('error', 'Unknown')}")
    else:
        try:
            with Pool(processes=n_workers) as pool:
                for meta in tqdm(
                    pool.imap_unordered(_worker_generate, args_list, chunksize=max(1, n_samples // (n_workers * 4))),
                    total=n_samples,
                    desc="Generating samples",
                ):
                    metadata.append(meta)
                    if not meta['success']:
                        tqdm.write(f"Sample {meta['idx']} failed: "
                                   f"{meta.get('error', 'Unknown')}")
        except (PermissionError, OSError) as e:
            print(f"\nWarning: Multiprocessing failed ({e}). "
                  "Falling back to sequential...")
            metadata = []
            for args in tqdm(args_list, desc="Generating samples (sequential)"):
                meta = _worker_generate(args)
                metadata.append(meta)
                if not meta['success']:
                    print(f"\nSample {meta['idx']} failed: "
                          f"{meta.get('error', 'Unknown')}")

    metadata = sorted(metadata, key=lambda x: x['idx'])

    n_success = sum(1 for m in metadata if m['success'])
    mech_counts = {}
    for m in metadata:
        if m['success']:
            mech = m.get('mechanism', 'Unknown')
            mech_counts[mech] = mech_counts.get(mech, 0) + 1

    summary = {
        'n_samples': n_samples,
        'n_success': n_success,
        'n_heating_rates': n_heating_rates,
        'add_noise': add_noise,
        'seed': seed,
        'n_workers': n_workers,
        'multi_mechanism': multi_mechanism,
        'mechanism_counts': mech_counts,
        'samples': metadata,
    }

    with open(os.path.join(outdir, "metadata.json"), "w") as f:
        json.dump(summary, f, indent=2)

    print(f"\nGeneration complete: {n_success}/{n_samples} successful")
    print(f"Mechanism counts: {mech_counts}")
    print(f"Metadata saved to {os.path.join(outdir, 'metadata.json')}")

    return metadata


# =============================================================================
# Main
# =============================================================================

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Generate TPD dataset for catalysis mechanism identification"
    )
    parser.add_argument("--n_samples", type=int, default=1000)
    parser.add_argument("--outdir", type=str, default="data_tpd/raw")
    parser.add_argument("--seed", type=int, default=42)
    parser.add_argument("--n_workers", type=int, default=None)
    parser.add_argument("--mechanism", type=str, default=None,
                        choices=TPD_MECHANISM_LIST)
    parser.add_argument("--multi_mechanism", action="store_true")
    parser.add_argument("--n_per_mechanism", type=int, default=None)
    parser.add_argument("--n_heating_rates", type=int, default=1)
    parser.add_argument("--no_noise", action="store_true")
    parser.add_argument("--test", action="store_true",
                        help="Run a single test simulation and plot")

    args = parser.parse_args()

    if args.test:
        print(f"Running test TPD simulations for all {len(TPD_MECHANISM_LIST)} mechanisms...\n")
        rng = np.random.default_rng(42)

        for mech in TPD_MECHANISM_LIST:
            params = sample_tpd_params(rng, mechanism=mech)
            result = _run_single_tpd(params)
            peak_rate = np.max(result['rate'])
            peak_T = result['temperature'][np.argmax(result['rate'])]
            print(f"{mech}:")
            print(f"  T range: [{result['temperature'][0]:.0f}, "
                  f"{result['temperature'][-1]:.0f}] K")
            print(f"  Peak rate: {peak_rate:.4e} at T = {peak_T:.0f} K")
            print(f"  Time steps: {len(result['time'])}")
            print(f"  Params: {params}")
            print()

        try:
            import matplotlib.pyplot as plt

            fig, axes = plt.subplots(2, 2, figsize=(12, 10))
            for ax, mech in zip(axes.flat, TPD_MECHANISM_LIST):
                params = sample_tpd_params(rng, mechanism=mech)
                for beta in [0.5, 2.0, 10.0]:
                    p = dict(params)
                    p['beta'] = beta
                    result = _run_single_tpd(p)
                    ax.plot(result['temperature'], result['rate'],
                            label=f'beta={beta:.1f} K/s')
                ax.set_xlabel('Temperature (K)')
                ax.set_ylabel('Desorption/Reaction Rate')
                ax.set_title(mech)
                ax.legend(fontsize=8)

            plt.tight_layout()
            plt.savefig('test_tpd_simulation.png', dpi=150)
            print("Plot saved to test_tpd_simulation.png")
        except ImportError:
            print("matplotlib not available, skipping plot")
    else:
        generate_dataset(
            n_samples=args.n_samples,
            outdir=args.outdir,
            seed=args.seed,
            n_workers=args.n_workers,
            mechanism=args.mechanism,
            multi_mechanism=args.multi_mechanism,
            n_per_mechanism=args.n_per_mechanism,
            n_heating_rates=args.n_heating_rates,
            add_noise=not args.no_noise,
        )