149 lines
3.2 KiB
Markdown
149 lines
3.2 KiB
Markdown
|
||
```rust
|
||
#![allow(unused)] // 忽略未使用的警告
|
||
|
||
|
||
|
||
use ndarray::*; // 导入 ndarray crate,提供高效的 n 维数组功能
|
||
|
||
|
||
|
||
// 定义结构体: MyStructType1,用于存储不同维度的矩阵
|
||
|
||
|
||
|
||
#[derive(Default)] // 使用 Default trait 自动实现默认构造函数
|
||
|
||
pub struct MyStructType1 {
|
||
|
||
pub matrix1: Array1<f64>, // 1D 矩阵,类型为 f64
|
||
|
||
pub matrix2: Array2<f64>, // 2D 矩阵,类型为 f64
|
||
|
||
pub matrix3: Array3<f64>, // 3D 矩阵,类型为 f64
|
||
|
||
}
|
||
|
||
|
||
|
||
#[derive(Default)] // 使用 Default trait 自动实现默认构造函数
|
||
|
||
pub struct MyStructType {
|
||
|
||
pub attribute_string: String, // Supplied by Driver: full path and filename for the HydroDyn module
|
||
|
||
pub attribute_bool: bool, // Supplied by Driver: true if using an input file, false if all inputs are being passed in by the caller
|
||
|
||
pub attribute_value: f64, // i32, f32, etc ...// Wave field handling (switch)
|
||
|
||
pub attribute_vec: Vec<f64>, // 用于存储一个固定类型元素如String的可变长度集合, 注意1维数组统一用Array1D
|
||
|
||
pub attribute_array1d: Array1<f64>, // 1D 矩阵,类型为 f64
|
||
|
||
pub attribute_array2d: Array2<f64>, // 2D 矩阵,类型为 f64
|
||
|
||
pub attribute_array3d: Array3<f64>, // 3D 矩阵,类型为 f64
|
||
|
||
pub attribute_struct: MyStructType1, // StructType 结构体
|
||
|
||
|
||
|
||
// default values
|
||
|
||
pub attribute_val_with_default: f64, // i32, f32, etc ...
|
||
|
||
pub attribute_vec_with_default: Vec<f64>, // ???
|
||
|
||
pub attribute_array_with_default: Array2<f64>, // 2D 矩阵,类型为 f64
|
||
|
||
|
||
|
||
// constant values
|
||
|
||
pub attribute_constant: f64, // gravity how to define? Supplied by Driver: Gravitational acceleration (m/s^2)
|
||
|
||
}
|
||
|
||
|
||
|
||
// 为 MyStructType 提供一个新的构造函数
|
||
|
||
impl MyStructType {
|
||
|
||
// new 方法用于创建一个新的 StructType 实例,默认初始化
|
||
|
||
pub fn new() -> Self {
|
||
|
||
MyStructType::default()// 调用默认构造函数
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
impl MyStructType {
|
||
|
||
|
||
|
||
pub fn test() -> Self {
|
||
|
||
MyStructType::new() //
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
fn main() {
|
||
|
||
// 定义矩阵的维度
|
||
|
||
let nx = 10_usize; // 矩阵的行数
|
||
|
||
let ny = 10_usize; // 矩阵的列数
|
||
|
||
let nz = 10_usize; // 3D 矩阵的深度(层数)
|
||
|
||
|
||
|
||
// 创建一个 MyStructType 实例
|
||
|
||
let mut my_struct = MyStructType::new();
|
||
|
||
|
||
|
||
// 初始化 1D 矩阵 matrix1,元素数量为 nx,且所有元素值为 0.0
|
||
|
||
// my_struct.attribute_array1d = Array1::zeros(nx); // or
|
||
|
||
// define 1D array, and assign values
|
||
|
||
// my_struct.attribute_array1d = array![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
|
||
|
||
let array1d = array![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
|
||
|
||
my_struct.attribute_array1d = array1d;
|
||
|
||
|
||
|
||
// 初始化 2D 矩阵 matrix2,行数为 nx,列数为 ny,且所有元素值为 0.0
|
||
|
||
my_struct.attribute_array2d = Array2::zeros((nx, ny));
|
||
|
||
|
||
|
||
// 初始化 3D 矩阵 matrix3,维度为 (nx, ny, nz),且所有元素值为 0.0
|
||
|
||
my_struct.attribute_array3d = Array3::zeros((nx, ny, nz));
|
||
|
||
}
|
||
```
|
||
|
||
|
||
|
||
```rust
|
||
pub inp_bl: Vec<BladeInputData>, // 各个叶片的输入数据
|
||
```
|
||
default inp_bl = vec![] |