runmat-runtime 0.5.4

Core runtime for RunMat with builtins, BLAS/LAPACK integration, and execution APIs
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
use runmat_analysis_core::AnalysisStepKind;
use runmat_analysis_fea::{
    ComputeBackend, FEA_FIELD_STRUCTURAL_DISPLACEMENT, FEA_FIELD_STRUCTURAL_VON_MISES,
};
use runmat_runtime::analysis::{
    analysis_create_model_op, analysis_run_nonlinear_with_options_op,
    AnalysisCreateModelIntentSpec, AnalysisCreateModelProfile, AnalysisNonlinearRunOptions,
};
use runmat_runtime::geometry::{
    geometry_load_op, geometry_prep_for_analysis_op, GeometryPrepForAnalysisSpec,
};
use runmat_runtime::operations::OperationContext;

const TRIANGLE_STL: &str = "solid tri\n  facet normal 0 0 1\n    outer loop\n      vertex 0 0 0\n      vertex 1 0 0\n      vertex 0 1 0\n    endloop\n  endfacet\nendsolid tri\n";

#[test]
fn prep_artifact_reference_changes_nonlinear_solve_profile_with_bounded_quality() {
    let geometry = geometry_load_op(
        "/fixtures/prep_solve_tri.stl",
        TRIANGLE_STL.as_bytes(),
        OperationContext::new(Some("trace-prep-solve-load".to_string()), None),
    )
    .expect("geometry load should succeed");
    let prep = geometry_prep_for_analysis_op(
        &geometry.data,
        GeometryPrepForAnalysisSpec::default(),
        OperationContext::new(Some("trace-prep-solve-prep".to_string()), None),
    )
    .expect("geometry prep should succeed");

    let created = analysis_create_model_op(
        &geometry.data,
        AnalysisCreateModelIntentSpec {
            model_id: "prep_solve_model".to_string(),
            profile: AnalysisCreateModelProfile::NonlinearStructural,
            prep_context: None,
        },
        OperationContext::new(Some("trace-prep-solve-create".to_string()), None),
    )
    .expect("create model should succeed");
    let mut model = created.data;
    model.steps[0].kind = AnalysisStepKind::Nonlinear;

    let baseline = analysis_run_nonlinear_with_options_op(
        &model,
        ComputeBackend::Cpu,
        AnalysisNonlinearRunOptions::production_recommended(),
        OperationContext::new(Some("trace-prep-solve-base".to_string()), None),
    )
    .expect("baseline nonlinear run should succeed");

    let prep_enhanced = analysis_run_nonlinear_with_options_op(
        &model,
        ComputeBackend::Cpu,
        AnalysisNonlinearRunOptions {
            prep_artifact_id: Some(prep.data.prep_artifact_id.clone()),
            ..AnalysisNonlinearRunOptions::production_recommended()
        },
        OperationContext::new(Some("trace-prep-solve-enhanced".to_string()), None),
    )
    .expect("prep-enhanced nonlinear run should succeed");
    let prep_enhanced_replay = analysis_run_nonlinear_with_options_op(
        &model,
        ComputeBackend::Cpu,
        AnalysisNonlinearRunOptions {
            prep_artifact_id: Some(prep.data.prep_artifact_id.clone()),
            ..AnalysisNonlinearRunOptions::production_recommended()
        },
        OperationContext::new(Some("trace-prep-solve-enhanced-replay".to_string()), None),
    )
    .expect("prep-enhanced replay nonlinear run should succeed");

    assert!(baseline
        .data
        .run
        .diagnostics
        .iter()
        .all(|diag| diag.code != "FEA_PREP_TOPOLOGY"));
    assert!(baseline
        .data
        .run
        .diagnostics
        .iter()
        .all(|diag| diag.code != "FEA_PREP_OPERATOR_TOPOLOGY"));
    assert!(baseline
        .data
        .run
        .diagnostics
        .iter()
        .all(|diag| diag.code != "FEA_PREP_REGION_TOPOLOGY"));
    assert!(baseline
        .data
        .run
        .diagnostics
        .iter()
        .all(|diag| diag.code != "FEA_PREP_ELEMENT_ASSEMBLY"));
    assert!(baseline
        .data
        .run
        .diagnostics
        .iter()
        .all(|diag| diag.code != "FEA_PREP_ELEMENT_CONNECTIVITY"));
    assert!(baseline
        .data
        .run
        .diagnostics
        .iter()
        .all(|diag| diag.code != "FEA_PREP_GRAPH_ASSEMBLY"));
    assert!(baseline
        .data
        .run
        .diagnostics
        .iter()
        .all(|diag| diag.code != "FEA_PREP_GRAPH_SOLVER"));
    assert!(baseline
        .data
        .run
        .diagnostics
        .iter()
        .all(|diag| diag.code != "FEA_PREP_CALIBRATION"));
    assert!(baseline
        .data
        .run
        .diagnostics
        .iter()
        .all(|diag| diag.code != "FEA_PREP_ACCEPTANCE"));

    assert!(prep_enhanced
        .data
        .run
        .diagnostics
        .iter()
        .any(|diag| diag.code == "FEA_PREP_CONTEXT"
            && diag.message.contains("element_geometry_node_count=")
            && diag.message.contains("element_geometry_edge_count=")
            && diag.message.contains("mean_element_edge_length_m=")
            && diag.message.contains("mean_element_area_m2=")
            && diag.message.contains("element_geometry_coverage_ratio=")));
    assert!(prep_enhanced
        .data
        .run
        .diagnostics
        .iter()
        .any(|diag| diag.code == "FEA_PREP_ASSEMBLY"));
    assert!(prep_enhanced
        .data
        .run
        .diagnostics
        .iter()
        .any(|diag| diag.code == "FEA_PREP_TOPOLOGY"));
    assert!(prep_enhanced
        .data
        .run
        .diagnostics
        .iter()
        .any(|diag| diag.code == "FEA_PREP_OPERATOR_TOPOLOGY"));
    assert!(prep_enhanced
        .data
        .run
        .diagnostics
        .iter()
        .any(|diag| diag.code == "FEA_PREP_REGION_TOPOLOGY"));
    assert!(prep_enhanced
        .data
        .run
        .diagnostics
        .iter()
        .any(|diag| diag.code == "FEA_PREP_ELEMENT_ASSEMBLY"));
    assert!(prep_enhanced
        .data
        .run
        .diagnostics
        .iter()
        .any(|diag| diag.code == "FEA_PREP_ELEMENT_CONNECTIVITY"));
    assert!(prep_enhanced
        .data
        .run
        .diagnostics
        .iter()
        .any(|diag| diag.code == "FEA_PREP_GRAPH_ASSEMBLY"));
    assert!(prep_enhanced
        .data
        .run
        .diagnostics
        .iter()
        .any(|diag| diag.code == "FEA_PREP_GRAPH_SOLVER"));
    assert!(prep_enhanced
        .data
        .run
        .diagnostics
        .iter()
        .any(|diag| diag.code == "FEA_PREP_CALIBRATION"));
    assert!(prep_enhanced
        .data
        .run
        .diagnostics
        .iter()
        .any(|diag| diag.code == "FEA_PREP_ACCEPTANCE"));

    let base_nonlinear = baseline
        .data
        .nonlinear_results
        .as_ref()
        .expect("baseline nonlinear payload present");
    let prep_nonlinear = prep_enhanced
        .data
        .nonlinear_results
        .as_ref()
        .expect("prep nonlinear payload present");

    let base_max_iters = base_nonlinear
        .iteration_counts
        .iter()
        .copied()
        .max()
        .unwrap_or(0);
    let prep_max_iters = prep_nonlinear
        .iteration_counts
        .iter()
        .copied()
        .max()
        .unwrap_or(0);
    assert!(base_max_iters.abs_diff(prep_max_iters) <= 16);
    assert!(
        base_max_iters != prep_max_iters
            || base_nonlinear.failed_increments != prep_nonlinear.failed_increments
    );

    let prep_topology_diag = prep_enhanced
        .data
        .run
        .diagnostics
        .iter()
        .find(|diag| diag.code == "FEA_PREP_TOPOLOGY")
        .expect("prep topology diagnostic should be present");
    let replay_topology_diag = prep_enhanced_replay
        .data
        .run
        .diagnostics
        .iter()
        .find(|diag| diag.code == "FEA_PREP_TOPOLOGY")
        .expect("prep topology diagnostic should be present in replay");
    assert_eq!(prep_topology_diag.message, replay_topology_diag.message);

    let prep_operator_topology_diag = prep_enhanced
        .data
        .run
        .diagnostics
        .iter()
        .find(|diag| diag.code == "FEA_PREP_OPERATOR_TOPOLOGY")
        .expect("prep operator topology diagnostic should be present");
    let replay_operator_topology_diag = prep_enhanced_replay
        .data
        .run
        .diagnostics
        .iter()
        .find(|diag| diag.code == "FEA_PREP_OPERATOR_TOPOLOGY")
        .expect("prep operator topology diagnostic should be present in replay");
    assert_eq!(
        prep_operator_topology_diag.message,
        replay_operator_topology_diag.message
    );

    let prep_region_topology_diag = prep_enhanced
        .data
        .run
        .diagnostics
        .iter()
        .find(|diag| diag.code == "FEA_PREP_REGION_TOPOLOGY")
        .expect("prep region topology diagnostic should be present");
    let replay_region_topology_diag = prep_enhanced_replay
        .data
        .run
        .diagnostics
        .iter()
        .find(|diag| diag.code == "FEA_PREP_REGION_TOPOLOGY")
        .expect("prep region topology diagnostic should be present in replay");
    assert_eq!(
        prep_region_topology_diag.message,
        replay_region_topology_diag.message
    );

    let prep_element_assembly_diag = prep_enhanced
        .data
        .run
        .diagnostics
        .iter()
        .find(|diag| diag.code == "FEA_PREP_ELEMENT_ASSEMBLY")
        .expect("prep element assembly diagnostic should be present");
    let replay_element_assembly_diag = prep_enhanced_replay
        .data
        .run
        .diagnostics
        .iter()
        .find(|diag| diag.code == "FEA_PREP_ELEMENT_ASSEMBLY")
        .expect("prep element assembly diagnostic should be present in replay");
    assert_eq!(
        prep_element_assembly_diag.message,
        replay_element_assembly_diag.message
    );

    let prep_element_connectivity_diag = prep_enhanced
        .data
        .run
        .diagnostics
        .iter()
        .find(|diag| diag.code == "FEA_PREP_ELEMENT_CONNECTIVITY")
        .expect("prep element connectivity diagnostic should be present");
    let replay_element_connectivity_diag = prep_enhanced_replay
        .data
        .run
        .diagnostics
        .iter()
        .find(|diag| diag.code == "FEA_PREP_ELEMENT_CONNECTIVITY")
        .expect("prep element connectivity diagnostic should be present in replay");
    assert_eq!(
        prep_element_connectivity_diag.message,
        replay_element_connectivity_diag.message
    );

    let prep_graph_diag = prep_enhanced
        .data
        .run
        .diagnostics
        .iter()
        .find(|diag| diag.code == "FEA_PREP_GRAPH_ASSEMBLY")
        .expect("prep graph assembly diagnostic should be present");
    let replay_graph_diag = prep_enhanced_replay
        .data
        .run
        .diagnostics
        .iter()
        .find(|diag| diag.code == "FEA_PREP_GRAPH_ASSEMBLY")
        .expect("prep graph assembly diagnostic should be present in replay");
    assert_eq!(prep_graph_diag.message, replay_graph_diag.message);

    let edge_count = metric_usize(&prep_graph_diag.message, "edge_count");
    let node_count = metric_usize(&prep_graph_diag.message, "node_count");
    assert!(edge_count > 0);
    assert!(edge_count <= node_count.saturating_mul(node_count.saturating_sub(1)) / 2);

    let prep_graph_solver_diag = prep_enhanced
        .data
        .run
        .diagnostics
        .iter()
        .find(|diag| diag.code == "FEA_PREP_GRAPH_SOLVER")
        .expect("prep graph solver diagnostic should be present");
    let replay_graph_solver_diag = prep_enhanced_replay
        .data
        .run
        .diagnostics
        .iter()
        .find(|diag| diag.code == "FEA_PREP_GRAPH_SOLVER")
        .expect("prep graph solver diagnostic should be present in replay");
    assert_eq!(
        prep_graph_solver_diag.message,
        replay_graph_solver_diag.message
    );

    let prep_calibration_diag = prep_enhanced
        .data
        .run
        .diagnostics
        .iter()
        .find(|diag| diag.code == "FEA_PREP_CALIBRATION")
        .expect("prep calibration diagnostic should be present");
    let replay_calibration_diag = prep_enhanced_replay
        .data
        .run
        .diagnostics
        .iter()
        .find(|diag| diag.code == "FEA_PREP_CALIBRATION")
        .expect("prep calibration diagnostic should be present in replay");
    assert_eq!(
        prep_calibration_diag.message,
        replay_calibration_diag.message
    );

    let prep_acceptance_diag = prep_enhanced
        .data
        .run
        .diagnostics
        .iter()
        .find(|diag| diag.code == "FEA_PREP_ACCEPTANCE")
        .expect("prep acceptance diagnostic should be present");
    let replay_acceptance_diag = prep_enhanced_replay
        .data
        .run
        .diagnostics
        .iter()
        .find(|diag| diag.code == "FEA_PREP_ACCEPTANCE")
        .expect("prep acceptance diagnostic should be present in replay");
    assert_eq!(prep_acceptance_diag.message, replay_acceptance_diag.message);
    let acceptance_score = metric_f64(&prep_acceptance_diag.message, "acceptance_score");
    assert!(acceptance_score > 0.0);

    let base_peak_displacement = baseline
        .data
        .run
        .field(FEA_FIELD_STRUCTURAL_DISPLACEMENT)
        .expect("baseline displacement field should be present")
        .as_host_f64()
        .expect("baseline displacement should be host-backed")
        .iter()
        .map(|value| value.abs())
        .fold(0.0_f64, f64::max);
    let prep_peak_displacement = prep_enhanced
        .data
        .run
        .field(FEA_FIELD_STRUCTURAL_DISPLACEMENT)
        .expect("prep displacement field should be present")
        .as_host_f64()
        .expect("prep displacement should be host-backed")
        .iter()
        .map(|value| value.abs())
        .fold(0.0_f64, f64::max);
    let displacement_delta_ratio = (prep_peak_displacement - base_peak_displacement).abs()
        / base_peak_displacement
            .max(prep_peak_displacement)
            .max(1.0e-9);
    assert!(displacement_delta_ratio > 1.0e-4);
    assert!(displacement_delta_ratio < 0.95);

    let base_peak_stress = baseline
        .data
        .run
        .field(FEA_FIELD_STRUCTURAL_VON_MISES)
        .expect("baseline von Mises field should be present")
        .as_host_f64()
        .expect("baseline stress should be host-backed")
        .iter()
        .map(|value| value.abs())
        .fold(0.0_f64, f64::max);
    let prep_peak_stress = prep_enhanced
        .data
        .run
        .field(FEA_FIELD_STRUCTURAL_VON_MISES)
        .expect("prep von Mises field should be present")
        .as_host_f64()
        .expect("prep stress should be host-backed")
        .iter()
        .map(|value| value.abs())
        .fold(0.0_f64, f64::max);
    let stress_delta_ratio = (prep_peak_stress - base_peak_stress).abs()
        / base_peak_stress.max(prep_peak_stress).max(1.0e-9);
    assert!(stress_delta_ratio > 1.0e-4);
    assert!(stress_delta_ratio < 0.95);
}

fn metric_usize(message: &str, key: &str) -> usize {
    message
        .split_whitespace()
        .find_map(|token| token.strip_prefix(&format!("{key}=")))
        .and_then(|value| value.parse::<usize>().ok())
        .unwrap_or(0)
}

fn metric_f64(message: &str, key: &str) -> f64 {
    message
        .split_whitespace()
        .find_map(|token| token.strip_prefix(&format!("{key}=")))
        .and_then(|value| value.parse::<f64>().ok())
        .unwrap_or(0.0)
}