|
| 1 | +from OMPython import ModelicaSystem |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +def main(file=None): |
| 5 | + """Run a simple parameters validation model.""" |
| 6 | + model = ModelicaSystem( |
| 7 | + str(Path(__file__).parent/'model.mo'), # Path to file where modelica code is. |
| 8 | + 'ParametersValidation', # Name of model from that file. |
| 9 | + ) |
| 10 | + # Run the model with different parameters values and print the results: |
| 11 | + for positive_number in [-1,1,.1]: |
| 12 | + for negative_number in [-1,1,-.1]: |
| 13 | + model.setParameters( |
| 14 | + [ |
| 15 | + f'positive_number={positive_number}', |
| 16 | + f'negative_number={negative_number}', |
| 17 | + ], |
| 18 | + ) |
| 19 | + print('--------------------------------------', file=file) |
| 20 | + print('Simulating with:', file=file) |
| 21 | + print(f'\tpositive_number = {positive_number}', file=file) |
| 22 | + print(f'\tnegative_number = {negative_number}', file=file) |
| 23 | + try: |
| 24 | + model.simulate() # Parameters values are validated here. |
| 25 | + except Exception as e: |
| 26 | + print(e, file=file) |
| 27 | + |
| 28 | +def test_passing()->bool: |
| 29 | + """Function to use for automatic testing. If returns `True` it means this example is working.""" |
| 30 | + class PrintSilencer: |
| 31 | + def write(s:str): |
| 32 | + pass |
| 33 | + test_passing = False |
| 34 | + try: |
| 35 | + main(file=PrintSilencer()) |
| 36 | + test_passing = True |
| 37 | + except Exception as e: |
| 38 | + test_passing = False |
| 39 | + return test_passing |
| 40 | + |
| 41 | +if __name__ == '__main__': |
| 42 | + main() |
0 commit comments