Skip to content

Commit

Permalink
Use a temporary directory for GAMSModel input/output GDX files
Browse files Browse the repository at this point in the history
  • Loading branch information
khaeru committed Jan 30, 2020
1 parent 6335564 commit 923dac8
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions ixmp/model/gams.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from pathlib import Path
from subprocess import check_call
from tempfile import TemporaryDirectory


from ixmp.backend.jdbc import JDBCBackend
Expand Down Expand Up @@ -76,15 +77,17 @@ class GAMSModel(Model):
defaults = {
'model_file': '{model_name}.gms',
'case': "{scenario.model}_{scenario.scenario}",
'in_file': '{model_name}_in.gdx',
'out_file': '{model_name}_out.gdx',
'in_file': str(Path('{temp_dir}', '{model_name}_in.gdx')),
'out_file': str(Path('{temp_dir}', '{model_name}_out.gdx')),
'solve_args': ['--in="{in_file}"', '--out="{out_file}"'],

# Not formatted
'gams_args': ['LogOption=4'],
'check_solution': True,
'comment': None,
'equ_list': None,
'var_list': None,
'use_temp_dir': True,
}

def __init__(self, name=None, **model_options):
Expand All @@ -100,6 +103,12 @@ def run(self, scenario):

self.scenario = scenario

if self.use_temp_dir:
# Create a temporary directory; automatically deleted at the end of
# the context
_temp_dir = TemporaryDirectory()
self.temp_dir = _temp_dir.name

def format(key):
value = getattr(self, key)
try:
Expand Down

0 comments on commit 923dac8

Please sign in to comment.