diff --git a/simccs_maptool/simccs_helper.py b/simccs_maptool/simccs_helper.py index 3e6c573..3377ae6 100644 --- a/simccs_maptool/simccs_helper.py +++ b/simccs_maptool/simccs_helper.py @@ -1,6 +1,8 @@ +import glob import io import logging import os +import re import shutil logger = logging.getLogger(__name__) @@ -85,7 +87,7 @@ def get_sinks_file(scenario_dir): def get_mps_file(scenario_dir): - return os.path.join(scenario_dir, "MIP", "cap.mps") + return next(glob.iglob(os.path.join(scenario_dir, "MIP", "*.mps")), None) def get_candidate_network_file(scenario_dir): @@ -135,9 +137,11 @@ def _get_scenario_path_components(scenario_dir): def write_mps_file( - scenario_dir, capital_recovery_rate=0.1, num_years=10, capacity_target=5 + scenario_dir, capital_recovery_rate=0.1, num_years=10, capacity_target=5, filename=None ): + if filename is not None and not filename.endswith(".mps"): + raise Exception(f"Invalid filename {filename}: must end with .mps") try: basepath, dataset_dirname, scenario = _get_scenario_path_components( scenario_dir @@ -166,6 +170,11 @@ def write_mps_file( scenario, "c", # modelVersion = "c" for capture ) + if filename is not None: + mps_fullpath = get_mps_file(scenario_dir) + normalized_filename = re.sub(r'[^\w.]+', '_', filename) + os.rename(mps_fullpath, + os.path.join(os.path.dirname(mps_fullpath), normalized_filename)) except Exception as e: logger.exception( "Error occurred when calling writeCapPriceMPS: " + str(e.stacktrace) diff --git a/simccs_maptool/templates/simccs_maptool/build.html b/simccs_maptool/templates/simccs_maptool/build.html index c6346dc..9467896 100644 --- a/simccs_maptool/templates/simccs_maptool/build.html +++ b/simccs_maptool/templates/simccs_maptool/build.html @@ -332,7 +332,7 @@ //generate MPS file -function generatempsfile(panelid,showmodal=1,paras=[]) { +function generatempsfile(panelid,showmodal=1,paras=[], filename=null) { // get the data from panelid //var source_selection = sourceselection_panel[panelid]; //var sink_selection = sinkselection_panel[panelid]; @@ -376,6 +376,9 @@ formData.set('sources', sourcedata); formData.set('sinks', sinkdata); formData.set('dataset', current_dataset_id); + if (filename) { + formData.set('mpsFilename', filename); + } // check if needs run candidate network _cached // Use the cachedCandidateNetwork if the selected sources and sinks are the same @@ -431,12 +434,14 @@ //console.log(panelid); var cplexInputValues; var mpsRequest = null; + // Name the mps file based on the scenario name + var filename = scenario_title_panel[panelid] + ".mps"; if (batchmode == 0) { - mpsRequest = generatempsfile(panelid,showmodal=0).then((result) => {cplexInputValues = result}); + mpsRequest = generatempsfile(panelid,showmodal=0,[],filename).then((result) => {cplexInputValues = result}); } // force regenerate mps for batch mode if (batchmode == 1) { - mpsRequest = generatempsfile(panelid,showmodal=0,paras).then((result) => {cplexInputValues = result}); + mpsRequest = generatempsfile(panelid,showmodal=0,paras,filename).then((result) => {cplexInputValues = result}); } mpsRequest.then(() => { diff --git a/simccs_maptool/views.py b/simccs_maptool/views.py index 190d029..5e6f2fd 100644 --- a/simccs_maptool/views.py +++ b/simccs_maptool/views.py @@ -113,6 +113,7 @@ def generate_mps(request): sinks = request.POST["sinks"] dataset_id = request.POST["dataset"] candidate_network = request.POST.get("candidateNetwork", None) + mps_filename = request.POST.get("mpsFilename", None) with tempfile.TemporaryDirectory() as datasets_basepath: dataset_dir = datasets.get_dataset_dir(dataset_id) @@ -142,6 +143,7 @@ def generate_mps(request): capital_recovery_rate=capital_recovery_rate, num_years=num_years, capacity_target=capacity_target, + filename=mps_filename, ) with open( simccs_helper.get_sources_file(scenario_dir)