caexfer 0.2.0

Transfer finite-element meshes, voxel occupancy, and results across CAE formats
Documentation
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
use std::collections::BTreeSet;
use std::io::Write;

use crate::core::{
    CellKind, Dataset, Error, FieldLocation, Mesh, Result, VoxelGrid, boundary_surface,
};
use crate::formats::{bdf, exodus, frd, inp, msh, op2, stl, su2, unv, vox, vti, vtk, vtu};

use super::load_match_grid;
use super::tetra;
use super::{
    AssumptionKind, ConversionReport, Format, InfillShape, Omission, Options, ReadResult, Stage,
};

/// Remove unmapped solver property IDs and return the number removed.
fn omit_property_ids(mesh: &mut Mesh) -> usize {
    mesh.cells
        .iter_mut()
        .map(|cell| usize::from(cell.property_id.take().is_some()))
        .sum()
}

/// Remove volume-only points while keeping surviving point fields aligned.
fn compact_mesh_points(dataset: &mut Dataset) -> usize {
    let original_count = dataset.mesh.points.len();
    let used: BTreeSet<usize> = dataset
        .mesh
        .cells
        .iter()
        .flat_map(|cell| cell.connectivity.iter().copied())
        .collect();
    let mut index_map = vec![0; original_count];
    for (new, &old) in used.iter().enumerate() {
        index_map[old] = new;
    }
    dataset.mesh.points = std::mem::take(&mut dataset.mesh.points)
        .into_iter()
        .enumerate()
        .filter_map(|(old, point)| {
            if used.contains(&old) {
                Some(point)
            } else {
                None
            }
        })
        .collect();
    for cell in &mut dataset.mesh.cells {
        for index in &mut cell.connectivity {
            *index = index_map[*index];
        }
    }
    for field in &mut dataset.fields {
        let width = field.components.len();
        field.values = used
            .iter()
            .flat_map(|&old| field.values[old * width..(old + 1) * width].iter().copied())
            .collect();
    }
    original_count - dataset.mesh.points.len()
}

/// Check route options that do not depend on source contents.
pub(crate) fn validate_route_options(target: Format, options: &Options) -> Result<()> {
    if options.infill_shape.is_some() != options.infill_unit_size.is_some()
        || options
            .infill_unit_size
            .is_some_and(|size| !size.is_finite() || size <= 0.0)
        || (options.infill_shape.is_some() && options.surface)
        || (options.match_grid.is_some()
            && (options.infill_shape.is_some() || options.surface || options.gmsh.is_some()))
    {
        return Err(Error::new(
            "E_USAGE",
            "infill requires a shape and positive unit size and cannot use another geometry projection",
        ));
    }
    if options.gmsh.is_some() && options.infill_shape != Some(InfillShape::Tet4) {
        return Err(Error::new("E_USAGE", "gmsh requires tet4 infill"));
    }
    if options.surface
        && matches!(
            target,
            Format::Vti | Format::Vox | Format::Op2 | Format::Pch
        )
    {
        return Err(Error::new(
            "E_USAGE",
            "--surface requires volume input and a mesh output",
        ));
    }
    if options.smooth_iterations > 50 || (options.smooth_iterations > 0 && target != Format::Stl) {
        return Err(Error::new(
            "E_USAGE",
            "smooth_iterations must be 1-50 and applies only to VTI/VOX to STL",
        ));
    }
    Ok(())
}

/// Write a previously read source in another supported format.
///
/// OP2 output requires exactly one nodal displacement field. A caller may
/// supply a synthetic field with a `SyntheticZero` assumption to mark that
/// provenance in the OP2 title.
///
/// # Errors
///
/// Returns a validation, representability, adapter, or output-stream error
/// for the selected destination format.
///
#[allow(clippy::too_many_lines)] // Destination branches share omission and assumption tracking.
pub fn convert(
    mut source: ReadResult,
    target: Format,
    options: &Options,
    writer: &mut dyn Write,
) -> Result<ConversionReport> {
    // Keep synthetic OP2 provenance on the typed source notice.
    let source_fields = source.dataset.fields.len();
    let assumed_zero = source
        .omissions
        .iter()
        .any(|item| item.assumption == Some(AssumptionKind::SyntheticZero));
    validate_route_options(target, options)?;
    let tetrahedralize = options.infill_shape == Some(InfillShape::Tet4);
    let voxel_target = matches!(target, Format::Vti | Format::Vox);
    let reference = load_match_grid(options)?;
    let grid_infill = match options.infill_shape {
        Some(InfillShape::Hex8) => Some(CellKind::Hex8),
        Some(InfillShape::Wedge6) => Some(CellKind::Wedge6),
        Some(InfillShape::Pyramid5) => Some(CellKind::Pyramid5),
        _ if reference.is_some() => Some(CellKind::Hex8),
        _ => None,
    };
    if (options.infill_shape.is_some() || reference.is_some()) && source.voxel_grid.is_some() {
        return Err(Error::new("E_USAGE", "infill options require mesh input"));
    }
    if voxel_target
        && options
            .infill_shape
            .is_some_and(|shape| shape != InfillShape::Hex8)
    {
        return Err(Error::new("E_USAGE", "VTI/VOX output requires hex8 infill"));
    }
    if grid_infill.is_some()
        && !voxel_target
        && (matches!(target, Format::Stl | Format::Op2 | Format::Pch)
            || source.dataset.mesh.cells.is_empty()
            || source
                .dataset
                .mesh
                .cells
                .iter()
                .any(|cell| cell.kind.dimension() != 2))
    {
        return Err(Error::new(
            "E_USAGE",
            "grid infill requires a closed surface mesh and a volume-mesh output",
        ));
    }
    if tetrahedralize
        && (source.voxel_grid.is_some()
            || matches!(
                target,
                Format::Vti | Format::Vox | Format::Stl | Format::Op2 | Format::Pch
            )
            || source.dataset.mesh.cells.is_empty()
            || source
                .dataset
                .mesh
                .cells
                .iter()
                .any(|cell| cell.kind.dimension() != 2))
    {
        return Err(Error::new(
            "E_USAGE",
            "tet4 infill requires a closed surface mesh and a mesh output",
        ));
    }
    if options.surface
        && !source
            .dataset
            .mesh
            .cells
            .iter()
            .any(|cell| cell.kind.dimension() == 3)
    {
        return Err(Error::new(
            "E_USAGE",
            "--surface requires volume input and a mesh output",
        ));
    }
    if options.smooth_iterations > 0 && source.voxel_grid.is_none() {
        return Err(Error::new(
            "E_USAGE",
            "smooth_iterations must be 1-50 and applies only to VTI/VOX to STL",
        ));
    }
    if options.surface {
        let lower = source
            .dataset
            .mesh
            .cells
            .iter()
            .filter(|cell| cell.kind.dimension() < 3)
            .count();
        let cell_fields = source
            .dataset
            .fields
            .iter()
            .filter(|field| field.location == FieldLocation::Cell)
            .count();
        let properties = source
            .dataset
            .mesh
            .cells
            .iter()
            .filter(|cell| cell.property_id.is_some())
            .count();
        let node_sets = source.dataset.mesh.node_sets.len();
        let cell_sets = source.dataset.mesh.cell_sets.len();
        source.dataset.mesh = boundary_surface(&source.dataset.mesh)?;
        source
            .dataset
            .fields
            .retain(|field| field.location == FieldLocation::Point);
        let interior_points = compact_mesh_points(&mut source.dataset);
        source.omissions.push(Omission::new(
            Stage::Destination,
            format!("external volume faces extracted as triangles; {lower} lower-dimensional cell(s) excluded; original element IDs replaced"),
        ));
        if interior_points > 0 {
            source.omissions.push(Omission::new(
                Stage::Destination,
                format!("{interior_points} interior point(s) and their field values omitted during surface extraction"),
            ));
        }
        for (count, label) in [
            (cell_fields, "cell field(s)"),
            (properties, "property ID(s)"),
            (node_sets, "named node set(s)"),
            (cell_sets, "named cell set(s)"),
        ] {
            if count > 0 {
                source.omissions.push(Omission::new(
                    Stage::Destination,
                    format!("{count} {label} omitted during surface extraction"),
                ));
            }
        }
    }
    if tetrahedralize {
        let fields = source.dataset.fields.len();
        let properties = source
            .dataset
            .mesh
            .cells
            .iter()
            .filter(|cell| cell.property_id.is_some())
            .count();
        let node_sets = source.dataset.mesh.node_sets.len();
        let cell_sets = source.dataset.mesh.cell_sets.len();
        let triangles = boundary_surface(&source.dataset.mesh)?;
        source.dataset = tetra::tetrahedralize(&triangles, options, options.infill_unit_size)?;
        compact_mesh_points(&mut source.dataset);
        source.omissions.push(Omission::new(
            Stage::Destination,
            "closed surface remeshed by Gmsh into conforming Tet4 cells; boundary triangles, node/element IDs and coordinates may change through STL float32 projection",
        ));
        for (count, label) in [
            (fields, "numeric field(s)"),
            (properties, "property ID(s)"),
            (node_sets, "named node set(s)"),
            (cell_sets, "named cell set(s)"),
        ] {
            if count > 0 {
                source.omissions.push(Omission::new(
                    Stage::Destination,
                    format!("{count} {label} omitted during tetrahedralization"),
                ));
            }
        }
    }
    if target == Format::Stl && source.voxel_grid.is_some() && !options.surface {
        source.dataset.mesh = source
            .voxel_grid
            .as_ref()
            .ok_or_else(|| Error::new("E_VOXEL", "missing voxel grid"))?
            .surface_mesh()?;
        if options.smooth_iterations > 0 {
            crate::core::smooth_surface(&mut source.dataset.mesh, options.smooth_iterations)?;
            source.omissions.push(Omission::new(
                Stage::Destination,
                format!("voxel STL surface smoothed for {} Taubin cycle(s); geometry no longer exactly follows voxel boundaries", options.smooth_iterations),
            ));
        }
        source.omissions.push(Omission::new(
            Stage::Destination,
            if options.smooth_iterations > 0 {
                "exposed voxel faces triangulated before smoothing; STL detail remains limited by grid resolution"
            } else {
                "exposed voxel faces triangulated; stair-step surface retains grid resolution"
            },
        ));
    } else if target == Format::Stl
        && !options.surface
        && source
            .dataset
            .mesh
            .cells
            .iter()
            .any(|c| c.kind != crate::core::CellKind::Triangle3)
    {
        let volume = source
            .dataset
            .mesh
            .cells
            .iter()
            .any(|c| c.kind.dimension() == 3);
        let lower = source
            .dataset
            .mesh
            .cells
            .iter()
            .filter(|c| c.kind.dimension() < 3)
            .count();
        let properties = source
            .dataset
            .mesh
            .cells
            .iter()
            .filter(|c| c.property_id.is_some())
            .count();
        let node_sets = source.dataset.mesh.node_sets.len();
        let cell_sets = source.dataset.mesh.cell_sets.len();
        source.dataset.mesh = boundary_surface(&source.dataset.mesh)?;
        for (count, label) in [
            (properties, "property ID(s)"),
            (node_sets, "named node set(s)"),
            (cell_sets, "named cell set(s)"),
        ] {
            if count > 0 {
                source.omissions.push(Omission::new(
                    Stage::Destination,
                    format!("{count} {label} omitted during STL boundary extraction"),
                ));
            }
        }
        source.omissions.push(Omission::new(Stage::Destination,
            if volume { format!("external volume faces triangulated for STL; {lower} lower-dimensional cell(s) excluded") }
            else { "quadrilateral faces triangulated for STL".to_owned() }));
    } else if !options.surface
        && grid_infill.is_some()
        && !matches!(target, Format::Stl | Format::Vti | Format::Vox)
    {
        let fields = source.dataset.fields.len();
        let properties = source
            .dataset
            .mesh
            .cells
            .iter()
            .filter(|cell| cell.property_id.is_some())
            .count();
        let node_sets = source.dataset.mesh.node_sets.len();
        let cell_sets = source.dataset.mesh.cell_sets.len();
        let spacing = options
            .infill_unit_size
            .or_else(|| reference.as_ref().map(|grid| grid.spacing))
            .ok_or_else(|| Error::new("E_USAGE", "missing infill unit size"))?;
        let grid = VoxelGrid::from_surface_with_reference(
            &source.dataset.mesh,
            spacing,
            reference.as_ref(),
        )?;
        let kind = grid_infill.unwrap_or(CellKind::Hex8);
        source.dataset.mesh = grid.volume_mesh_as(kind)?;
        source.dataset.fields.clear();
        source.omissions.push(Omission::new(
            Stage::Destination,
            format!("closed surface sampled at voxel centers and emitted as {kind:?} volume cells; original node and element IDs replaced"),
        ));
        if fields > 0 {
            source.omissions.push(Omission::new(
                Stage::Destination,
                format!("{fields} numeric field(s) omitted during volume meshing"),
            ));
        }
        for (count, label) in [
            (properties, "property ID(s)"),
            (node_sets, "named node set(s)"),
            (cell_sets, "named cell set(s)"),
        ] {
            if count > 0 {
                source.omissions.push(Omission::new(
                    Stage::Destination,
                    format!("{count} {label} omitted during volume meshing"),
                ));
            }
        }
    }
    let mut voxel_summary = None;
    let dataset = &mut source.dataset;
    let omissions = &mut source.omissions;
    if target != Format::Su2 {
        if !dataset.mesh.node_sets.is_empty() {
            omissions.push(Omission::new(
                Stage::Destination,
                format!(
                    "{} named node set(s) omitted by {}",
                    dataset.mesh.node_sets.len(),
                    target.name()
                ),
            ));
        }
        if !dataset.mesh.cell_sets.is_empty() {
            omissions.push(Omission::new(
                Stage::Destination,
                format!(
                    "{} named cell set(s) omitted by {}",
                    dataset.mesh.cell_sets.len(),
                    target.name()
                ),
            ));
        }
        dataset.mesh.node_sets.clear();
        dataset.mesh.cell_sets.clear();
    }
    match target {
        Format::Vti | Format::Vox => {
            let grid = if let Some(grid) = &source.voxel_grid {
                grid.clone()
            } else {
                let spacing = options
                    .infill_unit_size
                    .or_else(|| reference.as_ref().map(|grid| grid.spacing))
                    .ok_or_else(|| {
                        Error::new(
                            "E_USAGE",
                            "mesh to VTI/VOX requires hex8 infill or --match-grid",
                        )
                    })?;
                omissions.push(Omission::new(Stage::Destination,
                    "closed surface sampled at voxel centers; features below grid spacing may disappear"));
                let surface = if dataset.mesh.cells.iter().any(|c| c.kind.dimension() == 3) {
                    let excluded = dataset
                        .mesh
                        .cells
                        .iter()
                        .filter(|c| c.kind.dimension() < 3)
                        .count();
                    omissions.push(Omission::new(
                        Stage::Destination,
                        format!("external faces of volume cells extracted before voxel-center sampling; {excluded} lower-dimensional cell(s) excluded"),
                    ));
                    boundary_surface(&dataset.mesh)?
                } else {
                    dataset.mesh.clone()
                };
                if let Some(reference) = &reference {
                    VoxelGrid::from_surface_with_reference(&surface, spacing, Some(reference))?
                } else {
                    VoxelGrid::from_surface(&surface, spacing)?
                }
            };
            if dataset.mesh.cells.iter().any(|c| c.property_id.is_some()) {
                omissions.push(Omission::new(
                    Stage::Destination,
                    "mesh property IDs have no binary occupancy mapping",
                ));
            }
            if !dataset.fields.is_empty() {
                omissions.push(Omission::new(
                    Stage::Destination,
                    format!(
                        "{} numeric field(s) omitted from binary occupancy",
                        dataset.fields.len()
                    ),
                ));
            }
            if target == Format::Vox {
                if grid
                    .origin
                    .iter()
                    .any(|&v| v.to_bits() != 0.0_f64.to_bits())
                    || grid.spacing.to_bits() != 1.0_f64.to_bits()
                {
                    omissions.push(Omission::new(
                        Stage::Destination,
                        "VOX stores grid indices only; physical origin and spacing are omitted",
                    ));
                }
                vox::write(&grid, writer)?;
            } else {
                vti::write(&grid, writer)?;
            }
            voxel_summary = Some((grid.dims, grid.occupied.iter().filter(|&&v| v != 0).count()));
        }
        Format::Vtu => {
            // VTU has one array per location and name; multiple source steps
            // require the caller to select a step first.
            let mut seen = BTreeSet::new();
            for field in &dataset.fields {
                if !seen.insert((field.location as u8, field.name.as_str())) {
                    return Err(Error::new(
                        "E_VTU",
                        "multiple steps of one field; choose --step",
                    ));
                }
            }
            vtu::write_data(dataset, writer)?;
        }
        Format::Vtk => {
            for field in &dataset.fields {
                if field.step.is_some() || field.time.is_some() {
                    omissions.push(Omission::new(
                        Stage::Destination,
                        format!(
                            "field {} loses step/time metadata in legacy VTK",
                            field.name
                        ),
                    ));
                }
                if field
                    .components
                    .iter()
                    .enumerate()
                    .any(|(index, name)| name != &format!("C{}", index + 1))
                {
                    omissions.push(Omission::new(
                        Stage::Destination,
                        format!("field {} loses component labels in legacy VTK", field.name),
                    ));
                }
            }
            vtk::write_data(dataset, writer)?;
        }
        Format::Stl => {
            if !dataset.fields.is_empty() {
                omissions.push(Omission::new(
                    Stage::Destination,
                    format!(
                        "{} numeric field(s) omitted from STL triangle surface",
                        dataset.fields.len()
                    ),
                ));
            }
            if omit_property_ids(&mut dataset.mesh) > 0 {
                omissions.push(Omission::new(
                    Stage::Destination,
                    "property IDs have no STL mapping",
                ));
            }
            omissions.push(Omission::new(
                Stage::Destination,
                "STL has no node or element IDs or shared-vertex identity; binary coordinates use float32",
            ));
            dataset.fields.clear();
            stl::write_data(dataset, writer)?;
        }
        Format::Su2 => {
            if !dataset.fields.is_empty() {
                omissions.push(Omission::new(
                    Stage::Destination,
                    format!(
                        "{} numeric field(s) omitted from SU2 mesh",
                        dataset.fields.len()
                    ),
                ));
            }
            if !dataset.mesh.node_sets.is_empty() {
                omissions.push(Omission::new(
                    Stage::Destination,
                    format!(
                        "{} named node set(s) have no SU2 marker mapping",
                        dataset.mesh.node_sets.len()
                    ),
                ));
            }
            let dimension = dataset
                .mesh
                .cells
                .iter()
                .map(|cell| cell.kind.dimension())
                .max()
                .unwrap_or(0);
            let unrepresented = dataset
                .mesh
                .cell_sets
                .iter()
                .filter(|set| set.dimension != dimension.saturating_sub(1))
                .count();
            if unrepresented > 0 {
                omissions.push(Omission::new(
                    Stage::Destination,
                    format!("{unrepresented} cell set(s) are not SU2 boundary markers"),
                ));
            }
            dataset.mesh.node_sets.clear();
            dataset
                .mesh
                .cell_sets
                .retain(|set| set.dimension == dimension.saturating_sub(1));
            if omit_property_ids(&mut dataset.mesh) > 0 {
                omissions.push(Omission::new(
                    Stage::Destination,
                    "property IDs have no SU2 mapping",
                ));
            }
            omissions.push(Omission::new(
                Stage::Destination,
                "SU2 uses positional connectivity; original node and element IDs are not encoded",
            ));
            dataset.fields.clear();
            su2::write_data(dataset, writer)?;
        }
        Format::Unv => {
            if !dataset.fields.is_empty() {
                omissions.push(Omission::new(
                    Stage::Destination,
                    format!(
                        "{} numeric field(s) omitted from UNV geometry",
                        dataset.fields.len()
                    ),
                ));
            }
            let properties = omit_property_ids(&mut dataset.mesh);
            if properties > 0 {
                omissions.push(Omission::new(
                    Stage::Destination,
                    format!("{properties} property ID(s) omitted from UNV geometry"),
                ));
            }
            dataset.fields.clear();
            unv::write_data(dataset, writer)?;
        }
        Format::Exodus => {
            let properties = omit_property_ids(&mut dataset.mesh);
            if properties > 0 {
                omissions.push(Omission::new(
                    Stage::Destination,
                    format!("{properties} solver property ID(s) have no Exodus block mapping"),
                ));
            }
            omissions.push(Omission::new(
                Stage::Destination,
                "Exodus element block IDs are generated from cell topology; no source block identity is inferred",
            ));
            exodus::write_data(dataset, writer)?;
        }
        Format::Msh => {
            // MSH stores numeric tuples but loses these component labels and
            // this exporter's unmapped property IDs.
            let missing_steps = dataset
                .fields
                .iter()
                .filter(|field| field.step.is_none())
                .count();
            if missing_steps > 0 {
                omissions.push(Omission::new(
                    Stage::Destination,
                    format!("{missing_steps} field(s) without step metadata use MSH step 0"),
                ));
            }
            let labels = dataset
                .fields
                .iter()
                .filter(|field| {
                    field
                        .components
                        .iter()
                        .enumerate()
                        .any(|(i, name)| name != &format!("C{}", i + 1))
                })
                .count();
            if labels > 0 {
                omissions.push(Omission::new(
                    Stage::Destination,
                    format!("{labels} field(s) lose component labels in MSH NodeData/ElementData"),
                ));
            }
            let props = omit_property_ids(&mut dataset.mesh);
            if props > 0 {
                omissions.push(Omission::new(
                    Stage::Destination,
                    format!("{props} property IDs have no MSH entity mapping in this exporter"),
                ));
            }
            msh::write_version(dataset, options.msh_version.unwrap_or_default(), writer)?;
        }
        Format::Inp | Format::Bdf => {
            // These writers emit geometry-only solver input, so result fields
            // remain accounted for in the report instead of silently vanishing.
            if !dataset.fields.is_empty() {
                omissions.push(Omission::new(
                    Stage::Destination,
                    format!(
                        "{} numeric field(s) omitted from geometry-only solver input",
                        dataset.fields.len()
                    ),
                ));
            }
            if target == Format::Inp {
                // The bounded INP writer has no property definition mapping.
                let props = omit_property_ids(&mut dataset.mesh);
                if props > 0 {
                    omissions.push(Omission::new(
                        Stage::Destination,
                        format!("{props} property IDs omitted from INP"),
                    ));
                }
                inp::write(&dataset.mesh, writer)?;
            } else {
                // BDF requires a PID on these element cards; the writer uses
                // placeholder 1 where the source mesh has none.
                let missing = dataset
                    .mesh
                    .cells
                    .iter()
                    .filter(|cell| cell.property_id.is_none())
                    .count();
                if missing > 0 {
                    omissions.push(Omission::new(
                        Stage::Destination,
                        format!("{missing} BDF element(s) use placeholder PID 1; no property cards are emitted"),
                    ));
                }
                bdf::mesh::write(&dataset.mesh, writer)?;
            }
        }
        Format::Frd => {
            // The supported FRD result blocks are nodal, so cell fields must
            // be removed before calling its writer.
            let before = dataset.fields.len();
            dataset
                .fields
                .retain(|field| field.location == FieldLocation::Point);
            if dataset.fields.len() != before {
                omissions.push(Omission::new(
                    Stage::Destination,
                    format!(
                        "{} cell field(s) have no direct FRD nodal representation",
                        before - dataset.fields.len()
                    ),
                ));
            }
            for field in &mut dataset.fields {
                // FRD's displacement name and metadata layout differ from
                // the generic field model; record each normalization.
                if field.name.starts_with("DISPLACEMENT_SUBCASE_") {
                    omissions.push(Omission::new(
                        Stage::Destination,
                        format!(
                            "field {} is named DISP in FRD; subcase name is not retained",
                            field.name
                        ),
                    ));
                    field.name = "DISP".into();
                }
                if field.step.is_none() || field.time.is_none() {
                    omissions.push(Omission::new(
                        Stage::Destination,
                        format!(
                            "field {} uses FRD step/time 0 where metadata is absent",
                            field.name
                        ),
                    ));
                }
            }
            if omit_property_ids(&mut dataset.mesh) > 0 {
                omissions.push(Omission::new(
                    Stage::Destination,
                    "BDF property IDs have no direct FRD mesh mapping",
                ));
            }
            omissions.push(Omission::new(
                Stage::Destination,
                "FRD ASCII E12.5 rounds coordinates and field values to six significant digits",
            ));
            frd::write(dataset, writer)?;
        }
        Format::Op2 => {
            // The adapter writes a single real nodal displacement table.
            let mut matches = dataset.fields.iter().filter(|field| {
                let name = field.name.to_ascii_uppercase();
                field.location == FieldLocation::Point
                    && (name == "DISP"
                        || name == "DISPLACEMENT"
                        || name.starts_with("DISPLACEMENT_SUBCASE_"))
                    && matches!(field.components.len(), 3 | 6)
            });
            let (Some(field), None) = (matches.next(), matches.next()) else {
                return Err(Error::new(
                    "E_OP2",
                    "OP2 output requires exactly one 3- or 6-component nodal DISP field; select one result step",
                ));
            };

            // Recover a subcase encoded in the field name, defaulting to 1
            // for generic DISP names.
            let subcase = field
                .name
                .to_ascii_uppercase()
                .strip_prefix("DISPLACEMENT_SUBCASE_")
                .and_then(|text| text.parse().ok())
                .unwrap_or(1);
            if field.components.len() == 3 && options.zero_missing_rotations {
                omissions.push(Omission::assumed(AssumptionKind::ZeroRotations, "rotational displacement components R1/R2/R3 filled with typed float 0.0 by explicit request"));
            }
            if field.step.is_some_and(|step| step != 0) {
                omissions.push(Omission::new(
                    Stage::Destination,
                    "source step number is not encoded in the one-step OP2 table",
                ));
            }
            if dataset.fields.len() > 1 {
                omissions.push(Omission::new(
                    Stage::Destination,
                    format!(
                        "{} other numeric field(s) omitted from OP2 displacement output",
                        dataset.fields.len() - 1
                    ),
                ));
            }
            omissions.push(Omission::new(
                Stage::Destination,
                "OP2 contains no mesh; retain or export a matching companion mesh",
            ));
            omissions.push(Omission::new(
                Stage::Destination,
                "OP2 real displacement values use float32 precision",
            ));

            // The adapter builds the binary table before it reaches the
            // caller's output stream.
            let bytes = op2::write_displacements(
                dataset,
                field,
                subcase,
                options.zero_missing_rotations,
                assumed_zero,
            )?;
            writer.write_all(&bytes)?;
        }
        Format::Pch => return Err(Error::new("E_FORMAT", "PCH writing is not supported")),
    }
    Ok(ConversionReport {
        points: dataset.mesh.points.len(),
        cells: dataset.mesh.cells.len(),
        fields: source_fields,
        omissions: source.omissions,
        voxel_grid: voxel_summary,
        mesh_output: None,
    })
}