Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic Event Scheduler Model #37

Open
wants to merge 7 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from SR2ML.src import MaintenanceModel
from SR2ML.src import MCSSolver
from SR2ML.src import MarginModel
from SR2ML.src import basicEventScheduler
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend to use upper case for each leading character when define a class. That is: change basicEventScheduler to BasicEventScheduler

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed

from SR2ML.src.PostProcessors import DataLabeling
from SR2ML.src.PostProcessors import ETImporter
from SR2ML.src.PostProcessors import FTImporter
Expand Down
41 changes: 41 additions & 0 deletions doc/user_manual/include/basicEventScheduler.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
\section{Basic Event Scheduler Model}
\label{sec:basicEventScheduler}

This model is designed to read the initial and final time under which a set of basic events
are set to True, and then it generates an HistorySet with the full temporal profile of all
basic events.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend to add another paragraph to explain the structure of generated history set.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added



All the specifications of the Basic Event Scheduler model are given in the \xmlNode{ExternalModel} block.
Inside the \xmlNode{ExternalModel} block, the XML
nodes that belong to this models are:
\begin{itemize}
\item \xmlNode{variables}, \xmlDesc{string, required parameter}, a list containing the names of both the input and output variables of the model
\item \xmlNode{BE}, \xmlDesc{string, required parameter}, the name ID of all basic events
\begin{itemize}
\item \xmlAttr{tin}, \xmlDesc{required string attribute}, name ID of the variable describing the initial time of the BE
\item \xmlAttr{tin}, \xmlDesc{required string attribute}, name ID of the variable describing the final time of the BE
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change 'tin' to 'tfin'

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, good catch

\end{itemize}
\end{itemize}

The example of \textbf{basicEventScheduler} model is provided below:
\begin{lstlisting}[style=XML]
<Models>
<ExternalModel name="basicEventScheduler" subType="SR2ML.basicEventScheduler">
<variables>BE1_tin, BE2_tin, BE3_tin,
BE1_tfin,BE2_tfin,BE3_tfin,
BE1,BE2,BE3,time</variables>
<BE tin='BE1_tin' tfin='BE1_tfin'>BE1</BE>
<BE tin='BE2_tin' tfin='BE2_tfin'>BE2</BE>
<BE tin='BE3_tin' tfin='BE3_tfin'>BE3</BE>
<timeID>time</timeID>
</ExternalModel>
</Models>
\end{lstlisting}

\subsection{basicEventScheduler Reference Tests}
\begin{itemize}
\item SR2ML/tests/test\_basicEventScheduler.xml
\end{itemize}


1 change: 1 addition & 0 deletions doc/user_manual/user_manual.tex
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
\input{include/MCSImporter.tex}
\input{include/DiscreteRiskMeasures.tex}
\input{include/MarginModels.tex}
\input{include/basicEventScheduler.tex}

\section*{Document Version Information}
This document has been compiled using the following version of the plug-in git repository:
Expand Down
108 changes: 108 additions & 0 deletions src/basicEventScheduler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Copyright 2017 Battelle Energy Alliance, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please update the copyright info, (The above info is used by raven). Something like:

# Copyright 2020, Battelle Energy Alliance, LLC
# ALL RIGHTS RESERVED

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

"""
Created on June 24, 2020

@author: mandd
"""

#External Modules---------------------------------------------------------------
import numpy as np
import xarray as xr
import pandas as pd
#External Modules End-----------------------------------------------------------

#Internal Modules---------------------------------------------------------------
from PluginBaseClasses.ExternalModelPluginBase import ExternalModelPluginBase
#Internal Modules End-----------------------------------------------------------

class basicEventScheduler(ExternalModelPluginBase):
"""
This class is designed to create a Maintenance Scheduler model
"""
def __init__(self):
"""
Constructor
@ In, None
@ Out, None
"""
ExternalModelPluginBase.__init__(self)

def initialize(self, container, runInfoDict, inputFiles):
"""
Method to initialize the Basic Event Scheduler model
@ In, container, object, self-like object where all the variables can be stored
@ In, runInfoDict, dict, dictionary containing all the RunInfo parameters (XML node <RunInfo>)
@ In, inputFiles, list, list of input files (if any)
@ Out, None
"""

def _readMoreXML(self, container, xmlNode):
"""
Method to read the portion of the XML that belongs to the Basic Event Scheduler model
@ In, container, object, self-like object where all the variables can be stored
@ In, xmlNode, xml.etree.ElementTree.Element, XML node that needs to be read
@ Out, None
"""
container.basicEvents = {}

for child in xmlNode:
if child.tag == 'BE':
container.basicEvents[child.text.strip()] = [child.get('tin'),child.get('tfin')]
elif child.tag == 'timeID':
container.timeID = child.text.strip()
elif child.tag == 'variables':
variables = [str(var.strip()) for var in child.text.split(",")]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variables is not directly used in this class, you probably can remove these lines.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lines are needed as part of the external model class which require the "variables" node

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variables are processed in ExternalModel class, but you are not using it in your class.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed lines 65 and 66 but it errors out of line 68

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. We are looping over all nodes, even if they have already processed by other class. I'm ok to keep it here.

else:
raise IOError("basicEventScheduler: xml node " + str(child.tag) + " is not allowed")

def run(self, container, inputs):
"""
This method generate an historySet from the a pointSet which contains initial and final time of the
basic events
@ In, inputDataset, dict, dictionary of inputs from RAVEN
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change inputDataset to inputs

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@ In, container, object, self-like object where all the variables can be stored
@ Out, basicEventHistorySet, Dataset, xarray dataset which contains time series for each basic event
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no return for the run method. I think you can move the above line to the method docstring explanation part.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

"""
dataDict = {}
for key in container.basicEvents.keys():
dataDict[key] = []
dataDict[key].append(inputs[container.basicEvents[key][0]][0])
dataDict[key].append(inputs[container.basicEvents[key][1]][0])

inputDataset = pd.DataFrame.from_dict(dataDict, orient='index',columns=['tin', 'tfin'])

timeArray = np.concatenate([inputDataset['tin'],inputDataset['tfin']])
timeArraySorted = np.sort(timeArray,axis=0)
timeArrayCleaned = np.unique(timeArraySorted)

keys = list(container.basicEvents.keys())
dataVars={}
for key in keys:
dataVars[key]=(['RAVEN_sample_ID',container.timeID],np.zeros((1,timeArrayCleaned.shape[0])))

basicEventHistorySet = xr.Dataset(data_vars = dataVars,
coords = dict(time=timeArrayCleaned,
RAVEN_sample_ID=np.zeros(1)))

for key in container.basicEvents.keys():
tin = inputs[container.basicEvents[key][0]][0]
tend = inputs[container.basicEvents[key][1]][0]
indexes = np.where(np.logical_and(timeArrayCleaned>tin,timeArrayCleaned<=tend))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why 'timeArrayCleaned>tin', but not 'timeArrayCleaned>=tin'?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is due to to the conversion from interval-based data to instant-based data (i.e., the history set). The convention here is that a logical value set to 1 at a specific time instant implies that the basic event was actually True from the previous time instant

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point, it is better to clarify it in the user manual.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, added text.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the definition, 'tin' is the initial time for the basic event. Based on the calculation np.where(np.logical_and(timeArrayCleaned>tin,timeArrayCleaned<=tend)), when the time instant is tin, the value are set to 0. Does this mean the event are still the same as before at tin, and does not change state at tin? Only the time large than tin, the basic event are actually changing state. Do I state correctly? @mandd

basicEventHistorySet[key][0][indexes] = 1.0

container.__dict__[key] = basicEventHistorySet[key].values[0]

container.__dict__[container.timeID] = timeArrayCleaned

119 changes: 119 additions & 0 deletions tests/test_basicEventScheduler.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<Simulation verbosity="debug">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no gold files for this test

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's weird, i wonder why the regression test was green

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see issue #38

<TestInfo>
<name>SR2ML/basicEventScheduler</name>
<author>mandd</author>
<created>2021-07-14</created>
<classesTested>SR2ML/basicEventScheduler</classesTested>
<description>
This model provided the schedule of a set of basic events create an history set containing
basic event temporal profiles. Each basic event is represented by the initial and final time
where basic event is set to True (i.e., with probability=1.0)
</description>
</TestInfo>

<RunInfo>
<WorkingDir>basicEventScheduler</WorkingDir>
<Sequence>simRun</Sequence>
<batchSize>1</batchSize>
</RunInfo>

<Models>
<ExternalModel name="basicEventScheduler" subType="SR2ML.basicEventScheduler">
<variables>BE1_tin, BE2_tin, BE3_tin,
BE1_tfin,BE2_tfin,BE3_tfin,
BE1,BE2,BE3,time</variables>
<BE tin='BE1_tin' tfin='BE1_tfin'>BE1</BE>
<BE tin='BE2_tin' tfin='BE2_tfin'>BE2</BE>
<BE tin='BE3_tin' tfin='BE3_tfin'>BE3</BE>
<timeID>time</timeID>
</ExternalModel>
</Models>

<Distributions>
<Uniform name='BE1_tin_dist'>
<lowerBound>0.</lowerBound>
<upperBound>1.</upperBound>
</Uniform>
<Uniform name='BE2_tin_dist'>
<lowerBound>2.</lowerBound>
<upperBound>3.</upperBound>
</Uniform>
<Uniform name='BE3_tin_dist'>
<lowerBound>1.</lowerBound>
<upperBound>3.</upperBound>
</Uniform>
<Uniform name='BE1_tfin_dist'>
<lowerBound>2.</lowerBound>
<upperBound>3.</upperBound>
</Uniform>
<Uniform name='BE2_tfin_dist'>
<lowerBound>4.</lowerBound>
<upperBound>5.</upperBound>
</Uniform>
<Uniform name='BE3_tfin_dist'>
<lowerBound>3.</lowerBound>
<upperBound>5.</upperBound>
</Uniform>
</Distributions>

<Samplers>
<MonteCarlo name="MC_external">
<samplerInit>
<limit>1</limit>
</samplerInit>
<variable name="BE1_tin">
<distribution>BE1_tin_dist</distribution>
</variable>
<variable name="BE2_tin">
<distribution>BE2_tin_dist</distribution>
</variable>
<variable name="BE3_tin">
<distribution>BE3_tin_dist</distribution>
</variable>
<variable name="BE1_tfin">
<distribution>BE1_tfin_dist</distribution>
</variable>
<variable name="BE2_tfin">
<distribution>BE2_tfin_dist</distribution>
</variable>
<variable name="BE3_tfin">
<distribution>BE3_tfin_dist</distribution>
</variable>
</MonteCarlo>
</Samplers>

<Steps>
<MultiRun name="simRun">
<Input class="DataObjects" type="PointSet" >inputPlaceHolder</Input>
<Model class="Models" type="ExternalModel" >basicEventScheduler</Model>
<Sampler class="Samplers" type="MonteCarlo" >MC_external</Sampler>
<Output class="DataObjects" type="HistorySet" >sim_HS</Output>
<Output class="OutStreams" type="Print" >Print_sim_HS</Output>
</MultiRun>
</Steps>

<OutStreams>
<Print name="Print_sim_HS">
<type>csv</type>
<source>sim_HS</source>
<what>input,output</what>
</Print>
</OutStreams>

<DataObjects>
<PointSet name="inputPlaceHolder">
<Input>BE1_tin, BE2_tin, BE3_tin,
BE1_tfin,BE2_tfin,BE3_tfin</Input>
<Output>OutputPlaceHolder</Output>
</PointSet>
<HistorySet name="sim_HS">
<Input>BE1_tin, BE2_tin, BE3_tin,
BE1_tfin,BE2_tfin,BE3_tfin</Input>
<Output>BE1,BE2,BE3</Output>
<options>
<pivotParameter>time</pivotParameter>
</options>
</HistorySet>
</DataObjects>

</Simulation>
6 changes: 6 additions & 0 deletions tests/tests
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,10 @@
input = 'test_MCSSolver_margin.xml'
UnorderedCsv = 'MCSSolverMargin/Print_sim_PS.csv'
[../]

[./TestBasicEventScheduler]
type = 'RavenFramework'
input = 'test_basicEventScheduler.xml'
OrderedCsv = 'basicEventScheduler/Print_sim_HS_0.csv'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the csv file is missed in the gold folder

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change OrderedCsv to csv, otherwise, the test will be be tested.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

[../]
[]