Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion simulator/src/integrated/send_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
EventList, Trace,
run_messages::{
SendAlarm, SendRunLogData, SendRunStart, SendRunStop, SendSampleEnvLog,
SendSampleEnvLogValues,
},
utils::JsonValueError,
},
Expand Down Expand Up @@ -226,10 +227,17 @@ pub(crate) fn send_se_log_command(
})
.map(|timestamps| fbb.create_vector(&timestamps));

let values = match &sample_env.values {
SendSampleEnvLogValues::Literal(items) => items.clone(),
SendSampleEnvLogValues::FromNoise { length, noise } => {
sample_environment::generate_value(*length, noise)?
}
};

let values = Some(sample_environment::make_value(
&mut fbb,
values_type,
&sample_env.values,
&values,
));

let se_log_args = se00_SampleEnvironmentDataArgs {
Expand Down
13 changes: 13 additions & 0 deletions simulator/src/integrated/simulation_elements/noise.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::VecDeque;

use crate::integrated::simulation_elements::FloatRandomDistribution;

use super::{Interval, NumExpression, utils::JsonValueError};
use chrono::Utc;
use digital_muon_common::Time;
Expand All @@ -24,6 +26,13 @@ impl NoiseSource {
pub(crate) fn sample(&self, time: Time, frame_index: usize) -> Result<f64, JsonValueError> {
if self.bounds.is_in(time, frame_index)? {
match &self.attributes {
NoiseAttributes::Bernoulli { probability, value } => {
if rand::random_bool(probability.value(frame_index)?) {
value.sample(frame_index)
} else {
Ok(0.0)
}
}
NoiseAttributes::Uniform(Interval { min, max }) => {
let val = (max.value(frame_index)? - min.value(frame_index)?)
* rand::random::<f64>()
Expand All @@ -47,6 +56,10 @@ impl NoiseSource {
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "kebab-case", tag = "noise-type")]
pub(crate) enum NoiseAttributes {
Bernoulli {
probability: NumExpression<f64>,
value: FloatRandomDistribution<f64>,
},
Uniform(Interval<NumExpression<f64>>),
Gaussian {
mean: NumExpression<f64>,
Expand Down
16 changes: 13 additions & 3 deletions simulator/src/integrated/simulation_elements/run_messages.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
integrated::simulation_elements::utils::TextConstant,
integrated::simulation_elements::{noise::NoiseSource, utils::TextConstant},
runs::{
alarm::SeverityLevel,
runlog::ValueType,
Expand Down Expand Up @@ -32,16 +32,26 @@ pub(crate) struct SendRunLogData {
}

#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "kebab-case", tag = "run-command")]
#[serde(rename_all = "kebab-case")]
pub(crate) struct SendSampleEnvLog {
pub(crate) name: TextConstant,
pub(crate) channel: Option<i32>,
pub(crate) time_delta: Option<f64>,
pub(crate) values_type: ValuesType,
pub(crate) message_counter: Option<i64>,
pub(crate) location: LocationType,
pub(crate) values: Vec<String>,
pub(crate) timestamps: Option<Vec<DateTime<Utc>>>,
pub(crate) values: SendSampleEnvLogValues,
}

#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum SendSampleEnvLogValues {
Literal(Vec<String>),
FromNoise {
length: usize,
noise: Vec<NoiseSource>,
},
}

#[derive(Clone, Debug, Deserialize)]
Expand Down
12 changes: 12 additions & 0 deletions simulator/src/integrated/simulation_engine/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ pub(crate) enum Action {
//
FrameLoop(Loop<FrameAction>),
//
LogLoop(Loop<LogAction>),
//
SetTimestamp(Timestamp),
SetVetoFlags(u16),
SetPeriod(u64),
Expand Down Expand Up @@ -137,3 +139,13 @@ pub(crate) enum DigitiserAction {
GenerateTrace(GenerateTrace),
GenerateEventList(GenerateEventList),
}

#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum LogAction {
Comment(#[allow(unused)] String),
SendRunLogData(SendRunLogData),
SendSampleEnvLog(SendSampleEnvLog),
SendAlarm(SendAlarm),
SetTimestamp(Timestamp),
}
47 changes: 45 additions & 2 deletions simulator/src/integrated/simulation_engine/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::integrated::{
utils::JsonValueError,
},
simulation_engine::actions::{
Action, DigitiserAction, FrameAction, GenerateEventList, GenerateTrace, Timestamp,
TracingEvent, TracingLevel,
Action, DigitiserAction, FrameAction, GenerateEventList, GenerateTrace, LogAction,
Timestamp, TracingEvent, TracingLevel,
},
};
use chrono::{DateTime, TimeDelta, Utc};
Expand Down Expand Up @@ -255,6 +255,12 @@ pub(crate) fn run_schedule(engine: &mut SimulationEngine) -> Result<(), Simulati
run_frame(engine, frame_loop.schedule.as_slice())?;
}
}
Action::LogLoop(log_loop) => {
for index in log_loop.start.value()?..=log_loop.end.value()? {
engine.state.metadata.frame_number = index as FrameNumber;
run_logloop_schedule(engine, log_loop.schedule.as_slice())?;
}
}
Action::Comment(_) => (),
}
}
Expand Down Expand Up @@ -391,3 +397,40 @@ pub(crate) fn run_digitiser(
}
Ok(())
}

#[tracing::instrument(skip_all, level = "debug"
fields(
index = engine.state.metadata.frame_number,
num_actions = log_actions.len()
)
err(level = "error")
)]
pub(crate) fn run_logloop_schedule(
engine: &mut SimulationEngine,
log_actions: &[LogAction],
) -> Result<(), SimulationEngineError> {
for action in log_actions {
match action {
LogAction::SendRunLogData(run_log_data) => send_run_log_command(
&mut engine.externals,
&engine.state.metadata.timestamp,
run_log_data,
)?,
LogAction::SendSampleEnvLog(sample_env_log) => send_se_log_command(
&mut engine.externals,
&engine.state.metadata.timestamp,
sample_env_log,
)?,
LogAction::SendAlarm(alarm) => {
send_alarm_command(
&mut engine.externals,
&engine.state.metadata.timestamp,
alarm,
)?;
}
LogAction::SetTimestamp(timestamp) => set_timestamp(engine, timestamp)?,
LogAction::Comment(_) => (),
}
}
Ok(())
}
18 changes: 18 additions & 0 deletions simulator/src/runs/sample_environment.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::ValueEnum;
use digital_muon_common::Time;
use digital_muon_streaming_types::{
ecs_se00_data_generated::{
DoubleArray, DoubleArrayArgs, FloatArray, FloatArrayArgs, Int8Array, Int8ArrayArgs,
Expand All @@ -11,6 +12,8 @@ use digital_muon_streaming_types::{
use serde::Deserialize;
use std::str::FromStr;

use crate::integrated::simulation_elements::{noise::NoiseSource, utils::JsonValueError};

#[derive(Clone, Debug, Deserialize, ValueEnum)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum ValuesType {
Expand Down Expand Up @@ -84,6 +87,21 @@ where
)
}

pub(crate) fn generate_value(
length: usize,
noise_sources: &[NoiseSource],
) -> Result<Vec<String>, JsonValueError> {
(0..length)
.map(|time| {
noise_sources
.iter()
.map(|ns| ns.sample(time as Time, 0))
.sum::<Result<f64, _>>()
})
.map(|val| val.map(|val| val.to_string()))
.collect::<Result<Vec<_>, _>>()
}

pub(crate) fn make_value(
fbb: &mut FlatBufferBuilder,
value_type: ValueUnion,
Expand Down