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
22 changes: 0 additions & 22 deletions definitions.py

This file was deleted.

44 changes: 3 additions & 41 deletions temoa/_internal/run_actions.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,7 @@
"""
Basic-level atomic functions that can be used by a sequencer, as needed

Written by: J. F. Hyink
jeff@westernspark.us
https://westernspark.us
Created on: 11/15/23

Tools for Energy Model Optimization and Analysis (Temoa):
An open source framework for energy systems optimization modeling

Copyright (C) 2015, NC State University

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

A complete copy of the GNU General Public License v2 (GPLv2) is available
in LICENSE.txt. Users uncompressing this from an archive may not have
received this license file. If not, see <http://www.gnu.org/licenses/>.
"""

import os
import sqlite3
import sys
from logging import getLogger
Expand All @@ -46,7 +21,6 @@
)
from pyomo.opt import SolverResults

import definitions
from temoa._internal.table_writer import TableWriter
from temoa.core.config import TemoaConfig
from temoa.core.model import TemoaModel
Expand Down Expand Up @@ -157,21 +131,9 @@ def build_instance(
if not silent:
try:
# Check for warnings in log file to notify user. Ugly but it works
log_file = os.path.join(definitions.get_OUTPUT_PATH(), 'log.log')
with open(log_file) as f:
warnings_found = any(
'| WARNING |' in line or '| ERROR |' in line or '| CRITICAL |' in line
for line in f
)
if warnings_found:
sys.stderr.write(
'\r[%8.2f] Instance created with warnings. Check log file.\n'
% (time() - hack)
)
else:
sys.stderr.write(
'\r[%8.2f] Instance created. \n' % (time() - hack)
) # needs spaces to clear previous line
sys.stderr.write(
'\r[%8.2f] Instance created. \n' % (time() - hack)
) # needs spaces to clear previous line
sys.stderr.flush()
except Exception:
sys.stderr.write(
Expand Down
44 changes: 13 additions & 31 deletions temoa/_internal/table_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
tool for writing outputs to database tables
"""

from __future__ import annotations

import sqlite3
import sys
from collections import defaultdict
from collections.abc import Iterable
from importlib import resources
from logging import getLogger
from pathlib import Path
from typing import TYPE_CHECKING, cast

from pyomo.core import value
from pyomo.opt import SolverResults

from definitions import PROJECT_ROOT
from temoa._internal.data_brick import DataBrick
from temoa._internal.exchange_tech_cost_ledger import CostType
from temoa._internal.table_data_puller import (
Expand All @@ -39,30 +41,6 @@
pass

"""
Tools for Energy Model Optimization and Analysis (Temoa):
An open source framework for energy systems optimization modeling

Copyright (C) 2015, NC State University

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

A complete copy of the GNU General Public License v2 (GPLv2) is available
in LICENSE.txt. Users uncompressing this from an archive may not have
received this license file. If not, see <http://www.gnu.org/licenses/>.


Written by: J. F. Hyink
jeff@westernspark.us
https://westernspark.us
Created on: 2/9/24

Note: This file borrows heavily from the legacy pformat_results.py, and is somewhat of a restructure of that code
to accommodate the run modes more cleanly
Expand All @@ -85,10 +63,11 @@
]
optional_output_tables = ['output_flow_out_summary', 'output_mc_delta', 'output_storage_level']

flow_summary_file_loc = Path(
PROJECT_ROOT, 'temoa/extensions/modeling_to_generate_alternatives/make_flow_summary_table.sql'
flow_summary_file_loc = (
resources.files('temoa.extensions.modeling_to_generate_alternatives')
/ 'make_flow_summary_table.sql'
)
mc_tweaks_file_loc = Path(PROJECT_ROOT, 'temoa/extensions/monte_carlo/make_deltas_table.sql')
mc_tweaks_file_loc = resources.files('temoa.extensions.monte_carlo') / 'make_deltas_table.sql'
Comment thread
ParticularlyPythonicBS marked this conversation as resolved.


class TableWriter:
Expand Down Expand Up @@ -652,13 +631,16 @@ def make_mc_tweaks_table(self) -> None:
# make the table for monte carlo tweaks, if needed...
self.execute_script(mc_tweaks_file_loc)

def execute_script(self, script_file: str | Path) -> None:
def execute_script(self, script_file: str | Path | resources.abc.Traversable) -> None:
"""
A utility to execute a sql script on the current db connection
:return:
"""
with open(script_file) as table_script:
sql_commands = table_script.read()
if isinstance(script_file, resources.abc.Traversable):
sql_commands = script_file.read_text()
else:
with open(script_file) as table_script:
sql_commands = table_script.read()
logger.debug('Executing sql from file: %s ', script_file)

self.con.executescript(sql_commands)
Expand Down
2 changes: 0 additions & 2 deletions temoa/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from rich.logging import RichHandler
from rich.text import Text

from definitions import set_OUTPUT_PATH
from temoa._internal.temoa_sequencer import TemoaSequencer
from temoa.core.config import TemoaConfig
from temoa.core.modes import TemoaMode
Expand Down Expand Up @@ -87,7 +86,6 @@ def _setup_sequencer(
# Pass the silent flag to the logging setup
_setup_logging(final_output_path, debug=debug, silent=silent)

set_OUTPUT_PATH(final_output_path)
config = TemoaConfig.build_config(
config_file=config_file, output_path=final_output_path, silent=silent
)
Expand Down
13 changes: 8 additions & 5 deletions temoa/extensions/method_of_morris/morris.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# from __future__ import division
import time
from importlib import resources
from pathlib import Path

from pyomo.dataportal import DataPortal

from definitions import PROJECT_ROOT
from temoa._internal import run_actions
from temoa._internal.table_writer import TableWriter
from temoa.core.config import TemoaConfig
Expand Down Expand Up @@ -63,12 +63,15 @@ def evaluate(param_names, param_values, data: dict, k):
return Morris_Objectives


morris_root = Path(PROJECT_ROOT, 'temoa/extensions/method_of_morris')
morris_root = Path(__file__).parent
perturbation_coefficient = 0.2 # minus plus 10% of the baseline values
param_file = morris_root / 'm_params.txt'
db_file = Path(PROJECT_ROOT, 'data_files/untracked_data/morris/morris_utopia.sqlite')
config_path = Path(PROJECT_ROOT, 'data_files/untracked_data/morris/morris_utopia.toml')
with sqlite3.connect(db_file) as con:

db_resource = resources.files('data_files.untracked_data.morris') / 'morris_utopia.sqlite'
config_resource = resources.files('data_files.untracked_data.morris') / 'morris_utopia.toml'
with resources.as_file(db_resource) as db_file, \
resources.as_file(config_resource) as config_path, \
sqlite3.connect(str(db_file)) as con:
with open(param_file, 'w') as file:
param_names = {}
cur = con.cursor()
Expand Down
42 changes: 7 additions & 35 deletions temoa/extensions/method_of_morris/morris_sequencer.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,6 @@
"""
Tools for Energy Model Optimization and Analysis (Temoa):
An open source framework for energy systems optimization modeling

Copyright (C) 2015, NC State University

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

A complete copy of the GNU General Public License v2 (GPLv2) is available
in LICENSE.txt. Users uncompressing this from an archive may not have
received this license file. If not, see <http://www.gnu.org/licenses/>.

This code is modified from original work on Method of Morris framework
by Hadi Eshragi. Original morris.py file can be located in the energysystem
branch

Modified/Refactored by: J. F. Hyink
jeff@westernspark.us
https://westernspark.us
Created on: 5/30/24

An event sequencer to control the flow of a Method of Morris calculation. This code uses multiprocessing via
the joblib library

"""

import csv
Expand All @@ -39,6 +10,7 @@
import sys
import tomllib
from logging.handlers import QueueListener
from importlib import resources
from pathlib import Path

from joblib import Parallel, delayed
Expand All @@ -47,16 +19,15 @@
from SALib.sample.morris import sample
from SALib.util import compute_groups_matrix, read_param_file

from definitions import PROJECT_ROOT, get_OUTPUT_PATH
from temoa._internal.table_writer import TableWriter
from temoa.core.config import TemoaConfig
from temoa.data_io.hybrid_loader import HybridLoader
from temoa.extensions.method_of_morris.morris_evaluate import evaluate

logger = logging.getLogger(__name__)

solver_options_file = Path(
PROJECT_ROOT, 'temoa/extensions/method_of_morris/morris_solver_options.toml'
solver_options_file = (
resources.files('temoa.extensions.method_of_morris') / 'morris_solver_options.toml'
)
Comment thread
ParticularlyPythonicBS marked this conversation as resolved.


Expand Down Expand Up @@ -87,8 +58,9 @@ def __init__(self, config: TemoaConfig):

# read in the options
try:
with open(solver_options_file, 'rb') as f:
all_options = tomllib.load(f)
with resources.as_file(solver_options_file) as path:
with open(path, 'rb') as f:
all_options = tomllib.load(f)
s_options = all_options.get(self.config.solver_name, {})
logger.info('Using solver options: %s', s_options)

Expand All @@ -98,7 +70,7 @@ def __init__(self, config: TemoaConfig):

# output handling
self.verbose = False # for troubleshooting
self.mm_output_folder = get_OUTPUT_PATH() / 'MM_outputs'
self.mm_output_folder = self.config.output_path / 'MM_outputs'
self.mm_output_folder.mkdir(exist_ok=True)
self.param_file: Path = self.mm_output_folder / 'params.csv'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
"""
Tools for Energy Model Optimization and Analysis (Temoa):
An open source framework for energy systems optimization modeling

Copyright (C) 2015, NC State University

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

A complete copy of the GNU General Public License v2 (GPLv2) is available
in LICENSE.txt. Users uncompressing this from an archive may not have
received this license file. If not, see <http://www.gnu.org/licenses/>.


Written by: J. F. Hyink
jeff@westernspark.us
https://westernspark.us
Created on: 4/15/24

The purpose of this module is to perform top-level control over an MGA model run
Performs top-level control over an MGA model run
"""

import logging
Expand All @@ -35,6 +10,7 @@
from datetime import datetime
from logging import getLogger
from multiprocessing import Queue
from importlib import resources
from pathlib import Path
from queue import Empty

Expand All @@ -43,7 +19,6 @@
from pyomo.dataportal import DataPortal
from pyomo.opt import check_optimal_termination

from definitions import PROJECT_ROOT, get_OUTPUT_PATH
from temoa._internal.run_actions import build_instance
from temoa._internal.table_writer import TableWriter
from temoa.components.costs import total_cost_rule
Expand All @@ -58,8 +33,8 @@

logger = getLogger(__name__)

solver_options_path = Path(
PROJECT_ROOT, 'temoa/extensions/modeling_to_generate_alternatives/MGA_solver_options.toml'
solver_options_path = (
resources.files('temoa.extensions.modeling_to_generate_alternatives') / 'MGA_solver_options.toml'
)
Comment thread
ParticularlyPythonicBS marked this conversation as resolved.


Expand Down Expand Up @@ -88,8 +63,9 @@ def __init__(self, config: TemoaConfig):

# read in the options
try:
with open(solver_options_path, 'rb') as f:
all_options = tomllib.load(f)
with resources.as_file(solver_options_path) as path:
with open(path, 'rb') as f:
all_options = tomllib.load(f)
s_options = all_options.get(self.config.solver_name, {})
logger.info('Using solver options: %s', s_options)

Expand Down Expand Up @@ -208,6 +184,7 @@ def start(self):
con=self.con,
optimal_cost=tot_cost,
cost_relaxation=self.cost_epsilon,
config=self.config,
)

# 5. Set up the Workers
Expand All @@ -224,7 +201,7 @@ def start(self):
'solver_options': self.worker_solver_options,
}
# construct path for the solver logs
s_path = Path(get_OUTPUT_PATH(), 'solver_logs')
s_path = self.config.output_path / 'solver_logs'
if not s_path.exists():
s_path.mkdir()
for _ in range(num_workers):
Expand Down
Loading