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
//! Bincode-based FFI API for numerical FEA functions.
use Deserialize;
use Serialize;
use crateBincodeBuffer;
use cratefrom_bincode_buffer;
use crateto_bincode_buffer;
use crateFfiResult;
use cratephysics_fea;
/// Computes the axial stiffness of a 1D linear finite element using bincode serialization.
///
/// The axial stiffness represents the force-displacement relationship for a bar element:
/// k = (E × A) / L, where E is Young's modulus, A is cross-sectional area, and L is length.
///
/// # Arguments
///
/// * `buffer` - A bincode-encoded buffer containing `LinearElement1DInput` with:
/// - `length`: Element length L (m)
/// - `youngs_modulus`: Young's modulus E (Pa)
/// - `area`: Cross-sectional area A (m²)
///
/// # Returns
///
/// A bincode-encoded buffer containing `FfiResult<f64, String>` with either:
/// - `ok`: The computed axial stiffness k (N/m)
/// - `err`: Error message if deserialization failed
///
/// # Safety
///
/// This function is unsafe because it receives raw pointers through FFI.
/// The caller must ensure the input buffer contains valid bincode data.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
pub unsafe extern "C"
/// Computes the von Mises equivalent stress from a 2D stress state using bincode serialization.
///
/// The von Mises stress is a scalar measure of stress intensity used in yield criteria:
/// `σ_vm` = √(`σ_x²` - `σ_xσ_y` + `σ_y²` + `3τ_xy²`)
///
/// # Arguments
///
/// * `buffer` - A bincode-encoded buffer containing `StressInput` with:
/// - `sx`: Normal stress in x-direction `σ_x` (Pa)
/// - `sy`: Normal stress in y-direction `σ_y` (Pa)
/// - `txy`: Shear stress `τ_xy` (Pa)
///
/// # Returns
///
/// A bincode-encoded buffer containing `FfiResult<f64, String>` with either:
/// - `ok`: The von Mises stress `σ_vm` (Pa)
/// - `err`: Error message if deserialization failed
///
/// # Safety
///
/// This function is unsafe because it receives raw pointers through FFI.
/// The caller must ensure the input buffer contains valid bincode data.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
pub unsafe extern "C"
/// Computes the principal stresses and orientation from a 2D stress state using bincode serialization.
///
/// Principal stresses are the eigenvalues of the stress tensor, representing maximum and minimum
/// normal stresses. The angle indicates the orientation of the principal stress axes.
///
/// # Arguments
///
/// * `buffer` - A bincode-encoded buffer containing `StressInput` with:
/// - `sx`: Normal stress in x-direction `σ_x` (Pa)
/// - `sy`: Normal stress in y-direction `σ_y` (Pa)
/// - `txy`: Shear stress `τ_xy` (Pa)
///
/// # Returns
///
/// A bincode-encoded buffer containing `FfiResult<PrincipalStressOutput, String>` with either:
/// - `ok`: Object containing:
/// - `sigma1`: Maximum principal stress σ₁ (Pa)
/// - `sigma2`: Minimum principal stress σ₂ (Pa)
/// - `angle`: Orientation angle θ (radians)
/// - `err`: Error message if deserialization failed
///
/// # Safety
///
/// This function is unsafe because it receives raw pointers through FFI.
/// The caller must ensure the input buffer contains valid bincode data.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
pub unsafe extern "C"