Issue
I simulated a model with this script and now I just want to substitute T1 with a symbol t1 in a text file
How should change the T1 from array to something else so that I can substitute that ?
from pymodelica import compile_fmu
from pyfmi import load_fmu
# Import the plotting library
import matplotlib.pyplot as plt
import numpy as np
# Compile model
model_name = 'Testerequal'
mo_file = 'Testerequal.mo'
VDP_fmu = compile_fmu('Testerequal', compiler_options = {'extra_lib_dirs':'D:\JModelica.org-1.17\Testerequal'})
# Load model
vdpid = load_fmu(VDP_fmu, log_level=7)
res = vdpid.simulate(final_time=86400)
x0= res["int.y"]
t0= res["time"]
T1 = x0[40]
f = open('D:/JModelica.org-1.17/Testertwo/That.txt','r')
filedata = f.read()
f.close()
newdata = filedata.replace('t1',T1)
f = open('D:/JModelica.org-1.17/Testertwo/That.txt','w')
f.write(newdata)
f.close()
Solution
The problem is that you are trying to substitute an string with a numpy float.
The variable x0 in your case is a numpy array of floats and thus T1 is a numpy float. Now when you try to substitute t1 it will give you an error due to that T1 is not a string. If you just want to substitute the value of T1 for t1, just convert it to a string, i.e.:
filedata.replace("t1", str(T1))
Answered By - Christian Winther
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.