-
Notifications
You must be signed in to change notification settings - Fork 0
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 #26 from ESSS/PWPA-2291-improve-converter-api
[PWPA-2291] Improve the converter API
- Loading branch information
Showing
15 changed files
with
145 additions
and
137 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
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
29 changes: 12 additions & 17 deletions
29
src/alfasim_score/converter/alfacase/_tests/test_output_results.py
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,27 +1,22 @@ | ||
import json | ||
import pandas as pd | ||
import pytest | ||
from barril.units import Scalar | ||
from pathlib import Path | ||
from pytest_regressions.file_regression import FileRegressionFixture | ||
|
||
from alfasim_score.common import AnnulusLabel | ||
from alfasim_score.converter.alfacase.score_output_generator import ScoreOutputGenerator | ||
from alfasim_score.converter.alfacase.alfasim_score_converter import AlfasimScoreConverter | ||
|
||
|
||
def test_generate_output_file_results( | ||
shared_datadir: Path, datadir: Path, file_regression: FileRegressionFixture | ||
) -> None: | ||
alfasim_results_directory = shared_datadir / "case.data" | ||
well_start_position = Scalar(2072, "m") | ||
active_annuli = [AnnulusLabel.A, AnnulusLabel.B, AnnulusLabel.C] | ||
# It was defined to use 6 wall layers as output from ALFAsim | ||
layers = list(range(6)) | ||
output_generator = ScoreOutputGenerator( | ||
alfasim_results_directory, well_start_position, active_annuli, layers | ||
) | ||
output_generator.element_name = "7-SRR-2-RJS (2022-07-28_15-01-27)" | ||
output_filepath = datadir / "output_score.json" | ||
output_generator.generate_output_file(datadir / "output_score.json") | ||
output_content = output_filepath.read_text(encoding="utf-8") | ||
alfasim_results_path = shared_datadir / "case.data" | ||
# dummy input file just to have the reader for this test | ||
score_input_file = shared_datadir / "score_input_natural_flow.json" | ||
output_file = datadir / "output_score.json" | ||
converter = AlfasimScoreConverter(score_input_file, output_file) | ||
# change the element name to match this test result well name | ||
converter.output_builder.element_name = "7-SRR-2-RJS (2022-07-28_15-01-27)" | ||
# TODO: remember to get these annuli inside the output class | ||
converter.output_builder.active_annuli = [AnnulusLabel.A, AnnulusLabel.B, AnnulusLabel.C] | ||
converter.generate_score_output_file(alfasim_results_path) | ||
output_content = converter.output_builder.score_output_filepath.read_text(encoding="utf-8") | ||
file_regression.check(output_content, extension=".json", encoding="utf-8") |
43 changes: 43 additions & 0 deletions
43
src/alfasim_score/converter/alfacase/alfasim_score_converter.py
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,43 @@ | ||
import json | ||
from alfasim_sdk import convert_description_to_alfacase | ||
from pathlib import Path | ||
|
||
from alfasim_score.common import OperationType | ||
from alfasim_score.converter.alfacase.base_operation import BaseOperationBuilder | ||
from alfasim_score.converter.alfacase.injection_operation import InjectionOperationBuilder | ||
from alfasim_score.converter.alfacase.production_operation import ProductionOperationBuilder | ||
from alfasim_score.converter.alfacase.score_input_reader import ScoreInputReader | ||
from alfasim_score.converter.alfacase.score_output_generator import ScoreOutputBuilder | ||
|
||
|
||
class AlfasimScoreConverter: | ||
""" | ||
This class handles the process of convertions between ALFAsim and SCORE file formats: | ||
- it can convert the SCORE input file format into an alfacase to be used by ALFAsim simulator | ||
- it can use a ALFAsim result into a SCORE output file. | ||
""" | ||
|
||
def __init__(self, score_input_file: Path, score_output_file: Path): | ||
self.score_input = ScoreInputReader(score_input_file) | ||
self.alfacase_builder = self._get_score_to_alfacase_builder() | ||
self.output_builder = ScoreOutputBuilder(self.score_input, score_output_file) | ||
|
||
def _get_score_to_alfacase_builder(self) -> BaseOperationBuilder: | ||
"""Convert SCORE input file to an alfacase description.""" | ||
operation_type = self.score_input.read_operation_type() | ||
if operation_type == OperationType.PRODUCTION: | ||
return ProductionOperationBuilder(self.score_input) | ||
else: | ||
return InjectionOperationBuilder(self.score_input) | ||
|
||
def generate_alfasim_input_file(self, alfacase_filepath: Path) -> None: | ||
"""Create the ALFAsim input file (AKA alfacase) from an SCORE input file.""" | ||
alfacase_description = self.alfacase_builder.generate_operation_alfacase_description() | ||
alfacase_content = convert_description_to_alfacase(alfacase_description) | ||
alfacase_filepath.write_text(data=alfacase_content, encoding="utf-8") | ||
|
||
def generate_score_output_file(self, alfasim_results_folder: Path) -> None: | ||
"""Create the output file for SCORE based on the results generated by ALFAsim.""" | ||
output = self.output_builder.generate_output_results(alfasim_results_folder) | ||
json_data = json.dumps(output, indent=2) | ||
self.output_builder.score_output_filepath.write_text(json_data, encoding="utf-8") |
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.