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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
1049
1050
# -*- coding: utf-8 -*-
# Process-oriented evaluator
# Run with:
#   abaqus cae noGUI=eval.py
#
# Final stdout must be exactly one line:
#   True
# or
#   False

from abaqus import *
from abaqusConstants import *
from caeModules import *
from odbAccess import openOdb

import os
import sys
import math
import traceback

TASK_ID = 'task-13'
PROCESS_SPEC = {'artifact': {'job_name': 'Job-Modal-A', 'model_name': 'Model-Modal-A', 'step_name': 'Step-Modal-A'},
 'process': {'geometry': {'bbox_spans': {'x': 420.0, 'y': 12.0, 'z': 10.0}, 'tol': 0.4},
             'materials': [{'E': 210000.0, 'density': 7.8e-09, 'name': 'Steel', 'nu': 0.3}],
             'mesh': {'main_element_type': 'C3D8R', 'seed_sizes': [12.0], 'seed_tol': 1.2},
             'min_counts': {'boundary_conditions': 1, 'loads': 0},
             'section': {'material_names': ['Steel'], 'type': 'SOLID'},
             'step': {'kind': 'FREQUENCY', 'num_eigen': 4}},
 'solver': {'min_frames': 5, 'require_mode_frames': True}}

ABS_TOL = 1.0e-6
GEOM_TOL = 5.0e-3
MATERIAL_TOL = 1.0e-6
SEED_TOL_DEFAULT = 1.0e-3

DETAILS = []


def log(msg):
    DETAILS.append(str(msg))


def _write_text(path, text):
    try:
        f = open(path, 'w')
        f.write(text)
        f.close()
    except Exception:
        pass


def output_result(value, detail_path=None, result_path=None):
    text = 'True\n' if value else 'False\n'
    if result_path:
        _write_text(result_path, text)
    if detail_path:
        _write_text(detail_path, ''.join([line + '\n' for line in DETAILS]))

    try:
        sys.__stdout__.write(text)
        sys.__stdout__.flush()
    except Exception:
        try:
            sys.stdout.write(text)
            sys.stdout.flush()
        except Exception:
            pass


def fail(msg):
    log('[FAIL] ' + str(msg))
    return False


def ok(msg):
    log('[PASS] ' + str(msg))
    return True


def safe_float(v, default=None):
    try:
        return float(v)
    except Exception:
        return default


def close_enough(a, b, tol=ABS_TOL, rel=1.0e-3):
    fa = safe_float(a, None)
    fb = safe_float(b, None)
    if fa is None or fb is None:
        return False
    return abs(fa - fb) <= max(float(tol), abs(fb) * float(rel))


def ci(s):
    try:
        return str(s).strip().upper()
    except Exception:
        return ''


def names_equal(a, b):
    return ci(a) == ci(b)


def get_desktop(job_name):
    candidates = []
    up = os.environ.get('USERPROFILE', None)
    if up:
        candidates.append(os.path.join(up, 'Desktop'))
    candidates.append(r'C:\Users\user\Desktop')
    candidates.append(r'C:\Users\User\Desktop')

    for d in candidates:
        if os.path.exists(os.path.join(d, job_name + '.cae')) or os.path.exists(os.path.join(d, job_name + '.odb')):
            return d

    for d in candidates:
        if os.path.isdir(d):
            return d

    return r'C:\Users\user\Desktop'


def find_key_ci(repo, target):
    if target is None:
        return None
    tu = ci(target)
    try:
        for k in repo.keys():
            ku = ci(k)
            if ku == tu or ku.endswith('.' + tu):
                return k
    except Exception:
        return None
    return None


def get_repo_value_ci(repo, target):
    k = find_key_ci(repo, target)
    if k is None:
        return None
    try:
        return repo[k]
    except Exception:
        return None


def symbol_to_bool(v):
    if isinstance(v, bool):
        return v
    vu = ci(v)
    if vu in ('ON', 'TRUE', '1'):
        return True
    if vu in ('OFF', 'FALSE', '0'):
        return False
    return None


def is_set_value(v):
    vu = ci(v)
    if vu in ('', 'NONE', 'UNSET', 'UNCHANGED', 'FREED'):
        return False
    return True


def class_name(obj):
    try:
        return obj.__class__.__name__.upper()
    except Exception:
        try:
            return str(type(obj)).upper()
        except Exception:
            return ''


def step_kind_matches(step_obj, expected_kind):
    text = ' '.join([
        class_name(step_obj),
        ci(getattr(step_obj, 'procedureType', '')),
        ci(getattr(step_obj, 'analysis', '')),
    ])
    ek = ci(expected_kind)
    if ek == 'STATIC':
        return ('STATIC' in text) and ('HEAT' not in text)
    if ek == 'HEAT_TRANSFER':
        return ('HEAT' in text) or ('HEAT_TRANSFER' in text)
    if ek == 'BUCKLE':
        return 'BUCKLE' in text
    if ek == 'FREQUENCY':
        return 'FREQUENCY' in text
    return False


def choose_model(mdb_obj, expected_name=None):
    if expected_name:
        k = find_key_ci(mdb_obj.models, expected_name)
        if k is not None:
            return mdb_obj.models[k]

    best = None
    best_score = -1
    try:
        for mk in mdb_obj.models.keys():
            m = mdb_obj.models[mk]
            score = 0
            try:
                score += len(m.parts.keys()) * 10
            except Exception:
                pass
            try:
                for pk in m.parts.keys():
                    p = m.parts[pk]
                    score += len(p.elements) * 2 + len(p.nodes)
            except Exception:
                pass
            if score > best_score:
                best_score = score
                best = m
    except Exception:
        return None

    return best


def choose_primary_part(model):
    best = None
    best_score = -1
    try:
        for pk in model.parts.keys():
            p = model.parts[pk]
            score = 0
            try:
                score += len(p.elements) * 2 + len(p.nodes)
            except Exception:
                pass
            if score > best_score:
                best_score = score
                best = p
    except Exception:
        return None
    return best


def collect_node_xyz(nodes):
    out = []
    for n in nodes:
        try:
            x, y, z = n.coordinates
            out.append((float(x), float(y), float(z)))
        except Exception:
            pass
    return out


def bbox_from_xyz(xyz):
    if not xyz:
        return None
    xs = [p[0] for p in xyz]
    ys = [p[1] for p in xyz]
    zs = [p[2] for p in xyz]
    return {
        'x_min': min(xs), 'x_max': max(xs),
        'y_min': min(ys), 'y_max': max(ys),
        'z_min': min(zs), 'z_max': max(zs),
        'x_span': max(xs) - min(xs),
        'y_span': max(ys) - min(ys),
        'z_span': max(zs) - min(zs),
    }


def check_bbox(bbox, geom_spec):
    if bbox is None:
        return fail('Geometry bbox is empty')

    tol = float(geom_spec.get('tol', GEOM_TOL))

    spans = geom_spec.get('bbox_spans', None)
    if spans:
        for axis, exp in spans.items():
            key = axis.lower() + '_span'
            obs = bbox.get(key, None)
            if obs is None or not close_enough(obs, exp, tol=tol, rel=1.0e-3):
                return fail('bbox span mismatch for %s: obs=%s exp=%s tol=%s' % (axis, str(obs), str(exp), str(tol)))

    mins = geom_spec.get('bbox_mins', None)
    if mins:
        for axis, exp in mins.items():
            key = axis.lower() + '_min'
            obs = bbox.get(key, None)
            if obs is None or not close_enough(obs, exp, tol=tol, rel=1.0e-3):
                return fail('bbox min mismatch for %s: obs=%s exp=%s tol=%s' % (axis, str(obs), str(exp), str(tol)))

    maxs = geom_spec.get('bbox_maxs', None)
    if maxs:
        for axis, exp in maxs.items():
            key = axis.lower() + '_max'
            obs = bbox.get(key, None)
            if obs is None or not close_enough(obs, exp, tol=tol, rel=1.0e-3):
                return fail('bbox max mismatch for %s: obs=%s exp=%s tol=%s' % (axis, str(obs), str(exp), str(tol)))

    return True


def check_hole(xyz, hole_spec):
    if not hole_spec:
        return True

    cx = float(hole_spec['center'][0])
    cy = float(hole_spec['center'][1])
    r0 = float(hole_spec['radius'])
    tol = float(hole_spec.get('tol', 1.0))
    min_nodes = int(hole_spec.get('min_nodes', 6))

    hits = []
    for x, y, z in xyz:
        rr = math.sqrt((x - cx) ** 2 + (y - cy) ** 2)
        if abs(rr - r0) <= tol:
            hits.append(rr)

    if len(hits) < min_nodes:
        return fail('hole boundary nodes too few: obs=%s min=%s' % (str(len(hits)), str(min_nodes)))

    ravg = sum(hits) / float(len(hits))
    if not close_enough(ravg, r0, tol=tol, rel=1.0e-3):
        return fail('hole radius mismatch: obs=%s exp=%s tol=%s' % (str(ravg), str(r0), str(tol)))

    return True


def get_element_type_counts(elements):
    counts = {}
    for e in elements:
        t = ci(getattr(e, 'type', 'UNKNOWN'))
        counts[t] = counts.get(t, 0) + 1
    return counts


def check_mesh(part_obj, mesh_spec):
    if not mesh_spec:
        return True

    main_type = ci(mesh_spec.get('main_element_type', ''))
    tol = float(mesh_spec.get('seed_tol', SEED_TOL_DEFAULT))

    try:
        counts = get_element_type_counts(part_obj.elements)
    except Exception:
        return fail('Cannot inspect element types')

    total = 0
    for k in counts:
        total += counts[k]

    if main_type:
        main_count = counts.get(main_type, 0)
        if main_count <= 0:
            return fail('Main element type not found: ' + str(main_type))
        if total > 0 and float(main_count) / float(total) < 0.5:
            return fail('Main element type fraction too low for %s: %s/%s' % (main_type, str(main_count), str(total)))

    seed_sizes = mesh_spec.get('seed_sizes', [])
    if seed_sizes:
        observed = []
        try:
            s = part_obj.getPartSeeds(SIZE)
            sf = safe_float(s, None)
            if sf is not None and sf > 0.0:
                observed.append(sf)
        except Exception:
            pass

        if observed:
            for exp in seed_sizes:
                expf = float(exp)
                ok_seed = False
                for ob in observed:
                    if close_enough(ob, expf, tol=tol, rel=1.0e-3):
                        ok_seed = True
                        break
                if not ok_seed:
                    return fail('seed size mismatch: expected one of %s, observed=%s' % (str(seed_sizes), str(observed)))

    return True


def _read_material_value(mat_obj, field_name):
    obj = getattr(mat_obj, field_name, None)
    if obj is None:
        return None
    tbl = getattr(obj, 'table', None)
    if tbl is None:
        return None
    try:
        if len(tbl) == 0:
            return None
        row0 = tbl[0]
        if len(row0) == 0:
            return None
        return safe_float(row0[0], None)
    except Exception:
        return None


def check_materials(model, mats_spec):
    if not mats_spec:
        return True

    for exp in mats_spec:
        mobj = get_repo_value_ci(model.materials, exp.get('name'))
        if mobj is None:
            return fail('Material not found: ' + str(exp.get('name')))

        if 'E' in exp or 'nu' in exp:
            try:
                e_obs, nu_obs = mobj.elastic.table[0]
            except Exception:
                return fail('Material elastic data missing: ' + str(exp.get('name')))

            if 'E' in exp and not close_enough(e_obs, exp['E'], tol=MATERIAL_TOL, rel=1.0e-3):
                return fail('Material E mismatch for %s: obs=%s exp=%s' % (str(exp.get('name')), str(e_obs), str(exp['E'])))
            if 'nu' in exp and not close_enough(nu_obs, exp['nu'], tol=MATERIAL_TOL, rel=1.0e-3):
                return fail('Material nu mismatch for %s: obs=%s exp=%s' % (str(exp.get('name')), str(nu_obs), str(exp['nu'])))

        if 'density' in exp:
            obs = _read_material_value(mobj, 'density')
            if obs is None or not close_enough(obs, exp['density'], tol=MATERIAL_TOL, rel=5.0e-2):
                return fail('Material density mismatch for %s: obs=%s exp=%s' % (str(exp.get('name')), str(obs), str(exp['density'])))

        if 'conductivity' in exp:
            obs = _read_material_value(mobj, 'conductivity')
            if obs is None:
                return fail('Material conductivity missing for ' + str(exp.get('name')))
            target = exp['conductivity']
            if isinstance(target, (list, tuple)):
                matched = False
                for t in target:
                    if close_enough(obs, t, tol=MATERIAL_TOL, rel=5.0e-2):
                        matched = True
                        break
                if not matched:
                    return fail('Material conductivity mismatch for %s: obs=%s exp_any=%s' % (str(exp.get('name')), str(obs), str(target)))
            else:
                if not close_enough(obs, target, tol=MATERIAL_TOL, rel=5.0e-2):
                    return fail('Material conductivity mismatch for %s: obs=%s exp=%s' % (str(exp.get('name')), str(obs), str(target)))

        if 'specific_heat' in exp:
            obs = _read_material_value(mobj, 'specificHeat')
            if obs is None or not close_enough(obs, exp['specific_heat'], tol=MATERIAL_TOL, rel=5.0e-2):
                return fail('Material specificHeat mismatch for %s: obs=%s exp=%s' % (str(exp.get('name')), str(obs), str(exp['specific_heat'])))

        if 'expansion' in exp:
            obs = _read_material_value(mobj, 'expansion')
            if obs is None or not close_enough(obs, exp['expansion'], tol=MATERIAL_TOL, rel=5.0e-2):
                return fail('Material expansion mismatch for %s: obs=%s exp=%s' % (str(exp.get('name')), str(obs), str(exp['expansion'])))

    return True


def check_section(model, part_obj, section_spec):
    if not section_spec:
        return True

    try:
        assigns = part_obj.sectionAssignments
        if len(assigns) == 0:
            return fail('No section assignment found on primary part')
    except Exception:
        return fail('Cannot inspect section assignments on primary part')

    expected_type = ci(section_spec.get('type', ''))
    expected_materials = [ci(x) for x in section_spec.get('material_names', [])]
    expected_thickness = section_spec.get('thickness', None)
    expected_int_pts = section_spec.get('num_int_pts', None)

    found_match = False
    try:
        for sk in model.sections.keys():
            sec = model.sections[sk]
            sname = class_name(sec)

            if expected_type == 'SHELL' and 'SHELL' not in sname:
                continue
            if expected_type == 'SOLID' and 'SOLID' not in sname:
                continue

            mat_name = ci(getattr(sec, 'material', ''))
            if expected_materials and mat_name not in expected_materials:
                continue

            if expected_thickness is not None:
                th = safe_float(getattr(sec, 'thickness', None), None)
                if th is None or not close_enough(th, expected_thickness, tol=GEOM_TOL, rel=1.0e-3):
                    continue

            if expected_int_pts is not None:
                nipt = getattr(sec, 'numIntPts', None)
                ni = None
                try:
                    ni = int(nipt)
                except Exception:
                    ni = None
                if ni is None or ni != int(expected_int_pts):
                    continue

            found_match = True
            break
    except Exception:
        return fail('Cannot inspect section objects')

    if not found_match:
        return fail('No matching section found for expected section spec')

    return True


def all_set_names(model, part_obj):
    names = []
    try:
        names.extend(list(model.rootAssembly.sets.keys()))
    except Exception:
        pass
    try:
        names.extend(list(model.rootAssembly.nodeSets.keys()))
    except Exception:
        pass
    try:
        names.extend(list(part_obj.sets.keys()))
    except Exception:
        pass
    return names


def set_name_exists(all_names, target):
    tu = ci(target)
    for n in all_names:
        nu = ci(n)
        if nu == tu or nu.endswith('.' + tu):
            return True
    return False


def check_required_sets(model, part_obj, set_names):
    if not set_names:
        return True
    names = all_set_names(model, part_obj)
    for s in set_names:
        if not set_name_exists(names, s):
            return fail('Required set not found: ' + str(s))
    return True


def bc_matches_req(bc_obj, req):
    step_req = req.get('step', None)
    if step_req is not None:
        cstep = getattr(bc_obj, 'createStepName', None)
        if not names_equal(cstep, step_req):
            return False

    dofs = req.get('dofs', {})
    for dof_name in dofs.keys():
        exp = dofs[dof_name]
        obs = getattr(bc_obj, dof_name, None)
        if exp == 'SET':
            if not is_set_value(obs):
                return False
        elif exp == 'UNSET':
            if is_set_value(obs):
                return False
        else:
            if not is_set_value(obs):
                return False
            if not close_enough(obs, exp, tol=ABS_TOL, rel=1.0e-3):
                return False

    return True


def check_bcs(model, bc_specs, min_count=None):
    repo = model.boundaryConditions

    if min_count is not None:
        try:
            if len(repo.keys()) < int(min_count):
                return fail('Boundary condition count too low: obs=%s min=%s' % (str(len(repo.keys())), str(min_count)))
        except Exception:
            return fail('Cannot inspect boundary condition repository')

    if not bc_specs:
        return True

    try:
        bcs = [repo[k] for k in repo.keys()]
    except Exception:
        return fail('Cannot iterate boundary conditions')

    for req in bc_specs:
        matched = False
        for bc in bcs:
            if bc_matches_req(bc, req):
                matched = True
                break
        if not matched:
            return fail('Required BC signature not found: ' + str(req))

    return True


def type_matches_any(obj, tokens):
    if not tokens:
        return True
    txt = class_name(obj)
    for t in tokens:
        if ci(t) in txt:
            return True
    return False


def load_matches_req(load_obj, req):
    if not type_matches_any(load_obj, req.get('type_any', [])):
        return False

    step_req = req.get('step', None)
    if step_req is not None:
        cstep = getattr(load_obj, 'createStepName', None)
        if not names_equal(cstep, step_req):
            return False

    if 'magnitude' in req:
        mag = getattr(load_obj, 'magnitude', None)
        if mag is None:
            return False
        tol = float(req.get('tol', ABS_TOL))
        if not close_enough(mag, req['magnitude'], tol=tol, rel=1.0e-2):
            return False

    comp = req.get('component', None)
    if comp:
        val = getattr(load_obj, comp, None)
        if val is None:
            return False
        if not is_set_value(val):
            return False
        sign = req.get('sign', None)
        vf = safe_float(val, None)
        if vf is None:
            return False
        if sign == 'positive' and vf <= 0.0:
            return False
        if sign == 'negative' and vf >= 0.0:
            return False

    return True


def check_loads(model, load_specs, min_count=None):
    repo = model.loads

    if min_count is not None:
        try:
            if len(repo.keys()) < int(min_count):
                return fail('Load count too low: obs=%s min=%s' % (str(len(repo.keys())), str(min_count)))
        except Exception:
            return fail('Cannot inspect load repository')

    if not load_specs:
        return True

    try:
        loads = [repo[k] for k in repo.keys()]
    except Exception:
        return fail('Cannot iterate loads')

    for req in load_specs:
        matched = False
        for ld in loads:
            if load_matches_req(ld, req):
                matched = True
                break
        if not matched:
            return fail('Required load signature not found: ' + str(req))

    return True


def check_couplings(model, req):
    if not req:
        return True

    min_count = int(req.get('min_count', 0))

    try:
        repo = model.constraints
        couplings = []
        for k in repo.keys():
            c = repo[k]
            if 'COUPLING' in class_name(c):
                ctype = ci(getattr(c, 'couplingType', ''))
                if 'KINEMATIC' in ctype or req.get('accept_any_coupling', False):
                    couplings.append(c)
        if len(couplings) < min_count:
            return fail('Kinematic coupling count too low: obs=%s min=%s' % (str(len(couplings)), str(min_count)))
    except Exception:
        return fail('Cannot inspect coupling constraints')

    return True


def check_contact(model, req):
    if not req:
        return True

    min_interactions = int(req.get('min_interactions', 1))
    min_props = int(req.get('min_properties', 1))

    try:
        if len(model.interactions.keys()) < min_interactions:
            return fail('Interaction count too low: obs=%s min=%s' % (str(len(model.interactions.keys())), str(min_interactions)))
    except Exception:
        return fail('Cannot inspect interactions')

    try:
        if len(model.interactionProperties.keys()) < min_props:
            return fail('Interaction property count too low: obs=%s min=%s' % (str(len(model.interactionProperties.keys())), str(min_props)))
    except Exception:
        return fail('Cannot inspect interaction properties')

    if req.get('hard', False) or req.get('frictionless', False):
        hard_ok = not req.get('hard', False)
        fric_ok = not req.get('frictionless', False)

        try:
            for k in model.interactionProperties.keys():
                ip = model.interactionProperties[k]

                if req.get('hard', False):
                    nb = getattr(ip, 'normalBehavior', None)
                    po = ci(getattr(nb, 'pressureOverclosure', '')) if nb is not None else ''
                    if 'HARD' in po:
                        hard_ok = True

                if req.get('frictionless', False):
                    tb = getattr(ip, 'tangentialBehavior', None)
                    form = ci(getattr(tb, 'formulation', '')) if tb is not None else ''
                    if 'FRICTIONLESS' in form:
                        fric_ok = True
        except Exception:
            pass

        if not hard_ok:
            return fail('Hard contact property not confirmed')
        if not fric_ok:
            return fail('Frictionless tangential behavior not confirmed')

    return True


def check_predefined_temperature(model, reqs):
    if not reqs:
        return True

    try:
        fields = [model.predefinedFields[k] for k in model.predefinedFields.keys()]
    except Exception:
        return fail('Cannot inspect predefined fields')

    for req in reqs:
        step_req = req.get('step', None)
        mag_req = req.get('magnitude', None)

        matched = False
        for pf in fields:
            if 'TEMPERATURE' not in class_name(pf):
                continue
            cstep = getattr(pf, 'createStepName', None)
            if step_req is not None and not names_equal(cstep, step_req):
                continue

            if mag_req is not None:
                mags = getattr(pf, 'magnitudes', None)
                mv = None
                try:
                    if isinstance(mags, (list, tuple)) and len(mags) > 0:
                        mv = safe_float(mags[0], None)
                    else:
                        mv = safe_float(mags, None)
                except Exception:
                    mv = None

                if mv is None:
                    matched = True
                    break

                if close_enough(mv, mag_req, tol=GEOM_TOL, rel=1.0e-2):
                    matched = True
                    break
            else:
                matched = True
                break

        if not matched:
            return fail('Required temperature predefined field not found: ' + str(req))

    return True


def check_step(model, step_spec):
    step_name = PROCESS_SPEC['artifact']['step_name']
    step_key = find_key_ci(model.steps, step_name)
    if step_key is None:
        return fail('Step not found in CAE model: ' + str(step_name))

    step_obj = model.steps[step_key]

    kind = step_spec.get('kind', None)
    if kind is not None and not step_kind_matches(step_obj, kind):
        return fail('Step kind mismatch: expected %s got class=%s procedure=%s' % (
            str(kind), class_name(step_obj), str(getattr(step_obj, 'procedureType', None))))

    if 'nlgeom' in step_spec and step_spec['nlgeom'] is not None:
        exp = bool(step_spec['nlgeom'])
        obs_raw = getattr(step_obj, 'nlgeom', None)
        obs = symbol_to_bool(obs_raw)
        if obs is None or obs != exp:
            return fail('Step nlgeom mismatch: obs=%s exp=%s' % (str(obs_raw), str(exp)))

    if 'num_eigen' in step_spec and step_spec['num_eigen'] is not None:
        obs = getattr(step_obj, 'numEigen', None)
        try:
            oi = int(obs)
        except Exception:
            return fail('Step numEigen unreadable: ' + str(obs))
        if oi != int(step_spec['num_eigen']):
            return fail('Step numEigen mismatch: obs=%s exp=%s' % (str(oi), str(step_spec['num_eigen'])))

    if 'response' in step_spec and step_spec['response'] is not None:
        obs = ci(getattr(step_obj, 'response', ''))
        if ci(step_spec['response']) not in obs:
            return fail('Step response mismatch: obs=%s exp=%s' % (str(obs), str(step_spec['response'])))

    for key, attr in [('time_period', 'timePeriod'), ('initial_inc', 'initialInc'), ('max_inc', 'maxInc'), ('deltmx', 'deltmx')]:
        if key in step_spec and step_spec[key] is not None:
            obs = getattr(step_obj, attr, None)
            if not close_enough(obs, step_spec[key], tol=GEOM_TOL, rel=1.0e-2):
                return fail('Step %s mismatch: obs=%s exp=%s' % (str(attr), str(obs), str(step_spec[key])))

    return True


def check_cae_process(cae_path):
    try:
        openMdb(pathName=cae_path)
    except Exception as e:
        return fail('Cannot open CAE: ' + str(e)), None

    model = choose_model(mdb, PROCESS_SPEC['artifact'].get('model_name', None))
    if model is None:
        return fail('Cannot choose model from CAE'), None

    part_obj = choose_primary_part(model)
    if part_obj is None:
        return fail('Cannot choose primary part from CAE'), None

    proc = PROCESS_SPEC.get('process', {})

    if not check_step(model, proc.get('step', {})):
        return False, model
    ok('Step check passed')

    if not check_materials(model, proc.get('materials', [])):
        return False, model
    ok('Material check passed')

    if not check_section(model, part_obj, proc.get('section', {})):
        return False, model
    ok('Section check passed')

    xyz = collect_node_xyz(part_obj.nodes)
    bbox = bbox_from_xyz(xyz)
    if not check_bbox(bbox, proc.get('geometry', {})):
        return False, model
    if not check_hole(xyz, proc.get('geometry', {}).get('hole', None)):
        return False, model
    ok('Geometry check passed')

    if not check_mesh(part_obj, proc.get('mesh', {})):
        return False, model
    ok('Mesh check passed')

    if not check_required_sets(model, part_obj, proc.get('required_sets', [])):
        return False, model
    ok('Set check passed')

    min_bc = None
    min_load = None
    min_counts = proc.get('min_counts', {})
    if min_counts:
        min_bc = min_counts.get('boundary_conditions', None)
        min_load = min_counts.get('loads', None)

    if not check_bcs(model, proc.get('bc_signatures', []), min_count=min_bc):
        return False, model
    ok('BC check passed')

    load_ok = check_loads(model, proc.get('load_signatures', []), min_count=min_load)
    if load_ok:
        ok('Load check passed')
    else:
        if not check_keyword_bcs_loads_b(model, proc):
            return False, model
        ok('Keyword load fallback passed')

    if not check_couplings(model, proc.get('kinematic_coupling', None)):
        return False, model
    if proc.get('kinematic_coupling', None):
        ok('Coupling check passed')

    if not check_contact(model, proc.get('contact', None)):
        return False, model
    if proc.get('contact', None):
        ok('Contact check passed')

    if not check_predefined_temperature(model, proc.get('predefined_temperatures', [])):
        return False, model
    if proc.get('predefined_temperatures', []):
        ok('Predefined temperature check passed')

    return True, model



def check_keyword_bcs_loads_b(model, proc):
    text = ''
    try:
        model.keywordBlock.synchVersions(storeNodesAndElements=False)
        text = '\n'.join([str(x) for x in model.keywordBlock.sieBlocks]).upper()
    except Exception:
        return fail('Cannot read keyword block text for B-task fallback')

    # load type pattern
    for req in proc.get('load_signatures', []):
        types = [ci(x) for x in req.get('type_any', [])]
        ok = False
        for tp in types:
            if 'PRESSURE' in tp and ('*DLOAD' in text or '*DSLOAD' in text):
                ok = True
            if 'MOMENT' in tp and '*CLOAD' in text:
                ok = True
            if 'SURFACETRACTION' in tp and ('*DSLOAD' in text or 'TRVEC' in text):
                ok = True
        if not ok:
            return fail('Keyword load fallback not found for %s' % str(types))

    return True

def check_odb_completion(odb_path):
    solver_spec = PROCESS_SPEC.get('solver', {})
    min_frames = int(solver_spec.get('min_frames', 1))
    require_mode_frames = bool(solver_spec.get('require_mode_frames', False))

    odb = None
    try:
        odb = openOdb(path=odb_path, readOnly=True)

        step_key = find_key_ci(odb.steps, PROCESS_SPEC['artifact']['step_name'])
        if step_key is None:
            return fail('Target step not found in ODB: ' + str(PROCESS_SPEC['artifact']['step_name']))

        step = odb.steps[step_key]
        nf = len(step.frames)
        if nf < min_frames:
            return fail('ODB frame count too low: obs=%s min=%s' % (str(nf), str(min_frames)))

        if require_mode_frames and nf <= 1:
            return fail('ODB mode frames missing (need >1 frame for perturbation step)')

        try:
            status = ci(getattr(odb.diagnosticData, 'jobStatus', ''))
            if status and ('ABORT' in status or 'TERMINAT' in status):
                return fail('ODB diagnostic status indicates failure: ' + str(status))
        except Exception:
            pass

        last_frame = step.frames[-1]
        try:
            fv = safe_float(last_frame.frameValue, None)
            if fv is None:
                return fail('Last frame value is not numeric')
        except Exception:
            return fail('Cannot read last frame value')

        ok('ODB completion check passed')
        return True

    except Exception as e:
        log(traceback.format_exc())
        return fail('Cannot open/inspect ODB: ' + str(e))
    finally:
        try:
            if odb is not None:
                odb.close()
        except Exception:
            pass


def artifact_check(desktop):
    job_name = PROCESS_SPEC['artifact']['job_name']
    cae_path = os.path.join(desktop, job_name + '.cae')
    odb_path = os.path.join(desktop, job_name + '.odb')

    if not os.path.exists(cae_path):
        return fail('CAE missing: ' + cae_path), None, None
    if not os.path.exists(odb_path):
        return fail('ODB missing: ' + odb_path), None, None

    return True, cae_path, odb_path


def check_task():
    job_name = PROCESS_SPEC['artifact']['job_name']
    desktop = get_desktop(job_name)
    detail_path = os.path.join(desktop, 'eval_detail.txt')
    result_path = os.path.join(desktop, 'eval_result.txt')

    ok_art, cae_path, odb_path = artifact_check(desktop)
    if not ok_art:
        output_result(False, detail_path=detail_path, result_path=result_path)
        return False
    ok('Artifact check passed')

    ok_cae, _model = check_cae_process(cae_path)
    if not ok_cae:
        output_result(False, detail_path=detail_path, result_path=result_path)
        return False

    if not check_odb_completion(odb_path):
        output_result(False, detail_path=detail_path, result_path=result_path)
        return False

    output_result(True, detail_path=detail_path, result_path=result_path)
    return True


def main():
    check_task()


if __name__ == '__main__':
    main()