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
use std::collections::BTreeSet;
use std::io::Write;
use crate::core::{Error, FieldLocation, Mesh, Result};
use crate::formats::{bdf, exodus, frd, inp, msh, op2, stl, su2, unv, vtk, vtu};
use super::{AssumptionKind, ConversionReport, Format, 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()
}
/// 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> {
// Count fields before destination-specific filtering; the report describes
// the source dataset as well as any losses during output.
let source_fields = source.dataset.fields.len();
let assumed_zero = source
.omissions
.iter()
.any(|item| item.assumption == Some(AssumptionKind::SyntheticZero));
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::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,
mesh_output: None,
})
}