Skip to content

Commit

Permalink
Cleanup related to older numpy/scipy versions
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterMeisrimelModelon committed Jan 26, 2024
1 parent 1f012b3 commit 73a7417
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 38 deletions.
19 changes: 3 additions & 16 deletions src/pyfmi/fmi_coupled.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ cimport numpy as np
import scipy.optimize as sopt
import scipy

try:
from numpy.lib import NumpyVersion
USE_ROOT = NumpyVersion(scipy.version.version) >= "0.11.0"
except ImportError: #Numpy version is < 1.9.0 so assume scipy version is the same
USE_ROOT = False

def init_f_block(u, coupled, block):

i = 0
Expand Down Expand Up @@ -1986,12 +1980,8 @@ cdef class CoupledFMUModelME2(CoupledFMUModelBase):
u = np.array(u).ravel()

success = False
if USE_ROOT:
res = sopt.root(init_f_block, u, args=(self,block))
success = res["success"]
else:
[res, info, ier, msg] = sopt.fsolve(init_f_block, u, args=(self,block), full_output=True)
success = True if ier == 1 else False
res = sopt.root(init_f_block, u, args=(self,block))
success = res["success"]

if not success:
raise fmi.FMUException("Failed to converge the block.")
Expand All @@ -2000,10 +1990,7 @@ cdef class CoupledFMUModelME2(CoupledFMUModelBase):
for model in block["outputs"].keys():
self.get_specific_connection_outputs(model, block["outputs_mask"][model], y)
if USE_ROOT:
res = sopt.root(init_f_block, y[block["global_outputs_mask"]], args=(self,block))
else:
res = sopt.fsolve(init_f_block, y[block["global_outputs_mask"]], args=(self,block))
res = sopt.root(init_f_block, y[block["global_outputs_mask"]], args=(self,block))
if not res["success"]:
print res
raise Exception("Failed to converge the initialization system.")
Expand Down
26 changes: 4 additions & 22 deletions src/pyfmi/master.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ IF WITH_OPENMP:
DEF SERIAL = 0
DEF PARALLEL = 1

try:
from numpy.lib import NumpyVersion
USE_ROOT = NumpyVersion(scipy.version.version) >= "0.11.0"
except ImportError: #Numpy version is < 1.9.0 so assume scipy version is the same
USE_ROOT = False

cdef reset_models(list models):
for model in models:
model.reset()
Expand Down Expand Up @@ -993,10 +987,7 @@ cdef class Master:
JM_FMUS = False
break
if JM_FMUS:
if USE_ROOT:
res = sopt.root(init_f, y, args=(self))
else:
res = sopt.fsolve(init_f, y, args=(self))
res = sopt.root(init_f, y, args=(self))
if not res["success"]:
print(res)
raise fmi.FMUException("Failed to converge the output system.")
Expand Down Expand Up @@ -1125,10 +1116,7 @@ cdef class Master:
for model in block["outputs"].keys():
self.get_specific_connection_outputs(model, block["outputs_mask"][model], y)

if USE_ROOT:
res = sopt.root(init_f_block, y[block["global_outputs_mask"]], args=(self,block))
else:
res = sopt.fsolve(init_f_block, y[block["global_outputs_mask"]], args=(self,block))
res = sopt.root(init_f_block, y[block["global_outputs_mask"]], args=(self,block))
if not res["success"]:
print(res)
raise fmi.FMUException("Failed to converge the initialization system.")
Expand All @@ -1150,15 +1138,9 @@ cdef class Master:

if self.algebraic_loops: #If there is an algebraic loop, solve the resulting system
if self.support_directional_derivatives:
if USE_ROOT:
res = sopt.root(init_f, self.get_connection_outputs(), args=(self), jac=init_jac)
else:
res = sopt.fsolve(init_f, self.get_connection_outputs(), args=(self), jac=init_jac)
res = sopt.root(init_f, self.get_connection_outputs(), args=(self), jac=init_jac)
else:
if USE_ROOT:
res = sopt.root(init_f, self.get_connection_outputs(), args=(self))
else:
res = sopt.fsolve(init_f, self.get_connection_outputs(), args=(self))
res = sopt.root(init_f, self.get_connection_outputs(), args=(self))
if not res["success"]:
print(res)
raise fmi.FMUException("Failed to converge the initialization system.")
Expand Down

0 comments on commit 73a7417

Please sign in to comment.