-
-
Notifications
You must be signed in to change notification settings - Fork 122
Add FMU version to oscillator example #322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
LeonardWilleke
wants to merge
13
commits into
precice:develop
from
LeonardWilleke:create-fmu-oscillator
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4c92b98
Add FMU example. Exchanged data switches from 'Force' to 'Displacement'.
42884c1
Update README
d7fee6c
Update README.md
LeonardWilleke 8209447
Use Oscillator.fmu and force-force exchange
a4ee334
Adapt README.md
d32e15e
Adapt README.md
bf5b8aa
Minor changes to runner.py
fce783e
Update FMU model and README.md
a70d411
Update make files to allow compilation as FMI 1, 2 and 3
2b3157f
Implement requested changes
9849995
Adapt case to use fmiprecice instead of local runner script
f17125e
Update License and README, do cosmetic changes on code
f5d9fde
Add link to reference
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,20 @@ | ||
{ | ||
"model_params": { | ||
"mass.m": 1, | ||
"spring_fixed.c": 39.478, | ||
"spring_middle.c": 157.914 | ||
}, | ||
"initial_conditions": { | ||
"mass.u": 1, | ||
"mass.v": 0, | ||
"mass.a": -197.392 | ||
}, | ||
"simulation_params": { | ||
"fmu_file_name": "./Oscillator.fmu", | ||
"output_file_name": "./output/trajectory-Mass-Left.csv", | ||
"output": ["mass.u", "mass.v"], | ||
"fmu_read_data_names": ["force_in"], | ||
"fmu_write_data_names": ["force_out"], | ||
"fmu_instance_name": "instance_left" | ||
} | ||
} |
This file contains hidden or 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,9 @@ | ||
{ | ||
"coupling_params": { | ||
"participant_name": "Mass-Left", | ||
"config_file_name": "../precice-config.xml", | ||
"mesh_name": "Mass-Left-Mesh", | ||
"write_data": {"name": "Force-Left", "type": "scalar"}, | ||
"read_data": {"name": "Force-Right", "type": "scalar"} | ||
} | ||
} |
This file contains hidden or 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,20 @@ | ||
{ | ||
"model_params": { | ||
"mass.m": 1, | ||
"spring_fixed.c": 39.478, | ||
"spring_middle.c": 157.914 | ||
}, | ||
"initial_conditions": { | ||
"mass.u": 0, | ||
"mass.v": 0, | ||
"mass.a": 157.914 | ||
}, | ||
"simulation_params": { | ||
"fmu_file_name": "./Oscillator.fmu", | ||
"output_file_name": "./output/trajectory-Mass-Right.csv", | ||
"output": ["mass.u", "mass.v"], | ||
"fmu_read_data_names": ["force_in"], | ||
"fmu_write_data_names": ["force_out"], | ||
"fmu_instance_name": "instance_right" | ||
} | ||
} |
This file contains hidden or 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,9 @@ | ||
{ | ||
"coupling_params": { | ||
"participant_name": "Mass-Right", | ||
"config_file_name": "../precice-config.xml", | ||
"mesh_name": "Mass-Right-Mesh", | ||
"write_data": {"name": "Force-Right", "type": "scalar"}, | ||
"read_data": {"name": "Force-Left", "type": "scalar"} | ||
} | ||
} |
Binary file not shown.
This file contains hidden or 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,91 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import json | ||
import os | ||
import argparse | ||
from enum import Enum | ||
import sys | ||
|
||
class Participant(Enum): | ||
MASS_LEFT = "Mass-Left" | ||
MASS_RIGHT = "Mass-Right" | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument("fmi_setting_file_left", help="Path to the fmi setting file for MassLeft.", type=str) | ||
parser.add_argument("precice_setting_file_left", help="Path to the precice setting file for MassLeft.", type=str) | ||
parser.add_argument("fmi_setting_file_right", help="Path to the fmi setting file for MassRight.", type=str) | ||
parser.add_argument("precice_setting_file_right", help="Path to the precice setting file for MassRight.", type=str) | ||
parser.add_argument("participant_name", help="Participant for which the error should be calculated", type=str, | ||
choices=[p.value for p in Participant]) | ||
args = parser.parse_args() | ||
|
||
# Get input files | ||
fmi_file_left = args.fmi_setting_file_left | ||
precice_file_left = args.precice_setting_file_left | ||
fmi_file_right = args.fmi_setting_file_right | ||
precice_file_right = args.precice_setting_file_right | ||
participant_name = args.participant_name | ||
|
||
# Read json files | ||
folder = os.path.dirname(os.path.join(os.getcwd(), os.path.dirname(sys.argv[0]), fmi_file_left)) | ||
path = os.path.join(folder, os.path.basename(fmi_file_left)) | ||
read_file = open(path, "r") | ||
fmi_data_left = json.load(read_file) | ||
|
||
folder = os.path.dirname(os.path.join(os.getcwd(), os.path.dirname(sys.argv[0]), precice_file_left)) | ||
path = os.path.join(folder, os.path.basename(precice_file_left)) | ||
read_file = open(path, "r") | ||
precice_data_left = json.load(read_file) | ||
|
||
folder = os.path.dirname(os.path.join(os.getcwd(), os.path.dirname(sys.argv[0]), fmi_file_right)) | ||
path = os.path.join(folder, os.path.basename(fmi_file_right)) | ||
read_file = open(path, "r") | ||
fmi_data_right = json.load(read_file) | ||
|
||
folder = os.path.dirname(os.path.join(os.getcwd(), os.path.dirname(sys.argv[0]), precice_file_right)) | ||
path = os.path.join(folder, os.path.basename(precice_file_right)) | ||
read_file = open(path, "r") | ||
precice_data_right = json.load(read_file) | ||
|
||
|
||
# Define variables | ||
k_1 = fmi_data_left["model_params"]["spring_fixed.c"] | ||
k_2 = fmi_data_right["model_params"]["spring_fixed.c"] | ||
u0_1 = fmi_data_left["initial_conditions"]["mass.u"] | ||
u0_2 = fmi_data_right["initial_conditions"]["mass.u"] | ||
k_12_left = fmi_data_left["model_params"]["spring_middle.c"] | ||
k_12_right = fmi_data_right["model_params"]["spring_middle.c"] | ||
|
||
if k_12_left == k_12_right: | ||
k_12 = k_12_left | ||
else: | ||
raise Exception("k_12 has to be equal in both participants. Please adjust input values.") | ||
|
||
|
||
# Define analytical solution and read computed results | ||
K = np.array([[k_1 + k_12, -k_12], [-k_12, k_2 + k_12]]) | ||
eigenvalues, eigenvectors = np.linalg.eig(K) | ||
omega = np.sqrt(eigenvalues) | ||
A, B = eigenvectors | ||
c = np.linalg.solve(eigenvectors, [u0_1, u0_2]) | ||
|
||
if participant_name == Participant.MASS_LEFT.value: | ||
filename = fmi_data_left["simulation_params"]["output_file_name"] | ||
df = pd.read_csv(filename, delimiter=',') | ||
|
||
def u_analytical(t): return c[0] * A[0] * np.cos(omega[0] * t) + c[1] * A[1] * np.cos(omega[1] * t) | ||
|
||
elif participant_name == Participant.MASS_RIGHT.value: | ||
filename = fmi_data_right["simulation_params"]["output_file_name"] | ||
df = pd.read_csv(filename, delimiter=',') | ||
|
||
def u_analytical(t): return c[0] * B[0] * np.cos(omega[0] * t) + c[1] * B[1] * np.cos(omega[1] * t) | ||
|
||
times = df.iloc[:,0] | ||
positions = df.iloc[:,1] | ||
|
||
|
||
# Calculate error | ||
error = np.max(abs(u_analytical(np.array(times)) - np.array(positions))) | ||
print("Error w.r.t analytical solution:") | ||
print(f"{error}") |
This file contains hidden or 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,8 @@ | ||
#!/bin/sh | ||
set -e -u | ||
|
||
. ../../tools/cleaning-tools.sh | ||
|
||
rm -rfv ./output/ | ||
|
||
clean_precice_logs . |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.