2.3 KiB
2.3 KiB
结构体层面
use std::io::{BufWriter, Write};
use std::sync::{Arc, Mutex};
use std::fs::File;
struct sometype{
pub writer: Arc<Mutex<BufWriter<File>>>, // 文件句柄包装
}
impl sometype{
pub fn new() -> Self {
let file = File::create("MultibodyOutput.txt").expect("Unable to create file");
let writer = Arc::new(Mutex::new(BufWriter::new(file)));
Self {
writer,
}
}
}
pub fn write_header(&self) {
// 按照这样的方式写头文件Time (s), Wind speed (m/s), Power (W), Blade root Mx (N*m),
// Blade root My (N*m), Blade root Mz (N*m), Blade root Fx (N), Blade root Fy (N), Blade root Fz (N),
// let header = "Time (s), Blade 1 flapwise tip deflection (m), Blade 1 edgewise tip deflection (m), Blade 1 flapwise tip acceleration (m/s^2), Blade 1 edgewise tip acceleration (m/s^2)";
// "time", "TipDxc1", "TipDyc1", "TipDzc1", "TipDxb1", "TipDyb1", "TipALxb1", "TipALyb1", "TipALzb1", "TwrTpTDxi", "TwrTpTDyi", "TwrTpTDzi", "YawBrTVxp", "YawBrTVyp", "YawBrTVzp", "YawBrTAxp", "YawBrTAyp", "YawBrTAzp"写文件头
let header_vec = vec![
"time", "TipDxc1", "TipDyc1", "TipDzc1", "TipDxb1", "TipDyb1",
"TipALxb1", "TipALyb1", "TipALzb1", "TwrTpTDxi", "TwrTpTDyi",
"TwrTpTDzi", "YawBrTVxp", "YawBrTVyp", "YawBrTVzp", "YawBrTAxp",
"YawBrTAyp", "YawBrTAzp"
];
// 将字符串数组连接成一个单独的字符串,每个元素之间用逗号分隔
let header = header_vec.join(", ");
let mut writer = self.writer.lock().expect("Failed to acquire lock");
writeln!(writer, "{}", header).expect("Unable to write header");
}
/// 写入 Vec<f64> 数据,每个值之间用空格分隔
pub fn write_data(&self, data: &Vec<f64>) {
let mut writer = self.writer.lock().expect("Failed to acquire lock");
// 将 Vec<f64> 数据转换为以空格分隔的字符串
let data_str = data.iter()
.map(|v| v.to_string())
.collect::<Vec<String>>()
.join(" ");
// 写入数据行
writeln!(writer, "{}", data_str).expect("Unable to write data");
}
调用层面
m = sometype.new()
somevec = Vec<f64>
m.write_data(&somevec);