-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from NREL/feature/create-new-scenario
extending cli to add ability to add a scenario to an existing project
- Loading branch information
Showing
8 changed files
with
125 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
__version__ = "3.0.1" | ||
__version__ = "3.0.2" | ||
|
||
|
||
from PyDSS.utils.timing_utils import timer_stats_collector | ||
|
This file contains 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,58 @@ | ||
from pathlib import Path | ||
import toml | ||
import click | ||
import os | ||
|
||
from PyDSS.simulation_input_models import MappedControllers | ||
from PyDSS.pydss_project import PyDssScenario | ||
from PyDSS.common import CONTROLLER_TYPES, ControllerType | ||
|
||
def build_scenario(project_path:str, scenario_name:str, controller_mapping:str): | ||
project_path = Path(project_path) | ||
controller_mapping_path = Path(controller_mapping) | ||
assert project_path.exists(), "Provided project path does not exist" | ||
assert (project_path / "Scenarios").exists(), "provided project is not a valid PyDSS project" | ||
assert controller_mapping_path.exists(), "rovided controller_mapping file does not exist" | ||
assert controller_mapping_path.suffix.lower() == ".toml", "controller_mapping should be a TOML file" | ||
|
||
controller_map = toml.load(controller_mapping_path) | ||
mapped_controllers = MappedControllers(**controller_map) | ||
acceptable_controller_types = CONTROLLER_TYPES | ||
controllers = {} | ||
for controller in mapped_controllers.mapping: | ||
settings_path_obj = Path(controller.controller_file) | ||
assert controller.controller_type in ControllerType, \ | ||
f"{controller.controller_type} is not a valid contoller. Options are {acceptable_controller_types}" | ||
assert settings_path_obj.exists(), \ | ||
f"file for controller type {controller.controller_type} does not exist" | ||
controller_data = toml.load(settings_path_obj) | ||
if controller_data: | ||
if controller.controller_type in controllers: | ||
msg= f"Multiple keys files for the same controller type {controller.controller_type}." \ | ||
"Each controller type can only be attached to a single file." | ||
raise ValueError(msg) | ||
controllers[controller.controller_type] = toml.load(settings_path_obj) | ||
scenario_dir = project_path / "Scenarios" / scenario_name | ||
scenario_obj = PyDssScenario( | ||
[scenario_name], list(controllers.keys()), export_modes=None, visualization_types=None | ||
) | ||
scenario_obj.serialize(str(scenario_dir)) | ||
|
||
@click.argument("project-path", type=click.Path(exists=True)) | ||
@click.option( | ||
"-s", "--scenario_name", | ||
required=True, | ||
help="name of the new scenario", | ||
) | ||
@click.option( | ||
"-c", "--controller-mapping", | ||
required=True, | ||
default=None, | ||
type=click.Path(exists=True), | ||
help="JSON file that maps controller type to controller definition files", | ||
) | ||
@click.command() | ||
def add_scenario(project_path:str, scenario_name:str, controller_mapping:str): | ||
"""Add a new scenario to an existing project""" | ||
build_scenario(project_path, scenario_name, controller_mapping) | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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,24 @@ | ||
["Load.motor_1"] | ||
Kp1 = 0.0 | ||
Np1 = 1.0 | ||
Kp2 = 12.0 | ||
Np2 = 3.2 | ||
Kq1 = 6.0 | ||
Nq1 = 2.0 | ||
Kq2 = 11.0 | ||
Nq2 = 2.5 | ||
Tth = 4.0 | ||
Frst = 0.20 | ||
LFadj = 0.0 | ||
Tth1t = 0.7 | ||
Tth2t = 1.9 | ||
Pfault = 3.0 | ||
Qfault = 5.0 | ||
Vstall = 0.55 | ||
Vbreak = 0.86 | ||
Vrstrt = 0.95 | ||
Tstall = 0.032 | ||
Trestart = 0.3 | ||
ratedKW = 7.00 | ||
ratedPF = 0.939 | ||
R_stall_pu = 0.10 |
This file contains 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,3 @@ | ||
[[mapping]] | ||
controller_type = "MotorStall" | ||
controller_file = "./tests/data/add_scenario/MotorStall.toml" |
This file contains 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,27 @@ | ||
from click.testing import CliRunner | ||
from pathlib import Path | ||
import tempfile | ||
import shutil | ||
import toml | ||
import os | ||
|
||
from PyDSS.cli.add_scenario import build_scenario | ||
|
||
PYDSS_PROJECT = Path(__file__).parent / "data" / "project" | ||
MAPPING_FILE = Path(__file__).parent / "data" / "add_scenario" / "controller_map.toml" | ||
|
||
|
||
|
||
def test_add_scenario(): | ||
|
||
with tempfile.TemporaryDirectory() as tmpdirname: | ||
tmpdirname_obj = Path(tmpdirname) / "tmp" | ||
shutil.copytree(PYDSS_PROJECT, str(tmpdirname_obj)) | ||
settings_path = tmpdirname_obj / "simulation.toml" | ||
settings = toml.load(settings_path) | ||
settings['Project']['Project Path'] = str(tmpdirname_obj.absolute()) | ||
toml.dump(settings, open(settings_path, "w")) | ||
build_scenario(str(tmpdirname_obj), "test_scenario", str(MAPPING_FILE)) | ||
assert (tmpdirname_obj / "Scenarios" / "test_scenario").exists() | ||
assert (tmpdirname_obj / "Scenarios" / "test_scenario" / "pyControllerList" / "MotorStall.toml").exists() | ||
|