# ____________________________________________________________________________________
#
# Pyomo: Python Optimization Modeling Objects
# Copyright (c) 2008-2026 National Technology and Engineering Solutions of Sandia, LLC
# Under the terms of Contract DE-NA0003525 with National Technology and Engineering
# Solutions of Sandia, LLC, the U.S. Government retains certain rights in this
# software. This software is distributed under the 3-clause BSD License.
# ____________________________________________________________________________________
import logging
from pyomo.common.collections import ComponentSet
from pyomo.core.base import Transformation, TransformationFactory
from pyomo.core import Var, Expression, Objective
from pyomo.dae import ContinuousSet, DerivativeVar, Integral
from pyomo.dae.misc import generate_finite_elements
from pyomo.dae.misc import expand_components
from pyomo.dae.misc import create_partial_expression
from pyomo.dae.misc import add_discretization_equations
from pyomo.dae.misc import block_fully_discretized
from pyomo.dae.diffvar import DAE_Error
from pyomo.common.config import ConfigBlock, ConfigValue, PositiveInt, In
logger = logging.getLogger('pyomo.dae')
def _central_transform(v, s):
"""
Applies the Central Difference formula of order O(h^2) for first
derivatives
"""
def _ctr_fun(i):
tmp = list(s)
idx = s.ord(i) - 1
if idx == 0: # Needed since '-1' is considered a valid index in Python
raise IndexError("list index out of range")
return 1 / (tmp[idx + 1] - tmp[idx - 1]) * (v(tmp[idx + 1]) - v(tmp[idx - 1]))
return _ctr_fun
def _central_transform_order2(v, s):
"""
Applies the Central Difference formula of order O(h^2) for second
derivatives
"""
def _ctr_fun2(i):
tmp = list(s)
idx = s.ord(i) - 1
if idx == 0: # Needed since '-1' is considered a valid index in Python
raise IndexError("list index out of range")
return (
1
/ ((tmp[idx + 1] - tmp[idx]) * (tmp[idx] - tmp[idx - 1]))
* (v(tmp[idx + 1]) - 2 * v(tmp[idx]) + v(tmp[idx - 1]))
)
return _ctr_fun2
def _forward_transform(v, s):
"""
Applies the Forward Difference formula of order O(h) for first derivatives
"""
def _fwd_fun(i):
tmp = list(s)
idx = s.ord(i) - 1
return 1 / (tmp[idx + 1] - tmp[idx]) * (v(tmp[idx + 1]) - v(tmp[idx]))
return _fwd_fun
def _forward_transform_order2(v, s):
"""
Applies the Forward Difference formula of order O(h) for second derivatives
"""
def _fwd_fun(i):
tmp = list(s)
idx = s.ord(i) - 1
return (
1
/ ((tmp[idx + 2] - tmp[idx + 1]) * (tmp[idx + 1] - tmp[idx]))
* (v(tmp[idx + 2]) - 2 * v(tmp[idx + 1]) + v(tmp[idx]))
)
return _fwd_fun
def _backward_transform(v, s):
"""
Applies the Backward Difference formula of order O(h) for first derivatives
"""
def _bwd_fun(i):
tmp = list(s)
idx = s.ord(i) - 1
if idx == 0: # Needed since '-1' is considered a valid index in Python
raise IndexError("list index out of range")
return 1 / (tmp[idx] - tmp[idx - 1]) * (v(tmp[idx]) - v(tmp[idx - 1]))
return _bwd_fun
def _backward_transform_order2(v, s):
"""
Applies the Backward Difference formula of order O(h) for second
derivatives
"""
def _bwd_fun(i):
tmp = list(s)
idx = s.ord(i) - 1
# This check is needed since '-1' is considered a valid index in Python
if idx == 0 or idx == 1:
raise IndexError("list index out of range")
return (
1
/ ((tmp[idx - 1] - tmp[idx - 2]) * (tmp[idx] - tmp[idx - 1]))
* (v(tmp[idx]) - 2 * v(tmp[idx - 1]) + v(tmp[idx - 2]))
)
return _bwd_fun