Skip to content

Commit

Permalink
Finished check for file before loading inc files (#17)
Browse files Browse the repository at this point in the history
* Started work on issue #12 by renaming the output .gdx file to %scenario_input_data.gdx instead of generic _gams_py_gdb0.gdx

* Finished check for file before loading inc files

* Release 0.3.10

* Merge branch 'master' into check_for_loaded_incfiles
  • Loading branch information
Mathias157 authored Sep 25, 2024
1 parent e212768 commit 1749c5a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
project = "pybalmorel"
copyright = "2024, Mathias Berg Rosendal, Théodore Le Nalinec"
author = "Mathias Berg Rosendal, Théodore Le Nalinec"
release = "0.3.9"
release = "0.3.10"

exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", ".testenv", ".testenv/**"]

Expand Down
2 changes: 1 addition & 1 deletion docs/get_started/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ dependencies:
- pip:
- gamsapi[transfer]>=45.0.0
- eel>=0.17.0
- pybalmorel==0.3.9
- pybalmorel==0.3.10
```
2 changes: 1 addition & 1 deletion environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ dependencies:
- pip:
- gamsapi[transfer]>=45.0.0
- eel>=0.17.0
- pybalmorel==0.3.9
- pybalmorel==0.3.10
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "pybalmorel"
version = "0.3.9"
version = "0.3.10"
maintainers = [
{ name="Mathias Berg Rosendal", email="[email protected]"},
{ name="Théodore Le Nalinec"},
Expand Down
61 changes: 37 additions & 24 deletions src/pybalmorel/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,15 @@ def run(self, scenario: str, cmd_line_options: dict = {}):
def load_incfiles(self,
scenario: str = 'base',
use_provided_read_files: bool = True,
read_file: str = 'Balmorel_ReadData'):
read_file: str = 'Balmorel_ReadData',
overwrite: bool = False):
"""Will load .inc files from the specific scenario
Args:
scenario (str, optional): The scenario that you . Defaults to 'base'.
use_provided_read_files (bool, optional): Use provided Balmorel_ReadData.gms and Balmorelbb4_ReadData.inc. Defaults to True.
read_file (str, optional): The name of the read file to be executed. Defaults to Balmorel_ReadData
overwrite (bool, optional): Will overwrite an existing %scenario%_input_data.gdx file from a previous .load_incfiles execution
Raises:
KeyError: _description_
Expand All @@ -368,33 +370,44 @@ def load_incfiles(self,
if not(scenario in self.scenarios):
raise KeyError('%s scenario wasnt found.\nRun this Balmorel(...) class again if you just created the %s scenario.'%(scenario, scenario))


# Path to the GAMS system directory
model_folder = os.path.join(self.path, scenario, 'model')

if os.path.exists(os.path.join(model_folder, '%s_input_data.gdx'%scenario)) and not(overwrite):
ws = gams.GamsWorkspace()
db = ws.add_database_from_gdx(os.path.join(model_folder, '%s_input_data.gdx'%scenario))
self.input_data[scenario] = db

else:
# Are you using the provided 'ReadData'-Balmorel files or a custom one?
use_provided_read_files = True
if use_provided_read_files:
pkgdir = sys.modules['pybalmorel'].__path__[0]
# Copy Balmorel_ReadData and Balmorelbb4_ReadData
# into the model folder if there isn't one already
for file in ['Balmorel_ReadData.gms', 'Balmorelbb4_ReadData.inc']:
if not(os.path.exists(os.path.join(model_folder, file))):
shutil.copyfile(os.path.join(pkgdir, file), os.path.join(model_folder, file))
print(os.path.join(model_folder, file))

# Initialize GAMS Workspace
ws = gams.GamsWorkspace(working_directory=model_folder)

# Set options
opt = ws.add_options()
opt.gdx = '%s_input_data.gdx'%scenario # Setting the output gdx name (note, could be overwritten by the cmd line options, which is intended)

# Load the GAMS model
model_db = ws.add_job_from_file(os.path.join(model_folder, read_file), job_name=scenario)

# Are you using the provided 'ReadData'-Balmorel files or a custom one?
use_provided_read_files = True
if use_provided_read_files:
pkgdir = sys.modules['pybalmorel'].__path__[0]
# Copy Balmorel_ReadData and Balmorelbb4_ReadData
# into the model folder if there isn't one already
for file in ['Balmorel_ReadData.gms', 'Balmorelbb4_ReadData.inc']:
if not(os.path.exists(os.path.join(model_folder, file))):
shutil.copyfile(os.path.join(pkgdir, file), os.path.join(model_folder, file))
print(os.path.join(model_folder, file))

# Initialize GAMS Workspace
ws = gams.GamsWorkspace(working_directory=model_folder)

# Load the GAMS model
model_db = ws.add_job_from_file(os.path.join(model_folder, read_file))

# Run the GAMS file
model_db.run()
# Run the GAMS file
model_db.run(opt)

# Store the database (will take some minutes)
self.input_data[scenario] = model_db.get_out_db()


# Store the database (will take some minutes)
self.input_data[scenario] = model_db.get_out_db()
class GUI:
def __init__(self) -> None:
pass
Expand Down

0 comments on commit 1749c5a

Please sign in to comment.