|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | +import json |
| 4 | +import os |
| 5 | +import argparse |
| 6 | +from enum import Enum |
| 7 | +import sys |
| 8 | + |
| 9 | + |
| 10 | +class Participant(Enum): |
| 11 | + MASS_LEFT = "Mass-Left" |
| 12 | + MASS_RIGHT = "Mass-Right" |
| 13 | + |
| 14 | + |
| 15 | +parser = argparse.ArgumentParser() |
| 16 | +parser.add_argument("fmi_setting_file_left", help="Path to the fmi setting file for MassLeft.", type=str) |
| 17 | +parser.add_argument("precice_setting_file_left", help="Path to the precice setting file for MassLeft.", type=str) |
| 18 | +parser.add_argument("fmi_setting_file_right", help="Path to the fmi setting file for MassRight.", type=str) |
| 19 | +parser.add_argument("precice_setting_file_right", help="Path to the precice setting file for MassRight.", type=str) |
| 20 | +parser.add_argument("participant_name", help="Participant for which the error should be calculated", type=str, |
| 21 | + choices=[p.value for p in Participant]) |
| 22 | +args = parser.parse_args() |
| 23 | + |
| 24 | +# Get input files |
| 25 | +fmi_file_left = args.fmi_setting_file_left |
| 26 | +precice_file_left = args.precice_setting_file_left |
| 27 | +fmi_file_right = args.fmi_setting_file_right |
| 28 | +precice_file_right = args.precice_setting_file_right |
| 29 | +participant_name = args.participant_name |
| 30 | + |
| 31 | +# Read json files |
| 32 | +folder = os.path.dirname(os.path.join(os.getcwd(), os.path.dirname(sys.argv[0]), fmi_file_left)) |
| 33 | +path = os.path.join(folder, os.path.basename(fmi_file_left)) |
| 34 | +read_file = open(path, "r") |
| 35 | +fmi_data_left = json.load(read_file) |
| 36 | + |
| 37 | +folder = os.path.dirname(os.path.join(os.getcwd(), os.path.dirname(sys.argv[0]), precice_file_left)) |
| 38 | +path = os.path.join(folder, os.path.basename(precice_file_left)) |
| 39 | +read_file = open(path, "r") |
| 40 | +precice_data_left = json.load(read_file) |
| 41 | + |
| 42 | +folder = os.path.dirname(os.path.join(os.getcwd(), os.path.dirname(sys.argv[0]), fmi_file_right)) |
| 43 | +path = os.path.join(folder, os.path.basename(fmi_file_right)) |
| 44 | +read_file = open(path, "r") |
| 45 | +fmi_data_right = json.load(read_file) |
| 46 | + |
| 47 | +folder = os.path.dirname(os.path.join(os.getcwd(), os.path.dirname(sys.argv[0]), precice_file_right)) |
| 48 | +path = os.path.join(folder, os.path.basename(precice_file_right)) |
| 49 | +read_file = open(path, "r") |
| 50 | +precice_data_right = json.load(read_file) |
| 51 | + |
| 52 | + |
| 53 | +# Define variables |
| 54 | +k_1 = fmi_data_left["model_params"]["spring_fixed.c"] |
| 55 | +k_2 = fmi_data_right["model_params"]["spring_fixed.c"] |
| 56 | +u0_1 = fmi_data_left["initial_conditions"]["mass.u"] |
| 57 | +u0_2 = fmi_data_right["initial_conditions"]["mass.u"] |
| 58 | +k_12_left = fmi_data_left["model_params"]["spring_middle.c"] |
| 59 | +k_12_right = fmi_data_right["model_params"]["spring_middle.c"] |
| 60 | + |
| 61 | +if k_12_left == k_12_right: |
| 62 | + k_12 = k_12_left |
| 63 | +else: |
| 64 | + raise Exception("k_12 has to be equal in both participants. Please adjust input values.") |
| 65 | + |
| 66 | + |
| 67 | +# Define analytical solution and read computed results |
| 68 | +K = np.array([[k_1 + k_12, -k_12], [-k_12, k_2 + k_12]]) |
| 69 | +eigenvalues, eigenvectors = np.linalg.eig(K) |
| 70 | +omega = np.sqrt(eigenvalues) |
| 71 | +A, B = eigenvectors |
| 72 | +c = np.linalg.solve(eigenvectors, [u0_1, u0_2]) |
| 73 | + |
| 74 | +if participant_name == Participant.MASS_LEFT.value: |
| 75 | + filename = fmi_data_left["simulation_params"]["output_file_name"] |
| 76 | + df = pd.read_csv(filename, delimiter=',') |
| 77 | + |
| 78 | + def u_analytical(t): return c[0] * A[0] * np.cos(omega[0] * t) + c[1] * A[1] * np.cos(omega[1] * t) |
| 79 | + |
| 80 | +elif participant_name == Participant.MASS_RIGHT.value: |
| 81 | + filename = fmi_data_right["simulation_params"]["output_file_name"] |
| 82 | + df = pd.read_csv(filename, delimiter=',') |
| 83 | + |
| 84 | + def u_analytical(t): return c[0] * B[0] * np.cos(omega[0] * t) + c[1] * B[1] * np.cos(omega[1] * t) |
| 85 | + |
| 86 | +times = df.iloc[:, 0] |
| 87 | +positions = df.iloc[:, 1] |
| 88 | + |
| 89 | + |
| 90 | +# Calculate error |
| 91 | +error = np.max(abs(u_analytical(np.array(times)) - np.array(positions))) |
| 92 | +print("Error w.r.t analytical solution:") |
| 93 | +print(f"{error}") |
0 commit comments