-
Notifications
You must be signed in to change notification settings - Fork 43
Reliability Prototype #833
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
Draft
RHammond2
wants to merge
9
commits into
NatLabRockies:develop
Choose a base branch
from
RHammond2:feature/reliability
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+175
−0
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d53fbd8
0.9 release: develop -> main (#830)
johnjasa f6b1c18
add initial prototype for weibull reliability
RHammond2 19e3e1c
simplify relibaility model non openmdao model
RHammond2 6bcd000
Merge branch 'develop' into feature/reliability
RHammond2 fccb5bd
add model retrieval and remove extrasfrom baseclasses
RHammond2 6340023
Merge branch 'feature/reliability' of https://github.com/RHammond2/H2…
RHammond2 c1cfd2f
Merge branch 'develop' of https://github.com/NREL/H2Integrate into de…
RHammond2 e02b34d
Merge branch 'develop' into feature/reliability
RHammond2 58bdff9
make downtime and reliability separate models
RHammond2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| from abc import ABC, abstractmethod | ||
| from typing import Any | ||
|
|
||
| import numpy as np | ||
| from attrs import field, define, validators | ||
|
|
||
| from h2integrate.core.utilities import BaseConfig | ||
|
|
||
|
|
||
| # generated from np.random.SeedSequence().entropy | ||
| rng = np.random.default_rng(279299947538423226929715083173412195503) | ||
|
|
||
|
|
||
| N_TIMESTEPS = 8760 | ||
|
|
||
|
|
||
| def create_reliability(config: dict): | ||
| """Retrieves and initializes a matching reliability model.""" | ||
| name = config.pop("reliability") | ||
| match name: | ||
| case "WeibullReliability": | ||
| return WeibullReliability.from_dict(config) | ||
| case _: | ||
| raise NotImplementedError(f"{name} is not a valid model name") | ||
|
|
||
|
|
||
| def generate_downtime_model(config: dict): | ||
| name = config.pop("model") | ||
| match name: | ||
| case "LogNormalDowntime": | ||
| return LogNormalDowntime.from_dict(config) | ||
| case _: | ||
| raise NotImplementedError(f"{name} is not a valid model name") | ||
|
|
||
|
|
||
| @define(kw_only=True) | ||
| class BaseDowntime(ABC, BaseConfig): | ||
| @abstractmethod | ||
| def sample_downtime(self) -> np.ndarray: ... | ||
|
|
||
|
|
||
| @define(kw_only=True) | ||
| class LogNormalDowntime(BaseDowntime): | ||
| """Basic log-normal downtime model for generating the length of downtime for a given event. | ||
|
|
||
| Args: | ||
| mean (float): Average length of downtime per event, in hours. | ||
| sigma (float): Standard deviation of the distribution(s), in hours. | ||
| n_components (int): Number of identical components to sample. Primarily for convenience. | ||
| Defaults to 1. | ||
| """ | ||
|
|
||
| mean: float = field(validator=(validators.instance_of(float), validators.ge(0))) | ||
| sigma: float = field(validator=(validators.instance_of(float), validators.ge(0))) | ||
| n_components: int = field(default=1, validator=(validators.instance_of(int), validators.ge(1))) | ||
|
|
||
| def sample_downtime(self) -> np.ndarray: | ||
| size = (self.n_components, 100) if isinstance(self.mean, int | float) else 100 | ||
| return rng.lognormal(self.mean, self.sigma, size=size) | ||
|
|
||
|
|
||
| @define(kw_only=True) | ||
| class WeibullReliability(BaseConfig): | ||
| r"""Basic reliability model for operating/not operating statuses. | ||
|
|
||
| Assumes a full operational shutdown with zero ramping of production for an hourly, 1 year | ||
| simulation. | ||
|
|
||
| Args: | ||
| scale (float): Also referred to as :math:`\lambda` or :math:`\alpha`. Determines | ||
| the scale of distribution, and is equivalent to the mean time | ||
| between failure in years (MTBF), or 1 / annual failure rate. | ||
| shape (float): Also referred to as ``k`` or :math:`\beta`. A value less than 1 | ||
| corresponds to a decreasing hazard rate over time (break-in period failures); | ||
| a value greater than 1 corresponds to an increasing hazard rate over time ( | ||
| aging/wear-out failures); and a value of 1 corresponds to a constant hazard | ||
| rate over time (exponential distribution). | ||
| downtime (float): Average amount of downtime per failure. | ||
|
|
||
| Attributes: | ||
| rng (np.random._generator.Generator): NumPy random generator object. | ||
|
|
||
| TODO: | ||
| - how to pass n_timesteps through from plant? | ||
| - stabilize random generator/determine how to manage random seeding across library | ||
| """ | ||
|
|
||
| scale: float = field(validator=validators.instance_of(float)) | ||
| shape: float = field(validator=validators.instance_of(float)) | ||
| downtime: Any = field(converter=generate_downtime_model) | ||
| downtime_per_event: np.ndarray = field(init=False, validator=validators.instance_of(np.ndarray)) | ||
| availability: np.ndarray = field( | ||
| default=np.ones(N_TIMESTEPS), init=False, validator=validators.instance_of(np.ndarray) | ||
| ) | ||
|
|
||
| def __attrs_post_init__(self): | ||
| self.create_downtime_events() | ||
| self.calculate_availability() | ||
|
|
||
| def create_downtime_events(self): | ||
| """Creates a ``time_to_failure`` and ``downtime_per_event``.""" | ||
| # NOTE: Arrays are default length 30 to ensure enough events are created for a 1-year | ||
| # simulation without burdening the memory usage. | ||
| self.time_to_failures = np.ceil( | ||
| self.rng.weibul(self.shape, size=100) * self.scale * N_TIMESTEPS | ||
| ).astype(int) | ||
| self.downtime_per_event = self.downtime.sample_downtime() | ||
|
|
||
| def calculate_availability(self): | ||
| """Determine the timing and duration of outages for a single year of simulation time.""" | ||
| accumulated = 0 | ||
| while accumulated < N_TIMESTEPS: | ||
| event, self.time_to_failures = self.time_to_failures[0], self.time_to_failures[1:] | ||
| duration, self.downtime_per_event = ( | ||
| self.downtime_per_event[0], | ||
| self.downtime_per_event[1:], | ||
| ) | ||
| if event + accumulated > N_TIMESTEPS: | ||
| break | ||
|
|
||
| start = accumulated + event | ||
| end = start + duration | ||
| self.availability[start:end] = 0 | ||
| accumulated = start | ||
| if not self.time_to_failures: | ||
| self.create_downtime_events() | ||
|
|
||
|
|
||
| @define(kw_only=True) | ||
| class FixedIntervalReliability(BaseConfig): | ||
| frequency: float = field(validator=(validators.instance_of((float, int)), validators.gt(0))) | ||
| downtime: Any = field(converter=generate_downtime_model) | ||
| downtime_per_event: np.ndarray = field(init=False, validator=validators.instance_of(np.ndarray)) | ||
| availability: np.ndarray = field( | ||
| default=np.ones(N_TIMESTEPS), init=False, validator=validators.instance_of(np.ndarray) | ||
| ) | ||
|
|
||
| def __attrs_post_init__(self): | ||
| self.create_downtime_events() | ||
| self.calculate_availability() | ||
|
|
||
| def create_downtime_events(self): | ||
| """Creates a ``time_to_failure`` and ``downtime_per_event``.""" | ||
| self.time_to_failures = ... # TODO | ||
| self.downtime_per_event = self.downtime.sample_downtime() | ||
|
|
||
| def calculate_availability(self): | ||
| """Determine the timing and duration of outages for a single year of simulation time.""" | ||
| accumulated = 0 | ||
| while accumulated < N_TIMESTEPS: | ||
| event, self.time_to_failures = self.time_to_failures[0], self.time_to_failures[1:] | ||
| duration, self.downtime_per_event = ( | ||
| self.downtime_per_event[0], | ||
| self.downtime_per_event[1:], | ||
| ) | ||
| if event + accumulated > N_TIMESTEPS: | ||
| break | ||
|
|
||
| start = accumulated + event | ||
| end = start + duration | ||
| self.availability[start:end] = 0 | ||
| accumulated = start | ||
| if not self.time_to_failures: | ||
| self.create_downtime_events() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we "burn-in" here (run some number of simulated years before starting the sampling accumulation)? that way we won't have jan 01 correlated with a brand new system every time