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
//! Adapter tests for vtu.

use super::*;
use crate::core::{Cell, CellKind, Dataset, Field, FieldLocation, Mesh, Point};
use std::io::Write;

/// Build a small mesh with stable original IDs for VTU assertions.
fn triangle() -> Mesh {
    Mesh {
        points: vec![
            Point {
                id: 10,
                position: [0., 0., 0.],
            },
            Point {
                id: 20,
                position: [1., 0., 0.],
            },
            Point {
                id: 30,
                position: [0., 1., 0.],
            },
        ],
        cells: vec![Cell {
            id: 50,
            kind: CellKind::Triangle3,
            connectivity: vec![0, 1, 2],
            property_id: Some(7),
        }],
        ..Mesh::default()
    }
}

/// Write VTU arrays that retain mesh identity, connectivity, and cell kind.
#[test]
fn writes_ids_connectivity_and_type() {
    let mut output = Vec::new();
    write(&triangle(), &mut output).unwrap();
    let text = String::from_utf8(output).unwrap();
    assert!(text.contains("NumberOfPoints=\"3\" NumberOfCells=\"1\""));
    assert!(text.contains("nastran_node_id"));
    assert!(text.contains("0 1 2 "));
    assert!(text.contains("format=\"ascii\">\n5\n"));
}

/// Validate the mesh before emitting partial VTU output.
#[test]
fn invalid_mesh_writes_nothing() {
    let mut mesh = triangle();
    mesh.cells[0].connectivity[0] = 100;
    let mut output = Vec::new();
    assert!(write(&mesh, &mut output).is_err());
    assert_eq!(output, Vec::<u8>::new());
}

/// Map every supported linear cell to its VTK type code.
#[test]
fn all_linear_cell_numbers() {
    assert_eq!(
        [
            CellKind::Line2,
            CellKind::Triangle3,
            CellKind::Quad4,
            CellKind::Tet4,
            CellKind::Hex8,
            CellKind::Wedge6,
            CellKind::Pyramid5
        ]
        .map(write::vtk_type),
        [3, 5, 9, 10, 12, 13, 14]
    );
}

/// Preserve all seven linear topology families through VTU data arrays.
#[test]
fn all_linear_topologies_roundtrip() {
    let mesh = crate::formats::bdf::mesh::read(include_bytes!(
        "../../../../tests/fixtures/mixed-linear.bdf"
    ))
    .unwrap()
    .mesh;
    let mut bytes = Vec::new();
    write(&mesh, &mut bytes).unwrap();
    let decoded = read(std::str::from_utf8(&bytes).unwrap()).unwrap();
    assert_eq!(decoded.mesh.points, mesh.points);
    assert_eq!(decoded.mesh.cells, mesh.cells);
}

/// Keep large original IDs in integer arrays without floating-point rounding.
#[test]
fn preserves_large_ids_as_integers() {
    let mut mesh = triangle();
    mesh.points[0].id = 9_007_199_254_740_993;
    let mut output = Vec::new();
    write(&mesh, &mut output).unwrap();
    assert!(
        String::from_utf8(output)
            .unwrap()
            .contains("9007199254740993")
    );
}

/// Surface caller-owned stream failures from the VTU writer.
#[test]
fn propagates_writer_failure() {
    /// Output stream that always fails for writer error propagation tests.
    struct Broken;
    impl Write for Broken {
        fn write(&mut self, _: &[u8]) -> std::io::Result<usize> {
            Err(std::io::Error::other("test failure"))
        }
        fn flush(&mut self) -> std::io::Result<()> {
            Ok(())
        }
    }
    assert_eq!(write(&triangle(), Broken).unwrap_err().code, "E_IO");
}

/// Separate numeric fields from identity arrays at both entity locations.
#[test]
fn reads_numeric_point_and_cell_fields_without_mixing_ids() {
    let dataset = Dataset {
        mesh: triangle(),
        fields: vec![
            Field {
                name: "Temperature".into(),
                location: FieldLocation::Point,
                components: vec!["T".into()],
                values: vec![10., 20., 30.],
                step: Some(2),
                time: Some(0.5),
            },
            Field {
                name: "Energy".into(),
                location: FieldLocation::Cell,
                components: vec!["E".into()],
                values: vec![7.],
                step: Some(2),
                time: Some(0.5),
            },
        ],
    };
    let mut bytes = Vec::new();
    write_data(&dataset, &mut bytes).unwrap();
    let parsed = read(std::str::from_utf8(&bytes).unwrap()).unwrap();
    assert_eq!(parsed, dataset);
}

/// Decode escaped field labels with the current XML reader API.
#[test]
fn escaped_field_attributes_roundtrip() {
    let dataset = Dataset {
        mesh: triangle(),
        fields: vec![Field {
            name: "A&B<\"C\"".into(),
            location: FieldLocation::Point,
            components: vec!["T&1".into()],
            values: vec![1., 2., 3.],
            step: None,
            time: None,
        }],
    };
    let mut bytes = Vec::new();
    write_data(&dataset, &mut bytes).unwrap();
    assert_eq!(read(std::str::from_utf8(&bytes).unwrap()).unwrap(), dataset);
}

/// Ignore commented XML tags when selecting mesh coordinates.
#[test]
fn xml_comments_cannot_spoof_geometry() {
    let mut bytes = Vec::new();
    write(&triangle(), &mut bytes).unwrap();
    let source = String::from_utf8(bytes).unwrap();
    let spoof = "<!-- <Points><DataArray format=\"ascii\" NumberOfComponents=\"3\">100 0 0 200 0 0 300 0 0</DataArray></Points> -->\n";
    let source = source.replace("<Points>", &format!("{spoof}<Points>"));
    let dataset = read(&source).unwrap();
    assert_eq!(dataset.mesh.points, triangle().points);
}

/// Require exact XML attribute names rather than suffix matches.
#[test]
fn xml_attribute_names_match_exactly() {
    let mut bytes = Vec::new();
    write(&triangle(), &mut bytes).unwrap();
    let source = String::from_utf8(bytes)
        .unwrap()
        .replace("Name=\"connectivity\"", "XName=\"connectivity\"");
    assert_eq!(read(&source).unwrap_err().code, "E_VTU");
}

/// Bound declared VTU array sizes before allocation or multiplication.
#[test]
fn declared_counts_and_components_cannot_overflow() {
    let mut bytes = Vec::new();
    write(&triangle(), &mut bytes).unwrap();
    let source = String::from_utf8(bytes).unwrap();
    for malformed in [
        source.replace(
            "NumberOfPoints=\"3\"",
            "NumberOfPoints=\"18446744073709551615\"",
        ),
        source.replace(
            "NumberOfCells=\"1\"",
            "NumberOfCells=\"18446744073709551615\"",
        ),
    ] {
        assert_eq!(read(&malformed).unwrap_err().code, "E_VTU");
    }
    let source = source.replace(
        "</PointData>",
        "<DataArray type=\"Float64\" Name=\"x\" NumberOfComponents=\"18446744073709551615\" format=\"ascii\">1</DataArray></PointData>",
    );
    assert_eq!(read(&source).unwrap_err().code, "E_VTU");
}

/// Reject field names that cannot be represented in XML.
#[test]
fn invalid_xml_names_fail_before_writing() {
    for invalid_name in ["bad\0name", "bad\u{1}name", "bad\nname"] {
        let mut dataset = Dataset {
            mesh: triangle(),
            fields: vec![Field {
                name: invalid_name.into(),
                location: FieldLocation::Point,
                components: vec!["C1".into()],
                values: vec![1., 2., 3.],
                step: None,
                time: None,
            }],
        };
        let mut output = Vec::new();
        assert_eq!(write_data(&dataset, &mut output).unwrap_err().code, "E_VTU");
        assert_eq!(output, Vec::<u8>::new());
        dataset.fields[0].name = "valid".into();
        dataset.fields[0].components[0] = invalid_name.into();
        assert_eq!(write_data(&dataset, &mut output).unwrap_err().code, "E_VTU");
        assert_eq!(output, Vec::<u8>::new());
    }
}

/// User fields cannot shadow arrays that preserve original mesh identity.
#[test]
fn reserved_identity_array_names_are_rejected_before_output() {
    for reserved in [
        "nastran_node_id",
        "nastran_element_id",
        "nastran_property_id",
    ] {
        let dataset = Dataset {
            mesh: triangle(),
            fields: vec![Field {
                name: reserved.into(),
                location: FieldLocation::Point,
                components: vec!["C1".into()],
                values: vec![1.0, 2.0, 3.0],
                step: None,
                time: None,
            }],
        };
        let mut output = Vec::new();
        assert!(
            write_data(&dataset, &mut output)
                .unwrap_err()
                .message
                .contains("reserved ID array")
        );
        assert_eq!(output, []);
    }
}

/// Reject malformed XML structure before accepting changed geometry.
#[test]
fn structural_xml_mutations_fail_before_geometry_can_change() {
    let mut bytes = Vec::new();
    write(&triangle(), &mut bytes).unwrap();
    let source = String::from_utf8(bytes).unwrap();
    for (old, new) in [
        ("type=\"UnstructuredGrid\"", "type=\"PolyData\""),
        ("version=\"0.1\"", "compressor=\"vtkZLibDataCompressor\""),
        ("NumberOfPoints=\"3\"", "NumberOfPoints=\"4\""),
        ("NumberOfCells=\"1\"", "NumberOfCells=\"2\""),
        (
            "type=\"UInt64\" Name=\"nastran_node_id\"",
            "type=\"Int64\" Name=\"nastran_node_id\"",
        ),
        ("Name=\"connectivity\"", "Name=\"wrong_connectivity\""),
        ("Name=\"offsets\"", "Name=\"wrong_offsets\""),
        ("format=\"ascii\">\n0 0 0", "format=\"binary\">\n0 0 0"),
        ("NumberOfComponents=\"3\"", "NumberOfComponents=\"2\""),
        ("<Points>", "<Points><Unsupported/>"),
        ("</Piece>", "<Unsupported/></Piece>"),
        ("</UnstructuredGrid>", "<Piece/></UnstructuredGrid>"),
        ("</VTKFile>", "<![CDATA[untrusted]]></VTKFile>"),
    ] {
        assert!(source.contains(old), "missing fixture marker {old}");
        let changed = source.replacen(old, new, 1);
        assert_eq!(read(&changed).unwrap_err().code, "E_VTU", "{new}");
    }
}

/// Reject ambiguous XML trees and arrays even when the remaining mesh is valid.
#[test]
fn duplicate_and_nested_xml_records_cannot_shadow_mesh_data() {
    let mut bytes = Vec::new();
    write(&triangle(), &mut bytes).unwrap();
    let source = String::from_utf8(bytes).unwrap();
    for malformed in [
        source.replace("<Points>", "<Points></Points><Points>"),
        source.replace("<PointData>", "<PointData></PointData><PointData>"),
        source.replace("0 0 0", "<Nested>0 0 0</Nested>"),
        source.replace("0 0 0", "<Nested/>0 0 0"),
        source.replace("<Points>", "<Points>unexpected text"),
        source.replace("</VTKFile>", ""),
        format!("{source}<Extra/>"),
        format!("{source}<Extra></Extra>"),
        source.replace(
            "</Cells>",
            "<DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\">3</DataArray></Cells>",
        ),
        source.replace("<Points>", "<Points>\0"),
    ] {
        assert_eq!(read(&malformed).unwrap_err().code, "E_VTU");
    }
}

/// Detect truncated identity arrays, unused cells, and duplicate numeric fields.
#[test]
fn malformed_identity_and_field_arrays_are_not_projected() {
    let dataset = Dataset {
        mesh: triangle(),
        fields: vec![Field {
            name: "TEMP".into(),
            location: FieldLocation::Point,
            components: vec!["T".into()],
            values: vec![1.0, 2.0, 3.0],
            step: None,
            time: None,
        }],
    };
    let mut bytes = Vec::new();
    write_data(&dataset, &mut bytes).unwrap();
    let source = String::from_utf8(bytes).unwrap();
    for (old, new) in [
        (
            "Name=\"nastran_node_id\" format=\"ascii\">\n10\n20\n30\n",
            "Name=\"nastran_node_id\" format=\"ascii\">\n10\n20\n",
        ),
        (
            "type=\"UInt64\" Name=\"nastran_element_id\"",
            "type=\"Int64\" Name=\"nastran_element_id\"",
        ),
        (
            "type=\"UInt64\" Name=\"nastran_property_id\"",
            "type=\"Int64\" Name=\"nastran_property_id\"",
        ),
        ("0 1 2 \n</DataArray>", "0 1 2 0 \n</DataArray>"),
        (
            "Name=\"TEMP\" NumberOfComponents=\"1\" format=\"ascii\" ComponentName0=\"T\">\n1 \n2 \n3 \n",
            "Name=\"TEMP\" NumberOfComponents=\"1\" format=\"ascii\" ComponentName0=\"T\">\n1 \n2 \n",
        ),
        (
            "</PointData>",
            "<DataArray type=\"Float64\" Name=\"TEMP\" format=\"ascii\">1 2 3</DataArray></PointData>",
        ),
    ] {
        assert!(source.contains(old), "missing fixture marker {old}");
        let malformed = source.replacen(old, new, 1);
        assert_eq!(read(&malformed).unwrap_err().code, "E_VTU", "{new}");
    }
}

/// Reject truncated identity and field arrays instead of accepting partial data.
#[test]
fn numeric_vtu_mutations_reject_incomplete_identity_and_fields() {
    let dataset = Dataset {
        mesh: triangle(),
        fields: vec![Field {
            name: "TEMP".into(),
            location: FieldLocation::Point,
            components: vec!["T".into()],
            values: vec![1., 2., 3.],
            step: Some(2),
            time: Some(0.5),
        }],
    };
    let mut bytes = Vec::new();
    write_data(&dataset, &mut bytes).unwrap();
    let source = String::from_utf8(bytes).unwrap();
    for (old, new) in [
        (
            "Name=\"offsets\" format=\"ascii\">\n3",
            "Name=\"offsets\" format=\"ascii\">\n4",
        ),
        (
            "Name=\"types\" format=\"ascii\">\n5",
            "Name=\"types\" format=\"ascii\">\n22",
        ),
        (
            "Name=\"nastran_element_id\" format=\"ascii\">\n50",
            "Name=\"nastran_element_id\" format=\"ascii\">\n50 51",
        ),
        (
            "Name=\"nastran_property_id\" format=\"ascii\">\n7",
            "Name=\"nastran_property_id\" format=\"ascii\">\n7 8",
        ),
        (
            "Name=\"TEMP\" NumberOfComponents=\"1\" format=\"ascii\"",
            "Name=\"TEMP\" NumberOfComponents=\"1\" format=\"binary\"",
        ),
        (
            "Name=\"TEMP\" NumberOfComponents=\"1\"",
            "Name=\"TEMP\" NumberOfComponents=\"0\"",
        ),
        (
            "Name=\"TEMP\" NumberOfComponents=\"1\"",
            "Name=\"TEMP\" NumberOfComponents=\"x\"",
        ),
        (
            "type=\"Float64\" Name=\"TEMP\"",
            "type=\"Int64\" Name=\"TEMP\"",
        ),
    ] {
        assert!(source.contains(old), "missing fixture marker {old}");
        let changed = source.replacen(old, new, 1);
        assert_eq!(read(&changed).unwrap_err().code, "E_VTU", "{new}");
    }
}