Commit 7c6ee86d by dongshufeng

prepare to form nlp 3phase power flow model

parent 37486f9d
......@@ -6,9 +6,10 @@ use arrow_schema::{DataType, Field, Schema};
use ndarray::Array2;
use ds_common::dyn_topo::{read_dev_topo, read_dyn_topo};
use ds_common::tn_input::read_tn_input;
use mems::model::{get_wasm_result, PluginInput, PluginOutput};
use crate::read::{read_dev_ohm, read_tn_input};
use crate::read::read_dev_ohm;
mod read;
mod nlp;
......
use std::collections::HashMap;
use ndarray::{Array, Array2, Ix2};
use num_complex::Complex64;
use eig_domain::DataUnit;
use mems::model::dev::MeasPhase;
......@@ -11,36 +13,34 @@ pub fn get_pf_nlp_constraints(
dev_topo: Vec<Vec<u64>>,
dev_matrix: HashMap<u64, Vec<Array2<f64>>>,
input_tns: Vec<u64>,
input_phases: Vec<Vec<MeasPhase>>,
input_types: Vec<Vec<DataUnit>>,
input_values: Vec<Vec<f64>>) -> Option<Vec<String>> {
input_phases: Vec<MeasPhase>,
input_types: Vec<DataUnit>,
input_values: Vec<f64>) -> Option<Vec<String>> {
let mut constraint = Vec::with_capacity(dyn_topo.len());
for i in 0..input_tns.len() {
for j in 0..input_types[i].len() {
match input_types[i][j] {
DataUnit::kW => {
let mut active_exp = String::new();
constraint.push(active_exp);
match input_phases[i][j] {
MeasPhase::Total => {}
MeasPhase::A => {}
MeasPhase::B => {}
MeasPhase::C => {}
MeasPhase::A0 => {}
MeasPhase::B0 => {}
MeasPhase::C0 => {}
MeasPhase::CT => {}
MeasPhase::PT => {}
MeasPhase::Unknown => return None,
}
match input_types[i] {
DataUnit::kW => {
let mut active_exp = String::new();
constraint.push(active_exp);
match input_phases[i] {
MeasPhase::Total => {}
MeasPhase::A => {}
MeasPhase::B => {}
MeasPhase::C => {}
MeasPhase::A0 => {}
MeasPhase::B0 => {}
MeasPhase::C0 => {}
MeasPhase::CT => {}
MeasPhase::PT => {}
MeasPhase::Unknown => return None,
}
DataUnit::kVar => {
let mut reactive_exp = String::new();
constraint.push(reactive_exp);
}
DataUnit::kV => {}
_ => {}
}
DataUnit::kVar => {
let mut reactive_exp = String::new();
constraint.push(reactive_exp);
}
DataUnit::kV => {}
_ => {}
}
}
Some(constraint)
......
......@@ -4,9 +4,8 @@ use csv::StringRecordsIter;
use ndarray::Array2;
use eig_domain::DataUnit;
use mems::model::dev::MeasPhase;
const MAT_SIZE: usize = 2 * 54;
const MAT_SIZE: usize = 18;
pub(crate) fn read_dev_ohm(records: &mut StringRecordsIter<&[u8]>)
-> Result<HashMap<u64, Vec<Array2<f64>>>, String> {
......@@ -66,65 +65,6 @@ pub(crate) fn read_dev_ohm(records: &mut StringRecordsIter<&[u8]>)
Ok(map)
}
pub(crate) fn read_tn_input(records: &mut StringRecordsIter<&[u8]>)
-> Result<(Vec<u64>, Vec<Vec<MeasPhase>>, Vec<Vec<DataUnit>>, Vec<Vec<f64>>), String> {
let mut tn = Vec::new();
let mut input_type = Vec::new();
let mut input_phase = Vec::new();
let mut value = Vec::new();
// 按行读取csv
let mut row = 0;
loop {
match records.next() {
Some(Ok(record)) => {
let mut col = 0;
for str in record.iter() {
if col == 0 {
if let Ok(v) = str.parse() {
tn.push(v);
} else {
return Err(format!("Wrong bus input, row {row} col {col}"));
}
} else if col == 1 {
if let Ok(v) = serde_json::from_str(str) {
input_phase.push(v);
} else {
return Err(format!("Wrong bus input, row {row} col {col}"));
}
} else if col == 2 {
if let Ok(v) = serde_json::from_str(str) {
input_type.push(v);
} else {
return Err(format!("Wrong bus input, row {row} col {col}"));
}
} else if col == 3 {
if let Ok(v) = serde_json::from_str(str) {
value.push(v);
} else {
return Err(format!("Wrong bus input, row {row} col {col}"));
}
}
col += 1;
if col == 4 {
break;
}
}
if col != 4 {
return Err(format!("Wrong bus input, expected col 4, actual {col}"));
}
}
Some(Err(e)) => {
return Err(format!("Wrong bus input, err: {:?}", e));
}
None => {
break;
}
}
row += 1;
}
Ok((tn, input_phase, input_type, value))
}
fn create_rx(matrix: &[f64]) -> Vec<Array2<f64>> {
let r = Array2::from_shape_vec((3, 3), matrix[0..9].to_vec()).unwrap();
......
......@@ -7,4 +7,5 @@ edition = "2021"
log = "0.4"
serde_json = "1.0"
csv = "1.3.0"
eig-domain = { path = "../../../eig-domain" }
mems = { path = "../../../mems" }
\ No newline at end of file
use std::collections::HashMap;
use csv::StringRecordsIter;
use eig_domain::DataUnit;
use mems::model::dev::MeasPhase;
pub fn read_shunt_measures(records: &mut StringRecordsIter<&[u8]>)
......@@ -50,4 +50,63 @@ pub fn read_shunt_measures(records: &mut StringRecordsIter<&[u8]>)
row += 1;
}
Ok(meas)
}
pub fn read_tn_input(records: &mut StringRecordsIter<&[u8]>)
-> Result<(Vec<u64>, Vec<MeasPhase>, Vec<DataUnit>, Vec<f64>), String> {
let mut tn = Vec::new();
let mut input_type = Vec::new();
let mut input_phase = Vec::new();
let mut value = Vec::new();
// 按行读取csv
let mut row = 0;
loop {
match records.next() {
Some(Ok(record)) => {
let mut col = 0;
for str in record.iter() {
if col == 0 {
if let Ok(v) = str.parse() {
tn.push(v);
} else {
return Err(format!("Wrong bus input, row {row} col {col}"));
}
} else if col == 1 {
if let Ok(v) = serde_json::from_str(str) {
input_phase.push(v);
} else {
return Err(format!("Wrong bus input, row {row} col {col}"));
}
} else if col == 2 {
if let Ok(v) = serde_json::from_str(str) {
input_type.push(v);
} else {
return Err(format!("Wrong bus input, row {row} col {col}"));
}
} else if col == 3 {
if let Ok(v) = serde_json::from_str(str) {
value.push(v);
} else {
return Err(format!("Wrong bus input, row {row} col {col}"));
}
}
col += 1;
if col == 4 {
break;
}
}
if col != 4 {
return Err(format!("Wrong bus input, expected col 4, actual {col}"));
}
}
Some(Err(e)) => {
return Err(format!("Wrong bus input, err: {:?}", e));
}
None => {
break;
}
}
row += 1;
}
Ok((tn, input_phase, input_type, value))
}
\ No newline at end of file
......@@ -212,16 +212,16 @@ pub unsafe fn run(ptr: i32, len: u32) -> u64 {
}
}
}
let mut csv_str = String::from("tn,unit,phase,value\n");
let mut csv_str = String::from("tn,phase,unit,value\n");
for (tn, measure) in tn_measure {
for (unit, phase, f) in measure {
csv_str.push_str(&format!("{tn},{unit},{phase},{f}\n"))
csv_str.push_str(&format!("{tn},{phase},{unit},{f}\n"))
}
}
let schema = Schema::new(vec![
Field::new("tn", DataType::UInt64, false),
Field::new("unit", DataType::Utf8, false),
Field::new("phase", DataType::Utf8, false),
Field::new("unit", DataType::Utf8, false),
Field::new("value", DataType::Float64, false),
]);
let csv_bytes = vec![(TN_INPUT_DF_NAME.to_string(), csv_str.into_bytes())];
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论