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
use std::fs::File;
use std::io::Read;
use std::path::Path;

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

use super::{AssumptionKind, Format, Omission, Options, ReadResult, Stage};

/// Bound non-BDF file reads with both a metadata check and an actual read cap.
/// The second check handles files growing between the metadata and read calls.
fn read_limited(path: &Path, max_bytes: usize) -> Result<Vec<u8>> {
    // Metadata catches ordinary oversize files without allocating for them.
    if std::fs::metadata(path)?.len() > max_bytes as u64 {
        return Err(Error::new(
            "E_LIMIT",
            format!("input exceeds {max_bytes} bytes"),
        ));
    }

    // The extra byte detects a file that grew after the metadata check.
    let mut bytes = Vec::new();
    File::open(path)?
        .take(max_bytes.saturating_add(1) as u64)
        .read_to_end(&mut bytes)?;
    if bytes.len() > max_bytes {
        return Err(Error::new(
            "E_LIMIT",
            format!("input exceeds {max_bytes} bytes"),
        ));
    }
    Ok(bytes)
}

/// Decode a bounded text input with its format-specific UTF-8 error.
fn read_text(
    path: &Path,
    max_bytes: usize,
    code: &'static str,
    message: &'static str,
) -> Result<String> {
    String::from_utf8(read_limited(path, max_bytes)?).map_err(|_| Error::new(code, message))
}

/// Load a Nastran result companion mesh and report source projection losses.
/// BDF grid output frames are checked; other formats require the caller's
/// explicit basic-frame assertion because that metadata is unavailable.
fn read_result_mesh(
    path: &Path,
    options: &Options,
    result: Format,
) -> Result<(Mesh, Vec<Omission>)> {
    let format = Format::from_input_path(path)?;
    match format {
        Format::Bdf => {
            // BDF exposes GRID output frames, so verify them directly.
            let projection = bdf::mesh::read_from(File::open(path)?, options.max_bytes)?;
            if projection.has_nonbasic_output_frame {
                return Err(Error::new(
                    if result == Format::Pch {
                        "E_PCH"
                    } else {
                        "E_OP2"
                    },
                    "nonbasic GRID CD requires displacement frame transformation",
                ));
            }

            // Keep source losses from the companion deck visible in the
            // result conversion report.
            let omissions = projection
                .omissions
                .into_iter()
                .map(|item| {
                    Omission::new(
                        Stage::Source,
                        format!("BDF {} × {}: {}", item.category, item.count, item.detail),
                    )
                })
                .collect();
            Ok((projection.mesh, omissions))
        }
        Format::Op2 | Format::Pch => Err(Error::new(
            "E_USAGE",
            "result companion mesh must be a mesh-bearing format",
        )),
        _ => {
            // Other mesh formats carry no GRID CD, requiring an explicit
            // caller assertion before pairing them with Nastran displacements.
            if !options.assume_basic_frame {
                return Err(Error::new(
                    "E_USAGE",
                    "non-BDF result mesh lacks GRID CD; set Options.assume_basic_frame only when coordinates and displacements use the basic frame",
                ));
            }

            // A companion supplies geometry only; result-selection options
            // must not be reapplied while reading its mesh-bearing file.
            let mesh_options = Options {
                input_format: None,
                mesh: None,
                assume_basic_frame: false,
                subcase: None,
                step: None,
                ..options.clone()
            };
            let read = read_path(path, &mesh_options)?;
            if read.generated_point_ids {
                return Err(Error::new(
                    "E_USAGE",
                    "result companion mesh has assigned point IDs; original node IDs are required",
                ));
            }

            // A companion contributes geometry only; report ignored fields.
            let mut omissions = read
                .omissions
                .into_iter()
                .map(|item| {
                    Omission::new(
                        item.stage,
                        format!("companion {}: {}", format.name(), item.detail),
                    )
                })
                .collect::<Vec<_>>();
            if !read.dataset.fields.is_empty() {
                omissions.push(Omission::new(
                    Stage::Source,
                    format!(
                        "{} numeric field(s) in companion mesh ignored",
                        read.dataset.fields.len()
                    ),
                ));
            }
            omissions.push(Omission::assumed(
                AssumptionKind::BasicFrame,
                format!("companion {} has no GRID CD; basic-frame coordinates and {} displacements are assumed for this projection", format.name(), result.name().to_ascii_uppercase()),
            ));
            Ok((read.dataset.mesh, omissions))
        }
    }
}

/// Read a supported source file into a mesh and fields, reporting omitted data.
/// OP2/PCH input requires `options.mesh`. A non-BDF
/// companion additionally requires `options.assume_basic_frame`.
///
/// # Errors
///
/// Returns an option or format error, an I/O or size-limit error, or a source
/// reader error when the requested projection cannot be represented safely.
///
#[allow(clippy::too_many_lines)] // Format-specific read branches share projection reporting.
pub fn read_path(path: &Path, options: &Options) -> Result<ReadResult> {
    // An explicit source format overrides suffix-based selection.
    let format = options
        .input_format
        .map_or_else(|| Format::from_input_path(path), Ok)?;

    // Reject result-selection and frame options on formats that cannot use
    // them before opening or parsing any source file.
    if !matches!(format, Format::Op2 | Format::Pch)
        && (options.mesh.is_some() || options.subcase.is_some())
    {
        return Err(Error::new(
            "E_USAGE",
            "mesh and subcase apply only to OP2/PCH input",
        ));
    }
    if !matches!(format, Format::Op2 | Format::Pch) && options.assume_basic_frame {
        return Err(Error::new(
            "E_USAGE",
            "basic-frame assertion applies only to OP2/PCH input",
        ));
    }
    if !matches!(
        format,
        Format::Op2 | Format::Pch | Format::Frd | Format::Exodus
    ) && options.step.is_some()
    {
        return Err(Error::new(
            "E_USAGE",
            "step applies only to OP2, PCH, FRD, or Exodus input",
        ));
    }
    let mut generated_point_ids = false;
    let mut voxel_grid = None;
    let (dataset, omissions) = match format {
        Format::Bdf => {
            // BDF projection can lose solver cards while retaining geometry.
            let projection = bdf::mesh::read_from(File::open(path)?, options.max_bytes)?;
            let omissions = projection
                .omissions
                .into_iter()
                .map(|item| {
                    Omission::new(
                        Stage::Source,
                        format!("{} × {}: {}", item.category, item.count, item.detail),
                    )
                })
                .collect();
            (
                Dataset {
                    mesh: projection.mesh,
                    fields: Vec::new(),
                },
                omissions,
            )
        }
        Format::Vtu => {
            let text = read_text(path, options.max_bytes, "E_VTU", "VTU must be UTF-8 XML")?;
            let projection = vtu::read_projection(&text)?;
            generated_point_ids = projection.generated_point_ids;
            let mut omissions = Vec::new();
            if projection.generated_point_ids {
                omissions.push(Omission::new(
                    Stage::Source,
                    "VTU has no original point IDs; assigned one-based IDs",
                ));
            }
            if projection.generated_cell_ids {
                omissions.push(Omission::new(
                    Stage::Source,
                    "VTU has no original cell IDs; assigned one-based IDs",
                ));
            }
            (projection.dataset, omissions)
        }
        Format::Vti => {
            let text = read_text(path, options.max_bytes, "E_VTI", "VTI must be UTF-8 XML")?;
            let grid = vti::read(&text)?;
            let mesh = grid.volume_mesh()?;
            voxel_grid = Some(grid);
            generated_point_ids = true;
            (
                Dataset {
                    mesh,
                    fields: Vec::new(),
                },
                vec![Omission::new(
                    Stage::Source,
                    "VTI occupancy has no original mesh node or element IDs; grid IDs were generated",
                )],
            )
        }
        Format::Vox => {
            let bytes = read_limited(path, options.max_bytes)?;
            let projection = vox::read(&bytes)?;
            let mesh = projection.grid.volume_mesh()?;
            voxel_grid = Some(projection.grid);
            generated_point_ids = true;
            let mut omissions = vec![Omission::new(
                Stage::Source,
                "VOX has no physical origin or spacing; origin 0 and unit spacing are used",
            )];
            omissions.push(Omission::new(
                Stage::Source,
                "VOX has no original mesh node or element IDs; grid IDs were generated",
            ));
            omissions.push(Omission::new(
                Stage::Source,
                format!("VOX color indices and palette are omitted from occupancy ({} non-default color index voxel(s), palette present: {})",
                    projection.colored_voxels, projection.palette),
            ));
            (
                Dataset {
                    mesh,
                    fields: Vec::new(),
                },
                omissions,
            )
        }
        Format::Vtk => {
            let text = read_text(
                path,
                options.max_bytes,
                "E_VTK",
                "legacy VTK must be ASCII text",
            )?;
            let projection = vtk::read_projection(&text)?;
            generated_point_ids = projection.generated_point_ids;
            let mut omissions = Vec::new();
            if projection.generated_point_ids {
                omissions.push(Omission::new(
                    Stage::Source,
                    "legacy VTK has no original point IDs; assigned one-based IDs",
                ));
            }
            if projection.generated_cell_ids {
                omissions.push(Omission::new(
                    Stage::Source,
                    "legacy VTK has no original cell IDs; assigned one-based IDs",
                ));
            }
            (projection.dataset, omissions)
        }
        Format::Stl => {
            let bytes = read_limited(path, options.max_bytes)?;
            let projection = stl::read_projection(&bytes)?;
            generated_point_ids = true;
            let mut omissions = vec![Omission::new(
                Stage::Source,
                "STL has no node or element IDs; assigned one-based facet-local IDs without welding",
            )];
            if projection.normals > 0 {
                omissions.push(Omission::new(
                    Stage::Source,
                    format!(
                        "{} STL facet normal(s) not retained as numeric fields",
                        projection.normals
                    ),
                ));
            }
            if projection.attributed_facets > 0 {
                omissions.push(Omission::new(
                    Stage::Source,
                    format!(
                        "{} STL facet(s) have nonstandard attribute bytes that were omitted",
                        projection.attributed_facets
                    ),
                ));
            }
            (projection.dataset, omissions)
        }
        Format::Su2 => {
            let text = read_text(
                path,
                options.max_bytes,
                "E_SU2",
                "SU2 mesh must be UTF-8 ASCII",
            )?;
            let projection = su2::read_projection(&text)?;
            generated_point_ids = true;
            let omissions = vec![Omission::new(
                Stage::Source,
                "SU2 uses positional connectivity; assigned one-based node and element IDs",
            )];
            (projection.dataset, omissions)
        }
        Format::Unv => {
            let text = read_text(path, options.max_bytes, "E_UNV", "UNV must be UTF-8 ASCII")?;
            let projection = unv::read_projection(&text)?;
            let mut omissions = projection
                .omitted_datasets
                .into_iter()
                .map(|number| Omission::new(Stage::Source, format!("UNV dataset {number} omitted")))
                .collect::<Vec<_>>();
            if projection.tagged_elements > 0 {
                omissions.push(Omission::new(
                    Stage::Source,
                    format!("{} UNV element header(s) have entity, physical, or color tags without a mapping", projection.tagged_elements),
                ));
            }
            (projection.dataset, omissions)
        }
        Format::Exodus => {
            let bytes = read_limited(path, options.max_bytes)?;
            let projection = exodus::read_projection(&bytes, options.step)?;
            generated_point_ids = projection.generated_point_ids;
            let omissions = projection
                .omissions
                .into_iter()
                .map(|detail| Omission::new(Stage::Source, detail))
                .collect();
            (projection.dataset, omissions)
        }
        Format::Msh => {
            // The MSH reader handles geometry and data; other sections are
            // recorded as source omissions for the caller to review.
            let text = read_text(path, options.max_bytes, "E_MSH", "MSH must be UTF-8 ASCII")?;
            let projection = msh::read_projection(&text)?;
            let mut omissions = text
                .lines()
                .filter_map(|line| line.trim().strip_prefix('$'))
                .filter(|name| !name.starts_with("End"))
                .filter(|section| {
                    !matches!(
                        *section,
                        "MeshFormat" | "Nodes" | "Elements" | "NodeData" | "ElementData" | "PhysicalNames"
                    )
                })
                .map(|section| {
                    Omission::new(
                        Stage::Source,
                        if section == "Entities" {
                            "MSH $Entities bounds and CAD topology are not retained; physical group membership is projected".to_owned()
                        } else {
                            format!("MSH ${section} section is not represented in the projection")
                        },
                    )
                })
                .collect::<Vec<_>>();
            if projection.tagged_elements > 0 {
                omissions.push(Omission::new(
                    Stage::Source,
                    format!(
                        "{} MSH 2.2 element tag list(s) have geometrical or other metadata without a mapping",
                        projection.tagged_elements
                    ),
                ));
            }
            if projection.generated_group_names > 0 {
                omissions.push(Omission::new(
                    Stage::Source,
                    format!("{} MSH physical group(s) have no source name; assigned physical_<dimension>_<tag> names", projection.generated_group_names),
                ));
            }
            (projection.dataset, omissions)
        }
        Format::Inp => {
            let text = read_text(path, options.max_bytes, "E_INP", "INP must be UTF-8 text")?;
            let parsed = inp::read(&text)?;
            let omissions = parsed
                .omitted_keywords
                .into_iter()
                .map(|keyword| {
                    Omission::new(
                        Stage::Source,
                        format!("INP keyword *{keyword} is absent from mesh projection"),
                    )
                })
                .collect();
            (
                Dataset {
                    mesh: parsed.mesh,
                    fields: Vec::new(),
                },
                omissions,
            )
        }
        Format::Frd => {
            // Step selection filters complete fields after parsing the file.
            let bytes = read_limited(path, options.max_bytes)?;
            let mut dataset = frd::read(&bytes)?;
            if let Some(step) = options.step {
                let step = i64::try_from(step)
                    .map_err(|_| Error::new("E_FRD", "selected step exceeds Int64"))?;
                dataset.fields.retain(|field| field.step == Some(step));
                if dataset.fields.is_empty() {
                    return Err(Error::new("E_FRD", "selected step has no fields"));
                }
            }
            let mut omissions = Vec::new();
            if bytes.windows(2).any(|pair| pair == b"1U" || pair == b"1P") {
                omissions.push(Omission::new(
                    Stage::Source,
                    "FRD user/model parameter metadata is not represented in the projection",
                ));
            }
            (dataset, omissions)
        }
        Format::Op2 => {
            // OP2 result tables carry no complete matching mesh on their own.
            let mesh_path = options
                .mesh
                .as_deref()
                .ok_or_else(|| Error::new("E_USAGE", "OP2 requires --mesh matching mesh file"))?;
            let (mesh, mut omissions) = read_result_mesh(mesh_path, options, Format::Op2)?;
            let bytes = read_limited(path, options.max_bytes)?;
            let (dataset, assumed_zero) =
                op2::read_displacements(&bytes, &mesh, options.subcase, options.step)?;

            // Preserve both explicit synthetic provenance and the results
            // omitted by the selected displacement-table projection.
            if assumed_zero {
                omissions.push(Omission::assumed(
                    AssumptionKind::SyntheticZero,
                    "OP2 title marks this as a synthetic all-zero displacement table, not solver results",
                ));
            }
            omissions.push(Omission::new(
                Stage::Source,
                "OP2 result tables other than the selected real displacement table are not exported",
            ));
            (dataset, omissions)
        }
        Format::Pch => {
            let mesh_path = options
                .mesh
                .as_deref()
                .ok_or_else(|| Error::new("E_USAGE", "PCH requires --mesh matching mesh file"))?;
            let (mesh, mut omissions) = read_result_mesh(mesh_path, options, Format::Pch)?;
            let source = read_text(path, options.max_bytes, "E_PCH", "PCH must be ASCII text")?;
            let projection = pch::read(&source, &mesh, options.subcase, options.step)?;
            if projection.skipped_blocks > 0 {
                omissions.push(Omission::new(
                    Stage::Source,
                    format!(
                        "{} other PCH result block header(s) were not projected",
                        projection.skipped_blocks
                    ),
                ));
            }
            if source.contains("$TITLE")
                || source.contains("$SUBTITLE")
                || source.contains("$LABEL")
            {
                omissions.push(Omission::new(
                    Stage::Source,
                    "PCH title, subtitle, and label metadata are not represented",
                ));
            }
            (projection.dataset, omissions)
        }
    };
    Ok(ReadResult {
        format,
        dataset,
        voxel_grid,
        omissions,
        generated_point_ids,
    })
}