Skip to content
Snippets Groups Projects
Commit 7c8a291c authored by Udo Eisenbarth's avatar Udo Eisenbarth :speech_balloon:
Browse files

Add scenery.save_to_file fn.

This cleans up the example code a little bit.
Fixed detector.export_data.
Generate diagram before performing analysis. If analysis fails,
a diagram is generated anyway.
Update various examples to use the aboe save function.
parent 180d450a
No related branches found
No related tags found
No related merge requests found
use std::io::Write;
use std::{fs::File, path::Path};
use std::path::Path;
use opossum::{
analyzer::AnalyzerEnergy,
error::OpossumError,
lightdata::{DataEnergy, LightData},
nodes::{BeamSplitter, Detector, FilterType, IdealFilter, Source},
......@@ -35,19 +33,7 @@ fn main() -> Result<(), OpossumError> {
scenery.connect_nodes(i_s2, "out1", i_bs, "input2")?;
scenery.connect_nodes(i_bs, "out1_trans1_refl2", i_f, "front")?;
scenery.connect_nodes(i_f, "rear", i_d1, "in1")?;
let path = "beam_combiner.dot";
let mut output = File::create(path).unwrap();
write!(output, "{}", scenery.to_dot("")?).unwrap();
scenery.report(Path::new(""));
println!("");
let mut analyzer = AnalyzerEnergy::new(&scenery);
print!("Analyze...");
analyzer.analyze()?;
println!("Sucessful");
println!("");
scenery.report(Path::new(""));
scenery.save_to_file(Path::new("beam_combiner_test.opm"))?;
Ok(())
}
use std::io::Write;
use std::{fs::File, path::Path};
use std::path::Path;
use opossum::{
analyzer::AnalyzerEnergy,
error::OpossumError,
lightdata::{DataEnergy, LightData},
nodes::{BeamSplitter, EnergyMeter, FilterType, IdealFilter, Source, Spectrometer},
......@@ -39,19 +37,6 @@ fn main() -> Result<(), OpossumError> {
scenery.connect_nodes(i_f, "rear", i_d2, "in1")?;
scenery.connect_nodes(i_d2, "out1", i_d3, "in1")?;
let serialized = serde_json::to_string_pretty(&scenery).unwrap();
let path = "filter_test.opm";
let mut output = File::create(path).unwrap();
write!(output, "{}", serialized).unwrap();
scenery.report(Path::new("./"));
println!("");
let mut analyzer = AnalyzerEnergy::new(&scenery);
print!("Analyze...");
analyzer.analyze()?;
println!("Sucessful");
println!("");
scenery.report(Path::new("./"));
scenery.save_to_file(Path::new("filter_test.opm"))?;
Ok(())
}
......@@ -2,9 +2,7 @@ use opossum::error::OpossumError;
use opossum::nodes::{BeamSplitter, Dummy};
use opossum::OpticScenery;
use std::fs::File;
use std::io::Write;
use std::path::Path;
fn main() -> Result<(), OpossumError> {
let mut scenery = OpticScenery::new();
......@@ -26,9 +24,6 @@ fn main() -> Result<(), OpossumError> {
scenery.connect_nodes(bs2, "out1_trans1_refl2", out1, "front")?;
scenery.connect_nodes(bs2, "out2_trans2_refl1", out1, "front")?;
let path = "graph_w_ports.dot";
let mut output = File::create(path).unwrap();
write!(output, "{}", scenery.to_dot("")?).unwrap();
scenery.save_to_file(Path::new("graph_w_ports.opm"))?;
Ok(())
}
use opossum::{
error::OpossumError,
lightdata::{DataEnergy, LightData},
nodes::{BeamSplitter, Detector, Dummy, NodeReference, Source},
spectrum::create_he_ne_spectrum,
OpticScenery,
};
use std::fs::File;
use std::io::Write;
use std::path::Path;
fn main() -> Result<(), OpossumError> {
let mut scenery = OpticScenery::new();
scenery.set_description("Michaelson interferomater");
let src = scenery.add_node(Source::default());
let src = scenery.add_node(Source::new(
"Source",
LightData::Energy(DataEnergy {
spectrum: create_he_ne_spectrum(1.0),
}),
));
let bs = scenery.add_node(BeamSplitter::default());
let sample = scenery.add_node(Dummy::new("Sample"));
let rf = NodeReference::from_node(scenery.node(sample)?);
......@@ -29,8 +35,6 @@ fn main() -> Result<(), OpossumError> {
scenery.connect_nodes(m2, "rear", r_bs, "input2")?;
scenery.connect_nodes(r_bs, "out1_trans1_refl2", det, "in1")?;
let path = "michaelson.dot";
let mut output = File::create(path).unwrap();
write!(output, "{}", scenery.to_dot("")?).unwrap();
scenery.save_to_file(Path::new("michaelson.opm"))?;
Ok(())
}
use opossum::error::OpossumError;
use opossum::nodes::Dummy;
use opossum::OpticScenery;
use std::fs::File;
use std::io::Write;
use std::path::Path;
fn main() -> Result<(), OpossumError> {
println!("opticscenery example");
let mut scenery = OpticScenery::new();
scenery.set_description("OpticScenery demo");
let node1 = scenery.add_node(Dummy::new("dummy1"));
let node2 = scenery.add_node(Dummy::new("dummy2"));
scenery.connect_nodes(node1, "rear", node2, "front")?;
let serialized = serde_yaml::to_string(&scenery).unwrap();
println!("{}", serialized);
let path = "opticscenery.opm";
let mut output = File::create(path).unwrap();
write!(output, "{}", serialized).unwrap();
let restored = serde_yaml::from_str::<OpticScenery>(&serialized);
println!("{:?}", restored);
let path = "graph.dot";
let mut output = File::create(path).unwrap();
write!(output, "{}", scenery.to_dot("")?).unwrap();
scenery.save_to_file(Path::new("opticscenery.opm"))?;
Ok(())
}
......@@ -12,14 +12,12 @@ use opossum::{
type Result<T> = std::result::Result<T, OpossumError>;
fn main() -> Result<()> {
fn main() {
//not necessary, just for fun
show_intro();
if let Err(e) = do_it() {
println!("Error: {}", e);
Err(e)
} else {
Ok(())
std::process::exit(1);
}
}
......@@ -38,6 +36,13 @@ fn do_it() -> Result<()> {
OpossumError::OpticScenery(format!("error while parsing model file: {}", e))
})?;
println!("Success");
let mut dot_path = opossum_args.report_directory.clone();
dot_path.push(opossum_args.file_path.file_stem().unwrap());
dot_path.set_extension("dot");
print!("Write diagram to {}...", dot_path.display());
let mut output = File::create(dot_path).unwrap();
write!(output, "{}", scenery.to_dot("")?).unwrap();
println!("Success");
print!("\nAnalyzing...");
scenery.analyze(&opossum_args.analyzer)?;
println!("Success\n");
......@@ -52,12 +57,5 @@ fn do_it() -> Result<()> {
)
.unwrap();
println!("Success");
let mut dot_path = opossum_args.report_directory.clone();
dot_path.push(opossum_args.file_path.file_stem().unwrap());
dot_path.set_extension("dot");
print!("Write diagram to {}...", dot_path.display());
let mut output = File::create(dot_path).unwrap();
write!(output, "{}", scenery.to_dot("")?).unwrap();
println!("Success");
Ok(())
}
......@@ -9,7 +9,7 @@ use crate::{
};
use std::collections::HashMap;
use std::fmt::Debug;
use std::path::Path;
use std::path::{Path, PathBuf};
type Result<T> = std::result::Result<T, OpossumError>;
......@@ -69,9 +69,11 @@ impl Optical for Detector {
Ok(HashMap::from([("out2".into(), None)]))
}
}
fn export_data(&self, file_path: &Path) {
fn export_data(&self, report_dir: &Path) {
if let Some(data) = &self.light_data {
data.export(file_path)
let mut file_path = PathBuf::from(report_dir);
file_path.push(format!("spectrum_{}.svg", self.name()));
data.export(&file_path)
}
}
fn is_detector(&self) -> bool {
......
use std::cell::RefCell;
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::rc::Rc;
......@@ -14,7 +16,6 @@ use petgraph::algo::*;
use petgraph::prelude::NodeIndex;
use petgraph::visit::EdgeRef;
use serde_derive::{Deserialize, Serialize};
use serde_json::json;
type Result<T> = std::result::Result<T, OpossumError>;
......@@ -317,16 +318,36 @@ impl OpticScenery {
.0
.node_weights()
.filter(|node| node.0.borrow().is_detector());
report.insert("Detectors".into(), json!("Detectors"));
//report.insert("detectors".into(), json!("Detectors"));
let mut detectors: Vec<serde_json::Value> = Vec::new();
for node in detector_nodes {
detectors.push(node.0.borrow().report());
node.0.borrow().export_data(report_dir);
}
let detector_json = serde_json::Value::Array(detectors);
report.insert("Detectors".into(), detector_json);
report.insert("detectors".into(), detector_json);
serde_json::Value::Object(report)
}
pub fn save_to_file(&self, path: &Path) -> Result<()> {
let serialized = serde_json::to_string_pretty(&self).map_err(|e| {
OpossumError::OpticScenery(format!("deserialization of OpticScenery failed: {}", e))
})?;
let mut output = File::create(path).map_err(|e| {
OpossumError::OpticScenery(format!(
"could not create file path: {}: {}",
path.display(),
e
))
})?;
write!(output, "{}", serialized).map_err(|e| {
OpossumError::OpticScenery(format!(
"writing to file path {} failed: {}",
path.display(),
e
))
})?;
Ok(())
}
}
#[cfg(test)]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment