本文整理汇总了Python中pyfmi.load_fmu函数的典型用法代码示例。如果您正苦于以下问题:Python load_fmu函数的具体用法?Python load_fmu怎么用?Python load_fmu使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了load_fmu函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load
def load(self, fmu_path, output_dir):
"""Load the FMU for continued simulation in :meth:`continue_run`.
"""
import pyfmi
if 'log_level' in self._options:
self.fmu = pyfmi.load_fmu(fmu_path, log_level=self.log_level)
else:
self.fmu = pyfmi.load_fmu(fmu_path)
# Initialize the fmu, only call setup_experiment for FMUs 2.0
try:
self.fmu.setup_experiment()
version = 2
except AttributeError:
version = 1
self.fmu.initialize()
# Copy the log file to the result directory
log = ''
if version == 1:
log = self.fmu.get_identifier()
if version == 2:
log = self.fmu.get_name()
log += '_log.txt'
source = os.path.join(os.getcwd(), log)
destination = os.path.join(output_dir, 'log%i.txt' % self.interval)
move(source, destination)
示例2: simulate_multiple_fmus
def simulate_multiple_fmus():
"""Simulate one CYMDIST FMU coupled to a GridDyn FMU.
"""
cymdist=load_fmu("CYMDIST.fmu", log_level=7)
gridyn=load_fmu("GridDyn.fmu", log_level=7)
models = [cymdist, gridyn]
connections = [(gridyn, "VMAG_A", cymdist, "VMAG_A"),
(gridyn, "VMAG_B", cymdist, "VMAG_B"),
(gridyn, "VMAG_C", cymdist, "VMAG_C"),
(gridyn, "VANG_A", cymdist, "VANG_A"),
(gridyn, "VANG_B", cymdist, "VANG_B"),
(gridyn, "VANG_C", cymdist, "VANG_C"),
(cymdist, "KWA_800032440", gridyn, "KWA_800032440"),
(cymdist, "KWB_800032440", gridyn, "KWB_800032440"),
(cymdist, "KWC_800032440", gridyn, "KWC_800032440"),
(cymdist, "KVARA_800032440", gridyn, "KVARA_800032440"),
(cymdist, "KVARB_800032440", gridyn, "KVARB_800032440"),
(cymdist, "KVARC_800032440", gridyn, "KVARC_800032440"),]
coupled_simulation = Master (models, connections)
opts=coupled_simulation.simulate_options()
opts['step_size']=1.0
opts['logging']=True
res=coupled_simulation.simulate(options=opts,
start_time=0.0,
final_time=1.0)
示例3: simulate_cymdist_griddyn_fmus
def simulate_cymdist_griddyn_fmus():
"""Simulate one CYMDIST FMU coupled to a dummy griddyn FMU.
"""
# Parameters which will be arguments of the function
start_time = 0.0
stop_time = 0.1
step_size = 0.1
# Path to configuration file
path_config="Z:\\thierry\\proj\\cyder_repo\\NO_SHARING\\CYMDIST\\config.json"
cymdist_con_val_str = bytes(path_config, 'utf-8')
cymdist=load_fmu("../fmus/CYMDIST/CYMDIST.fmu", log_level=7)
griddyn=load_fmu("../fmus/griddyn/griddyn14bus.fmu", log_level=7)
models = [cymdist, griddyn]
connections = [(griddyn, "Bus11_VA", cymdist, "VMAG_A"),
(griddyn, "Bus11_VB", cymdist, "VMAG_B"),
(griddyn, "Bus11_VC", cymdist, "VMAG_C"),
(griddyn, "Bus11_VAngleA", cymdist, "VANG_A"),
(griddyn, "Bus11_VAngleB", cymdist, "VANG_B"),
(griddyn, "Bus11_VAngleC", cymdist, "VANG_C"),
(cymdist, "IA", griddyn, "Bus11_IA"),
(cymdist, "IB", griddyn, "Bus11_IB"),
(cymdist, "IC", griddyn, "Bus11_IC"),
(cymdist, "IAngleA", griddyn, "Bus11_IAngleA"),
(cymdist, "IAngleB", griddyn, "Bus11_IAngleB"),
(cymdist, "IAngleC", griddyn, "Bus11_IAngleC")]
coupled_simulation = Master (models, connections)
opts=coupled_simulation.simulate_options()
opts['step_size']=step_size
# Get the configuration file
cymdist_con_val_ref = cymdist.get_variable_valueref("_configurationFileName")
cymdist.set("_saveToFile", 0)
cymdist.set_string([cymdist_con_val_ref], [cymdist_con_val_str])
# Set the communication step size
cymdist.set("_communicationStepSize", step_size)
# Set the value of the multiplier
griddyn.set('multiplier', 3.0)
# Run simulation
start = datetime.now()
res=coupled_simulation.simulate(options=opts,
start_time=start_time,
final_time=stop_time)
end = datetime.now()
print('This is the voltage value (VMAG_A) in Cymdist' + str(res[cymdist]['VMAG_A']))
print('This is the current value (IA) in Cymdist' + str(res[cymdist]['IA']))
print('Ran a coupled CYMDIST/GridDyn simulation in ' +
str((end - start).total_seconds()) + ' seconds.')
示例4: test_ExtFuncShared
def test_ExtFuncShared(self):
"""
Test compiling a model with external functions in a shared library. Real, Integer, and Boolean arrays.
"""
fmu_name = compile_fmu(self.cpath, self.fpath, compiler_options={'variability_propagation':True})
model = load_fmu(fmu_name)
s_ceval = model.get('s')
res = model.simulate()
s_sim1 = res.final('s')
fmu_name = compile_fmu(self.cpath, self.fpath, compiler_options={'variability_propagation':False})
model = load_fmu(fmu_name)
res = model.simulate()
s_sim2 = res.final('s')
nose.tools.assert_equals(s_sim1, s_sim2)
示例5: run_demo
def run_demo(with_plots=True):
"""
An example on how to simulate a model using the ODE simulator.
"""
curr_dir = os.path.dirname(os.path.abspath(__file__));
file_name = os.path.join(curr_dir,'files','VDP.mop')
fmu_name = compile_fmu("JMExamples.VDP.VDP",
curr_dir+"/files/JMExamples.mo")
model = load_fmu(fmu_name)
opts = model.simulate_options()
opts["CVode_options"]["rtol"] = 1e-6
res = model.simulate(final_time=10, options=opts)
assert N.abs(res.final('x1') - 7.34186386e-01) < 1e-3
assert N.abs(res.final('x2') + 1.58202722) < 1e-3
x1 = res['x1']
x2 = res['x2']
if with_plots:
plt.figure()
plt.plot(x2, x1)
plt.legend(('x1(x2)'))
plt.show()
示例6: run_demo
def run_demo(with_plots=True, version="2.0"):
# Compile model
fmu_name = compile_fmu("IBPSA.Fluid.FixedResistances.Examples.PlugFlowPipe","C:\My_Libs\modelica-ibpsa\IBPSA")
fmu_name=("IBPSA_Fluid_FixedResistances_Examples_PlugFlowPipe.fmu")
#print("FMU compiled",fmu_name)
print(fmu_name)
# Load model
pipe = load_fmu(fmu_name)
print("FMU loaded", pipe)
res = pipe.simulate(final_time=100)
x1 = res['Tin.offset']
x2 = res['sou.m_flow']
t = res['time']
plt.figure(1)
plt.plot(t, x1, t, x2)
plt.legend(('Tin (K)','mdot (kg/s)'))
plt.title('Pipe')
plt.ylabel('y axis')
plt.xlabel('Time (s)')
plt.show()
示例7: furuta_dfo_cost
def furuta_dfo_cost(x):
# We need to scale the inputs x down since they are scaled up
# versions of the parameters (x = scalefactor*[param1 param2])
armFrictionCoefficient = x[0]/1e3
pendulumFrictionCoefficient = x[1]/1e3
model = load_fmu(os.path.join(curr_dir, '..', 'examples', 'files', 'FMUs', 'Furuta.fmu'))
# Set new parameter values into the model
model.set('armFriction', armFrictionCoefficient)
model.set('pendulumFriction', pendulumFrictionCoefficient)
# Create options object and set verbosity to zero to disable printouts
opts = model.simulate_options()
opts['CVode_options']['verbosity'] = 0
# Simulate model response with new parameter values
res = model.simulate(start_time=0., final_time=40, options=opts)
# Load simulation result
phi_sim = res['armJoint.phi']
theta_sim = res['pendulumJoint.phi']
t_sim = res['time']
# Evaluate the objective function
y_meas = N.vstack((phi_meas, theta_meas))
y_sim = N.vstack((phi_sim, theta_sim))
obj = dfo.quad_err(t_meas, y_meas, t_sim, y_sim)
return obj
示例8: test_inlined_switches
def test_inlined_switches(self):
""" Test a model that need in-lined switches to initialize. """
path = os.path.join(get_files_path(), 'Modelica', 'event_init.mo')
fmu_name = compile_fmu('Init', path)
model = load_fmu(fmu_name)
model.initialize()
assert N.abs(model.get("x") - (-2.15298995)) < 1e-3
示例9: run_demo
def run_demo(with_plots=True):
curr_dir = os.path.dirname(os.path.abspath(__file__));
class_name = 'ExtFunctions.transposeSquareMatrix'
mofile = os.path.join(curr_dir, 'files', 'ExtFunctions.mo')
# Compile and load model
fmu_name = compile_fmu(class_name, mofile)
model = load_fmu(fmu_name)
# Simulate
res = model.simulate()
# Get result data
b1_1 = res['b_out[1,1]']
b1_2 = res['b_out[1,2]']
b2_1 = res['b_out[2,1]']
b2_2 = res['b_out[2,2]']
t = res['time']
assert N.abs(res.final('b_out[1,1]') - 1) < 1e-6
assert N.abs(res.final('b_out[1,2]') - 3) < 1e-6
assert N.abs(res.final('b_out[2,1]') - 2) < 1e-6
assert N.abs(res.final('b_out[2,2]') - 4) < 1e-6
if with_plots:
fig = p.figure()
p.clf()
p.plot(t, b1_1, label='b1_1')
p.plot(t, b1_2, label='b1_2')
p.plot(t, b2_1, label='b2_1')
p.plot(t, b2_2, label='b2_2')
p.legend()
p.grid()
p.show()
示例10: run_demo
def run_demo(with_plots=True):
"""
This example shows how to simulate a system that contains switches. The
example model is simple in the sense that no reinitialization of the
variables is needed at the event points.
"""
curr_dir = os.path.dirname(os.path.abspath(__file__));
class_name = 'IfExpExamples.IfExpExample2'
mofile = curr_dir+'/files/IfExpExamples.mo'
fmu_name = compile_fmu(class_name, mofile)
# Load the dynamic library and XML data
model = load_fmu(fmu_name)
# Simulate
res = model.simulate(final_time=5.0)
# Get results
x = res['x']
u = res['u']
t = res['time']
assert N.abs(res.final('x') - 3.5297217) < 1e-3
assert N.abs(res.final('u') - (-0.2836621)) < 1e-3
if with_plots:
fig = p.figure()
p.plot(t, x, t, u)
p.legend(('x','u'))
p.show()
示例11: simulate_single_griddyn_fmu
def simulate_single_griddyn_fmu():
"""Simulate one griddyn FMU.
"""
# Parameters which will be arguments of the function
start_time = 0.0
stop_time = 0.1
griddyn=load_fmu("../../../../NO_SHARING/griddyn/Test/griddyn.fmu", log_level=7)
# Set the inputs
opts=griddyn.simulate_options()
opts['ncp']=1.0
# Set the model name reference to be completed in Python API
griddyn.set("power", 10)
# Run simulation
start = datetime.now()
res=griddyn.simulate(start_time=start_time,
final_time=stop_time,
options=opts)
end = datetime.now()
print('This is the time value ' + str(res['time']))
print('This is the load value ' + str(res['load']))
print('Ran a single GridDyn simulation in ' +
str((end - start).total_seconds()) + ' seconds.')
示例12: run_demo
def run_demo(with_plots=True):
"""
Demonstrates how to use an JMODELICA generated FMU for sensitivity
calculations.
"""
curr_dir = os.path.dirname(os.path.abspath(__file__));
fmu_name = compile_fmu("Robertson", curr_dir+"/files/Robertson.mo")
model = load_fmu(fmu_name)
# Get and set the options
opts = model.simulate_options()
opts['sensitivities'] = ["p1","p2","p3"]
opts['ncp'] = 400
#Simulate
res = model.simulate(final_time=4, options=opts)
dy1dp1 = res['dy1/dp1']
dy2dp1 = res['dy2/dp1']
time = res['time']
nose.tools.assert_almost_equal(dy1dp1[40], -0.35590, 3)
nose.tools.assert_almost_equal(dy2dp1[40], 3.9026e-04, 6)
if with_plots:
plt.plot(time, dy1dp1, time, dy2dp1)
plt.legend(('dy1/dp1', 'dy2/dp1'))
plt.show()
示例13: f1
def f1(x):
model = load_fmu(fmu_name)
# We need to scale the inputs x down since they are scaled up
# versions of a1 and a2 (x = scalefactor*[a1 a2])
a1 = x[0]/1e6
a2 = x[1]/1e6
# Set new values for a1 and a2 into the model
model.set('qt.a1',a1)
model.set('qt.a2',a2)
# Create options object and set verbosity to zero to disable printouts
opts = model.simulate_options()
opts['CVode_options']['verbosity'] = 0
# Simulate model response with new parameters a1 and a2
res = model.simulate(input=(['u1','u2'],u),start_time=0.,final_time=60,options=opts)
# Load simulation result
x1_sim = res['qt.x1']
x2_sim = res['qt.x2']
t_sim = res['time']
# Evaluate the objective function
y_meas = N.vstack((y1_meas,y2_meas))
y_sim = N.vstack((x1_sim,x2_sim))
obj = dfo.quad_err(t_meas,y_meas,t_sim,y_sim)
return obj
示例14: run_demo
def run_demo(with_plots=True):
curr_dir = os.path.dirname(os.path.abspath(__file__));
class_name = 'ExtFunctions.addTwo'
mofile = os.path.join(curr_dir, 'files', 'ExtFunctions.mo')
# Compile and load model
fmu_name = compile_fmu(class_name, mofile)
model = load_fmu(fmu_name)
# Simulate
res = model.simulate()
# Load result data
sim_a = res['a']
sim_b = res['b']
sim_c = res['c']
t = res['time']
assert N.abs(res.final('a') - 1) < 1e-6
assert N.abs(res.final('b') - 2) < 1e-6
assert N.abs(res.final('c') - 3) < 1e-6
if with_plots:
fig = p.figure()
p.clf()
p.subplot(3,1,1)
p.plot(t, sim_a)
p.subplot(3,1,2)
p.plot(t, sim_b)
p.subplot(3,1,3)
p.plot(t, sim_c)
p.show()
示例15: run_demo
def run_demo(with_plots=True):
"""
An example on how to simulate a model using the DAE simulator. The result
can be compared with that of sim_rlc.py which has solved the same problem
using dymola. Also writes information to a file.
"""
curr_dir = os.path.dirname(os.path.abspath(__file__));
class_name = 'RLC_Circuit'
mofile = curr_dir+'/files/RLC_Circuit.mo'
fmu_name = compile_fmu(class_name, mofile)
rlc = load_fmu(fmu_name)
res = rlc.simulate(final_time=30)
sine_y = res['sine.y']
resistor_v = res['resistor.v']
inductor1_i = res['inductor1.i']
t = res['time']
assert N.abs(res.final('resistor.v') - 0.159255008028) < 1e-3
if with_plots:
fig = p.figure()
p.plot(t, sine_y, t, resistor_v, t, inductor1_i)
p.legend(('sine.y','resistor.v','inductor1.i'))
p.show()