Open Access
This article is

- freely available
- re-usable

*Appl. Sci.*
**2016**,
*6*(10),
273;
doi:10.3390/app6100273

Article

Passive Guaranteed Simulation of Analog Audio Circuits: A Port-Hamiltonian Approach

Project-team S3 (Sound Signals and Systems) and Analysis/Synthesis team, Laboratory of Sciences and Technologies of Music and Sound (UMR 9912), IRCAM-CNRS-UPMC, 1 Place Igor Stravinsky, Paris 75004, France

*

Correspondence: Tel.: +33-1-44-78-13-14

^{†}

The PhD thesis of A. Falaize is funded by UPMC, ED 130 (ÉDITE), Paris.

Academic Editor:
Vesa Valimaki

Received: 25 April 2016 / Accepted: 13 September 2016 / Published: 24 September 2016

## Abstract

**:**

We present a method that generates passive-guaranteed stable simulations of analog audio circuits from electronic schematics for real-time issues. On one hand, this method is based on a continuous-time power-balanced state-space representation structured into its energy-storing parts, dissipative parts, and external sources. On the other hand, a numerical scheme is especially designed to preserve this structure and the power balance. These state-space structures define the class of port-Hamiltonian systems. The derivation of this structured system associated with the electronic circuit is achieved by an automated analysis of the interconnection network combined with a dictionary of models for each elementary component. The numerical scheme is based on the combination of finite differences applied on the state (with respect to the time variable) and on the total energy (with respect to the state). This combination provides a discrete-time version of the power balance. This set of algorithms is valid for both the linear and nonlinear case. Finally, three applications of increasing complexities are given: a diode clipper, a common-emitter bipolar-junction transistor amplifier, and a wah pedal. The results are compared to offline simulations obtained from a popular circuit simulator.

Keywords:

simulation; analog circuits; network modeling; passive system## 1. Introduction

The characteristic input-to-output behavior of analog audio circuits (timbre, transitory) rests on the possibly highly nonlinear components appearing in such systems. These components make the stability of the simulations difficult to guarantee. The motivation of this work stems from the following observations:

- Analog circuits combine energy-storing components, dissipative components, and sources.
- Storage components do not produce energy, and dissipative components decrease it.

In this sense, analog circuits can be considered as passive systems with external power supply. We shall exploit this passivity property by transposing it to the digital domain, ensuring the stability of the simulations (see [1,2,3]).

The available approaches for the automated derivation of physical modeling and numerical simulation of audio circuits can be divided in two classes [4]: wave scattering methods (WS) and Kirchhoff’s variables methods (KV). Mixed WS/KV methods have also been proposed in references [5,6]. The well-established wave-digital filter (WDF) formalism [7] belongs to the class of WS methods. For linear circuits, it provides a computationally realizable system of equations: First, by defining parametric wave variables for each elementary component and multiports (serial and parallel); Second, by discretizing the corresponding constitutive laws with the bilinear transform; and Third, by choosing the wave’s parameters so as to reduce the computational complexity and to avoid instantaneous feedback loops. An extension to nonlinear circuits has been considered in [8,9] and applied to, for example, the real-time simulation of vacuum-tube guitar amplifiers in [10,11]. WDF ensures the passivity of the resulting digital system [7,12], including systems with scalar nonlinearity [13]. However, the passivity property of WDF structures is not ensured for circuits with more than one nonlinear element (e.g., [14] §3.2 and [15] §6).

The class of KV methods for audio circuits encompasses nonlinear state-space representations [16]. Several modeling techniques are available to derive the discrete-time state-space model, either from the global time-continuous model (e.g., [17]) or from the interconnection of discretized elementary components (e.g., [18]). The resulting set of nonlinear implicit equations solved at each sample can be structured so as to obtain a computationally realizable system by applying the K-method introduced in [19] with developments in [18]. However, this structure does not encode the passivity of the original circuit naturally, and it must be investigated on a case-by-case basis [20].

In this paper, we consider the port-Hamiltonian systems (PHS) approach, introduced in the 1990’s [21,22,23]. PHS are extensions of classical Hamiltonian systems [24], specifically defined to address open dynamical systems made of energy storage components, dissipative components, and some connection ports through which energy can transit. This approach leads to a state-space representation of physical systems structured according to energy flow, thus encoding the passivity property, even for nonlinear cases. This class of physical systems encompasses not only electrical circuits, but also multi-domain systems, such as loudspeakers, which involves electrical, magnetic, mechanical, acoustical, and thermodynamical phenomena.

The port-Hamiltonian structure is derived by applying the Kirchhoff’s laws to a given schematic, similarly to other existing approaches (e.g., WDF and K-method). Here, the advantage of the PHS formulation is the direct encoding of the underlying passive structure. This passivity property is transposed to the discrete-time domain by appropriate numerical methods, so as to ensure the numerical stability. For linear storage components (inductors and capacitors), the combination of the PHS structure with any of the trapezoidal rule or the mid-point rule yields the same numerical scheme that preserves the passivity in discrete-time. For nonlinear storage components, we propose the use of the discrete gradient method [25] combined with the PHS structure to achieve this goal. This result is compared to the aforementioned methods. As a second result, we provide an automated method that derives the PHS structure from a given analog circuit, based on an especially designed graph analysis.

This paper is organized as follows. Section 2 presents the class of the port-Hamiltonian systems; Section 3 is devoted to the automated derivation of algebraic-differential equations in the continuous time domain from the electronic schematics; Section 4 presents the numerical scheme which provides a discrete-time version of the power balance; Then, applications are presented in Section 5 and results are compared to LT-Spice simulations, before conclusions and perspectives.

## 2. Port-Hamiltonian Systems

First and foremost, we provide an introduction to the port-Hamiltonian systems (PHS) formalism. It is shown how this structure guarantees the passivity of the model in continuous time. Second, for the sake of intuition, we give an introductory example.

#### 2.1. Formalism and Property

Denote $\mathtt{E}\left(t\right)\ge 0$ the energy stored in an open physical system (an electronic circuit). If the system is conservative, its time variation $\frac{\mathtt{d}\mathtt{E}}{\mathtt{d}t}\left(t\right)$ reduces to the power $\mathtt{S}\left(t\right)$ received from the sources through the external ports. If the system includes dissipative phenomena, the power $\mathtt{D}\left(t\right)\ge 0$ is dissipated, and the evolution of energy is governed by the following power balance:

$$\frac{\mathtt{d}\mathtt{E}}{\mathtt{d}t}\left(t\right)=-\mathtt{D}\left(t\right)+\mathtt{S}\left(t\right).$$

The port-Hamiltonian approach is used to decompose such open physical systems in (i) a set of components that are combined according to (ii) a conservative interconnection network. These two ingredients are detailed below in the case of electronic circuits.

#### 2.1.1. Components

Electronic circuit components are sorted as (or can be a combination of):

- ${n}_{S}$
- internal components that store energy $\mathtt{E}\ge 0$ (capacitors or inductors),
- ${n}_{D}$
- internal components that dissipate power $\mathtt{D}\ge 0$ (resistors, diodes, transistors, etc.),
- ${n}_{P}$
- external ports that convey power $\mathtt{S}\phantom{\rule{0.277778em}{0ex}}(\in \mathbb{R})$ from sources (voltage or current generators) or any external system (active, dissipative, or mixed).

The behavior of each component is described by a relation between two power variables: the current i and the voltage v, defined in receiver convention (the received power is $\mathtt{P}=v\xb7i$).

The energy ${\mathtt{E}}_{s}$ stored in storage component $s\in [1\cdots {n}_{S}]$ is expressed as a storage function ${h}_{s}$ of an appropriate state ${x}_{s}$: ${\mathtt{E}}_{s}\left(t\right)={h}_{s}\left({x}_{s}\left(t\right)\right)\ge 0$. Typically, for a linear capacitor with capacitance C, the state can be the charge $x=q$ and the positive definite function is $h\left(q\right)={q}^{2}/\left(2C\right)$. Storage power variables $({v}_{s},{i}_{s})$ are related to the variation of the state $\frac{\mathtt{d}{x}_{s}}{\mathtt{d}t}$ and the gradient of the storage function ${h}_{s}^{\prime}\left({x}_{s}\right)$, the product of which is precisely the received power: ${v}_{s}\xb7{i}_{s}=\frac{\mathtt{d}{\mathtt{E}}_{s}}{\mathtt{d}t}={h}_{s}^{\prime}\xb7\frac{\mathtt{d}{x}_{s}}{\mathtt{d}t}$. For the capacitance, these constitutive laws are $i=\frac{\mathtt{d}q}{\mathtt{d}t}=\frac{\mathtt{d}x}{\mathtt{d}t}$ and $v=q/C={h}^{\prime}$. Note that these definitions apply equally for non-quadratic storage functions $h\left(x\right)\ge 0$ for which ${h}^{\prime \prime}\left(x\right)$ is not constant.

The power ${\mathtt{D}}_{d}$ instantaneously dissipated by the dissipative component $d\in [1\cdots {n}_{D}]$ is expressed with respect to an appropriate dissipation variable ${w}_{d}$: ${\mathtt{D}}_{d}\left(t\right)\equiv {\mathtt{D}}_{d}\left({w}_{d}\left(t\right)\right)\ge 0$. Typically, for a linear resistance R, w can be a current $w=i$ and $\mathtt{D}\left(i\right)=R\xb7{i}^{2}$. As for storage components, a mapping of the dissipative power variables $({v}_{d},{i}_{d})$ is provided, based on the factorization ${\mathtt{D}}_{d}\left({w}_{d}\right)={w}_{d}\xb7{z}_{d}\left({w}_{d}\right)$, introducing a dissipation function ${z}_{d}$. For the resistance, $i=w$ and $v=R\xb7i=z\left(w\right)$.

The power instantaneously provided to the system through external port $p\in [1\cdots {n}_{P}]$ is ${\mathtt{S}}_{p}\left(t\right)$, and we arrange the source variables $({v}_{p},{i}_{p})$ in two vectors: one is considered as an input ${u}_{p}$, and the other as the associated output ${y}_{p}$, so that the power received from sources on port p is ${\mathtt{S}}_{p}={y}_{p}\xb7{u}_{p}=-{v}_{p}\xb7{i}_{p}$ (receiver convention, with ${v}_{p}\xb7{i}_{p}$ the power received by the sources).

#### 2.1.2. Conservative Interconnection

The interconnection of the components is achieved by relating all the voltages and currents through the application of the Kirchhoff’s laws to the interconnection network (schematic). This defines a conservative interconnection, according to Tellegen’s theorem recalled below (see also [26] and [27] §9.4).

**Theorem**

**1 (Tellegen).**

Consider an electronic circuit made of N edges defined in same convention (here receiver), with individual voltages $v={({v}_{1},\cdots ,{v}_{N})}^{\u22ba}$ and currents ${i}_{n}={({i}_{1},\cdots ,{i}_{N})}^{\u22ba}$ which comply with the Kirchhoff’s laws. Then

$${\mathbf{v}}^{\u22ba}\xb7\mathbf{i}=0.$$

A direct consequence of (2) is that no power is created nor lost in the structure: ${\mathbf{v}}^{\u22ba}\xb7\mathbf{i}={\sum}_{i=1}^{N}{\mathtt{P}}_{n}=0$, with ${\mathtt{P}}_{n}={v}_{n}\xb7{i}_{n}$ the power received by edge n, thus defining a conservative interconnection (Tellegen’s theorem is a special case of a more general interconnection structure, namely, the Dirac structure (see [23] §2.1.2 for details)). Now, denote $({\mathbf{v}}_{\mathbf{s}},{\mathbf{i}}_{\mathbf{s}})$, $({\mathbf{v}}_{\mathbf{d}},{\mathbf{i}}_{\mathbf{d}})$, and $({\mathbf{v}}_{\mathbf{p}},{\mathbf{i}}_{\mathbf{p}})$ the sets of all the power variables associated with storage components, dissipative components, and sources (respectively), and $\mathbf{v}={({\mathbf{v}}_{\mathbf{s}}^{\u22ba},{\mathbf{v}}_{\mathbf{d}}^{\u22ba},{\mathbf{v}}_{\mathbf{p}}^{\u22ba})}^{\u22ba}$, $\mathbf{i}={({\mathbf{i}}_{\mathbf{s}}^{\u22ba},{\mathbf{i}}_{\mathbf{d}}^{\u22ba},{\mathbf{i}}_{\mathbf{p}}^{\u22ba})}^{\u22ba}$ the vectors of all the power variables. Then, Tellegen’s theorem restores the power balance (1) with
where $\nabla \mathcal{H}:{\mathbb{R}}^{{n}_{S}}\to {\mathbb{R}}^{{n}_{S}}$ denotes the gradient of the total energy $\mathtt{E}=\mathcal{H}\left(\mathbf{x}\right)={\sum}_{s=1}^{{n}_{S}}{h}_{s}\left({x}_{s}\right)$ with respect to (w.r.t.) the vector of the states ${\left[\mathbf{x}\right]}_{s}={x}_{s}$, and function $\mathbf{z}:{\mathbb{R}}^{{n}_{D}}\to {\mathbb{R}}^{{n}_{D}}$ denotes the collection of functions ${z}_{d}$ w.r.t. the vector $\mathbf{w}\in {\mathbb{R}}^{{n}_{D}}$ of ${\left[\mathbf{w}\right]}_{d}={w}_{d}$ so that $\mathbf{z}{\left(\mathbf{w}\right)}^{\u22ba}\xb7\mathbf{w}={\sum}_{d=1}^{{n}_{D}}{\mathtt{D}}_{d}\left({w}_{d}\right)$ is the total dissipated power.

$$\begin{array}{ccc}\hfill {\mathbf{v}}^{\u22ba}\xb7\mathbf{i}& =& {\mathbf{v}}_{\mathbf{s}}^{\u22ba}\xb7{\mathbf{i}}_{\mathbf{s}}+{\mathbf{v}}_{\mathbf{d}}^{\u22ba}\xb7{\mathbf{i}}_{\mathbf{d}}+{\mathbf{v}}_{\mathbf{p}}^{\u22ba}\xb7{\mathbf{i}}_{\mathbf{p}}\hfill \\ & =& \underset{\frac{\mathtt{d}\mathtt{E}}{\mathtt{d}t}}{\underbrace{{\nabla \mathcal{H}}^{\u22ba}\left(\mathbf{x}\right)\xb7\frac{\mathtt{d}\mathbf{x}}{\mathtt{d}t}}}+\underset{\mathtt{D}}{\underbrace{\mathbf{z}{\left(\mathbf{w}\right)}^{\u22ba}\xb7\mathbf{w}}}-\underset{\mathtt{S}}{\underbrace{{\mathbf{u}}^{\u22ba}\xb7\mathbf{y}}},\hfill \end{array}$$

The above description of storage components, dissipative components, and source, along with the conservative interconnection stated by the Kirchhoff’s laws, constitute the minimal definition of a port-Hamiltonian system (PHS) (see [23] §2.2). In this work, we focus on circuits that admit an explicit realization of PHS, for which the quantities $\mathbf{b}={({b}_{1},\cdots ,{b}_{N})}^{\u22ba}={(\frac{\mathtt{d}\mathbf{x}}{\mathtt{d}t},\mathbf{w},-\mathbf{y})}^{\u22ba}$ (with ${b}_{n}={v}_{n}$ or ${b}_{n}={i}_{n}$) can be expressed as linear combinations of the remaining N powers variables organized in the dual vector $\mathbf{a}={({a}_{1},\cdots ,{a}_{N})}^{\u22ba}={(\nabla \mathcal{H}\left(\mathbf{x}\right),\mathbf{z}\left(\mathbf{w}\right),\mathbf{u})}^{\u22ba}$ (with ${a}_{n}={i}_{n}$ if ${b}_{n}={v}_{n}$ or ${a}_{n}={v}_{n}$ if ${b}_{n}={i}_{n}$):

$$\mathbf{b}=\mathbf{J}\xb7\mathbf{a}.$$

Then, ${\mathbf{a}}^{\u22ba}\xb7\mathbf{b}={\mathbf{a}}^{\u22ba}\xb7\mathbf{J}\xb7\mathbf{a}=0$ from Tellegen’s theorem, so that the matrix $\mathbf{J}$ is necessarily skew-symmetric (${\mathbf{J}}^{\u22ba}=-\mathbf{J}$). More precisely, we consider the following algebraic-differential system of equations
where matrices ${\mathbf{J}}_{\mathbf{x}}$, ${\mathbf{J}}_{\mathbf{w}}$, ${\mathbf{J}}_{\mathbf{y}}$ are skew-symmetric. The significance of the structure matrices is the following:

$$\underset{\mathbf{b}}{\underbrace{\left(\begin{array}{c}\frac{\mathtt{d}\mathbf{x}}{\mathtt{d}t}\\ \mathbf{w}\\ -\mathbf{y}\end{array}\right)}}=\underset{\mathbf{J}}{\underbrace{\left(\begin{array}{ccc}\hfill {\mathbf{J}}_{\mathbf{x}}& \hfill -\mathbf{K}& \hfill -{\mathbf{G}}_{\mathbf{x}}\\ \hfill {\mathbf{K}}^{\u22ba}& \hfill {\mathbf{J}}_{\mathbf{w}}& \hfill -{\mathbf{G}}_{\mathbf{w}}\\ \hfill {{\mathbf{G}}_{\mathbf{x}}}^{\u22ba}& \hfill {{\mathbf{G}}_{\mathbf{w}}}^{\u22ba}& \hfill {\mathbf{J}}_{\mathbf{y}}\end{array}\right)}}\xb7\underset{\mathbf{a}}{\underbrace{\left(\begin{array}{c}\nabla \mathcal{H}\left(\mathbf{x}\right)\\ \mathbf{z}\left(\mathbf{w}\right)\\ \mathbf{u}\end{array}\right)}},$$

- ${\mathbf{J}}_{\mathbf{x}}\in {\mathbb{R}}^{{n}_{S}\times {n}_{S}}$
- expresses the conservative power exchanges between storage components (this corresponds to the so-called $\mathbf{J}$ matrix in classical Hamiltonian systems);
- ${\mathbf{J}}_{\mathbf{w}}\in {\mathbb{R}}^{{n}_{D}\times {n}_{D}}$
- expresses the conservative power exchanges between dissipative components;
- ${\mathbf{J}}_{\mathbf{y}}\in {\mathbb{R}}^{{n}_{P}\times {n}_{P}}$
- expresses the conservative power exchanges between ports (direct connections of inputs to outputs);
- $\mathbf{K}\in {\mathbb{R}}^{{n}_{S}\times {n}_{D}}$
- expresses the conservative power exchanges between the storage components and the dissipative components;
- ${\mathbf{G}}_{\mathbf{x}}\in {\mathbb{R}}^{{n}_{S}\times {n}_{P}}$
- expresses the conservative power exchanges between ports and storage components (input gain matrix);
- ${\mathbf{G}}_{\mathbf{w}}\in {\mathbb{R}}^{{n}_{D}\times {n}_{P}}$
- expresses the conservative power exchanges between ports and dissipative components (input gain matrix).

**Property**

**1 (Power Balance).**

The variation of the total energy $\mathtt{E}=\mathcal{H}\left(\mathbf{x}\right)$ of a system governed by (5) is given by (1), with $\mathtt{D}=\mathbf{z}{\left(\mathbf{w}\right)}^{\u22ba}\xb7\mathbf{w}\ge 0$ the total dissipated power, and $\mathtt{S}={\mathbf{u}}^{\u22ba}\xb7\mathbf{y}$ the total power incoming on external ports.

**Proof.**

We have ${\mathbf{a}}^{\u22ba}\xb7\mathbf{b}=\frac{\mathtt{d}\mathtt{E}}{\mathtt{d}t}+\mathtt{D}-\mathtt{S}$. Now ${\mathbf{a}}^{\u22ba}\xb7\mathbf{b}={\mathbf{a}}^{\u22ba}\xb7\mathbf{J}\xb7\mathbf{a}=0$ since $\mathbf{J}$ is skew-symmetric. ☐

**Remark**

**1 (Power variables).**

This work is devoted to the treatment of electronic circuits for which power variables are chosen as current and voltage. However, all the aforementioned definitions apply equally to multiphysical systems, provided an adapted set of power variables, generically denoted by flux (currents, velocities, magnetic flux variations) and efforts (voltages, forces, magnetomotive force), the product of which is a power (see [23] Table 1.1). This follows the bond-graph modeling approach [28,29], on which the PHS formalism is built (see [23] §1.6 and 2.1). The treatment of multiphysical audio systems in the PHS formalism can be found in [30] (electromechanical piano that includes mechanical, electrical, and magnetic phenomena) and [31] (§III.B) (modulated air flow for musical acoustics applications that includes mechanical and acoustical phenomena).

#### 2.2. Example

Consider the resistor-inductor-capacitor (RLC) circuit in Figure 1, with ${n}_{S}=2$, ${n}_{D}=1$, and ${n}_{P}=2$, described as follows. For the linear inductance L, the state and the positive definite function can be the magnetic flux ${x}_{1}=\varphi $ and ${h}_{1}\left(\varphi \right)={\varphi}^{2}/\left(2L\right)$, so that ${v}_{L}=d{h}_{1}/d{x}_{1}$ and ${i}_{L}=\frac{\mathtt{d}{x}_{1}}{\mathtt{d}t}$. For the capacitance and the resitance, quantities are defined with ${x}_{2}=q$ and $\mathbf{w}=\left[{i}_{R}\right]$. Port variables are arranged as input $\mathbf{u}={[{v}_{1},{v}_{2}]}^{\u22ba}$ and output $\mathbf{y}={[-{i}_{1},-{i}_{2}]}^{\u22ba}$ (edges receiver convention).

Applying Kirchhoff’s laws to this simple serial circuit yields

$$\left(\begin{array}{c}{v}_{L}\\ {i}_{C}\\ {i}_{R}\\ {i}_{1}\\ {i}_{2}\end{array}\right)=\left(\begin{array}{ccccc}0& -1& -1& -1& +1\\ +1& 0& 0& 0& 0\\ +1& 0& 0& 0& 0\\ \hfill +1& 0& 0& \hfill 0& \hfill 0\\ \hfill -1& 0& 0& \hfill 0& \hfill 0\end{array}\right)\xb7\left(\begin{array}{c}{i}_{L}\\ {v}_{C}\\ {v}_{R}\\ {v}_{1}\\ {v}_{2}\end{array}\right).$$

From the constitutive laws of components, this equation restores the form (5) exactly, block by block. It provides the algebraic-differential equations that govern the system with input $\mathbf{u}$ and output $\mathbf{y}$.

This work aims at simulating such passive systems by firstly generating Equation (5) associated to a given circuit, and secondly by deriving its numerical version so that a discrete power balance is satisfied.

**Remark**

**2 (Reduction).**

The system (5) can be reduced by decomposing function $\mathbf{z}$ into its linear and nonlinear parts. See Appendix A for details.

## 3. Generation of Equations

This section provides a method to translate the description of a circuit (components and interconnections) from a netlist in a Spice-style [32] to the Formulation (5). Compared to standard methods that express all the currents as a function of all the voltages (see [18,27,32]), Formulation (5) expresses vector $\mathbf{b}$ of selected power variables (voltage or current) as a function of the vector $\mathbf{a}$ of complementary power variables (if ${\left[\mathbf{b}\right]}_{n}$ is a voltage of a branch, ${\left[\mathbf{a}\right]}_{n}$ is the associated current with receiver convention). To derive the matrix $\mathbf{J}$ that relates the voltages and the currents arranged in vectors $\mathbf{a}$ and $\mathbf{b}$ according to Kirchhoff’s laws (as in example §2.2), we propose a two-step method:

**Step 1**:- from a netlist ($\mathcal{L}$) to a graph ($\mathcal{G}$) that represents the Kirchhoff’s laws for a chosen orientation (convention);
**Step 2**:

Step 1 is standard. The presentation focuses on convention choices and details our procedure. In step 2, we propose an algorithm that analyzes if Formulation (2) is available (that is, the circuit is realizable into the PHS formalism) and delivers the matrix $\mathbf{J}$ in this case. Otherwise, the circuit corresponds to an implicit formulation that is not addressed in this paper. In practice, such cases appear for serial(/parallel) connection of voltage(/current)-controlled components. In this case, port-Hamiltonian Formulation (5) requires extension (see [22,23]).

#### 3.1. Graph Encoding

#### 3.1.1. Netlists

Each line of a netlist describes an element of the corresponding schematic, with: identification label, list of connection nodes, type of element, and list of parameters. We divide netlists into two blocks: internal components (dissipative and storage) and external ports (supplies and ground). In the first block (components), each line includes a reference to the appropriate entry in the dictionary and a list of the parameters for the corresponding model. Each line of the second block (external ports) provides the label of the externalized node, the type of supply (voltage or current), and the symbol∼if the supply is modulated (typically, the input signal), or a value if constant (typically, a battery).

As an example, the netlist corresponding to the circuit in Figure 2 is given in Table 1. Here, the components are given lines ${\ell}_{1}$ to ${\ell}_{3}$ (gray). The first two lines describe dipoles: a linear capacitor between ${N}_{1}$ and ${N}_{2}$ with label $C1$ and capacitance value $20{e}^{-9}$ F; and a resistor between ${N}_{3}$ and ${N}_{4}$ with label $R1$ and resistance value $1.5{e}^{3}$ Ω. The third line describes a npn bipolar-junction transistor. From the dictionary (Appendix B), the base terminal appears to be connected to the circuit’s node ${N}_{2}$, the emitter terminal to ${N}_{3}$, and the collector terminal to ${N}_{5}$. For this component, the list of parameters is: forward and reverse common emitter current gain, reverse saturation current, and thermal voltage. External ports are given lines ${\ell}_{4}$ to ${\ell}_{7}$. Line ${\ell}_{4}$ describes a constant 9 V voltage supply (labeled $Vcc$) on the circuit’s node ${N}_{4}$. ${\ell}_{5}$ describes a modulated voltage supply (here considered as the input signal) on ${N}_{4}$. ${\ell}_{6}$ describes a constant 0 A current supply on node ${N}_{3}$; this permits the recovery of the voltage on that node ${N}_{3}$ as an output to the circuit. ${\ell}_{7}$ describes the connection of the circuit’s node ${N}_{5}$ to the ground.

#### 3.1.2. Graph

A graph $=\{\mathsf{N},\mathsf{B}\}$ is defined by two lists of nodes $\mathsf{N}$ (also called vertices) and branches $\mathsf{B}$ (also called edges), with $\mathsf{B}\subset {\mathsf{N}}^{2}$ (each element of $\mathsf{B}$ is an object defined on two elements of $\mathsf{N}$, see [33] for details). The dictionary (Appendix B) encodes the graph of each elementary component. The branches of such an elementary graph contain the constitutive laws of the corresponding component:

- Dipoles are made of two nodes and a single branch, defining a single couple of state x and storage function $h\left(x\right)$ (storage component), or dissipative variable w and scalar relation $z\left(w\right)$ (dissipative components).
- More generally, n-ports multipole are made of n nodes and at least $n-1$ branches, defining $n-1$ couples of variables and functions. Typically, the graph for the bipolar junction is made of two branches (base-emitter and base-collector).

The graph corresponding to a given circuit is derived from its netlist description in two steps:

- build the internal graph by connecting the elementary graph of the components from the first block of the netlist,
- introduce a reference node ${N}_{0}$ (or datum, see [27] §10) to define the external branches from the second block.

Typically, ${N}_{0}$ corresponds to the ground or any local electrostatic potential which does not impact the currents nor voltages. Then, $\mathsf{N}$ is built from the list of nodes appearing at least once in the netlist, plus the reference node $\mathsf{N}\triangleq [{N}_{0},{N}_{1},\cdots ,{N}_{{n}_{N}}]$. According to Section 2, the set of branches is organized as $\mathsf{B}=\{{\mathsf{B}}_{S},{\mathsf{B}}_{D},{\mathsf{B}}_{P}\}$, with ${\mathsf{B}}_{S}$ the ${n}_{S}$ energy storage branches, ${\mathsf{B}}_{D}$ the ${n}_{D}$ dissipative branches, and ${\mathsf{B}}_{P}$ the ${n}_{P}$ sources.

As an example, the construction of the graph in Figure 2 from its netlist 1 is as follows. Firstly, the internal graph is built. It is made of ${n}_{N}=5$ nodes $\{{\mathsf{N}}_{1},\cdots ,{\mathsf{N}}_{5}\}$ and four branches ${\mathsf{B}}_{S}=\left\{{C}_{1}\right\}$ and ${\mathsf{B}}_{D}=\{{R}_{1},{Q}_{1,bc},{Q}_{1,be}\}$. Secondly, we introduce the (virtual) reference node ${\mathsf{N}}_{0}$ to define the four branches corresponding to the external ports ${\mathsf{B}}_{P}=\{IN,Vcc,OUT,GRD\}$.

#### 3.1.3. Kirchhoff’s Laws on Graphs

We assign to each branch b both a voltage ${v}_{b}$ and a current ${i}_{b}$ in receiver convention, the direction of the branch indicating the direction of the current. Note that the power supplied to the system on port p is the power emitted by the port branch ${\mathtt{S}}_{p}={u}_{p}\xb7{y}_{p}=-{v}_{p}\xb7{i}_{p}$. For a circuit made of ${n}_{N}+1$ nodes and ${n}_{B}={n}_{S}+{n}_{D}+{n}_{P}$ branches, we define: the set of electrostatic potentials on the nodes $\mathbf{e}={({e}_{1}\cdots {e}_{{n}_{N}})}^{\u22ba}$, the set of voltages $\mathbf{v}={({v}_{1}\cdots {v}_{{n}_{B}})}^{\u22ba}$, and the set of currents $\mathbf{i}={({i}_{1}\cdots {i}_{{n}_{B}})}^{\u22ba}$. The orientation of an entire graph is encoded in its incidence matrix $\Gamma \in {\mathbb{R}}^{(+1)\times {n}_{B}}$, defined below [27] (§9).

$${[\Gamma ]}_{n,b}=\left\{\begin{array}{cc}-1& \mathrm{if}\mathrm{branch}b\mathrm{is}\mathrm{outgoing}\mathrm{node}n,\hfill \\ 1& \mathrm{if}\mathrm{branch}b\mathrm{is}\mathrm{ingoing}\mathrm{node}n,\hfill \\ 0& \mathrm{otherwise}.\hfill \end{array}\right.$$

As an example, the incidence matrix for the circuit described in Table 1 is given equation below (0 are replaced by dots). Notice the grey columns correspond to the components, and the other columns correspond to the external ports, or sources (as in Table 1 and Figure 2).

Since the reference potential ${e}_{0}$ does not influence the voltages nor the currents, it is not taken into account in the Kirchhoff’s laws, and we define the reduced incidence matrix $\widehat{\Gamma}\in {\mathbb{R}}^{{n}_{N}\times {n}_{B}}$ obtained by deleting the row corresponding to the datum ${\mathsf{N}}_{0}$ in Γ. This leads to the following matrix formulation of Kirchhoff’s Voltage Law (KVL) and Kirchhoff’s Current Law (KCL) [27] (§10), from which the structure (5) is derived.

$$\left\{\begin{array}{cccc}\hfill {\widehat{\Gamma}}^{\u22ba}\xb7\mathbf{e}& =& \mathbf{v},\hfill & \left(\mathrm{KVL}\right)\\ \hfill \widehat{\Gamma}\xb7\mathbf{i}& =& \mathrm{\U0001d7d8}.\hfill & \left(\mathrm{KCL}\right)\end{array}\right.$$

#### 3.2. Realizability Analysis

The PHS structure (5) relies on (i) an arrangement of currents $\mathbf{i}$ and voltages $\mathbf{v}$ in two vectors $\mathbf{a}$ and $\mathbf{b}$ and (ii) a set of linear relations encoded in the skew-symmetric matrix $\mathbf{J}$ that corresponds to the conservation laws (8) applied on (

**i**,**v**). For storage and sources components, step (i) is straightforward with the constraints given in Table 2. For dissipative components, this step is achieved by selecting each component as voltage-controlled or current-controlled in order to satisfy a criterion on the matrix description of the interconnection scheme. This realizability criterion is given in Section 3.2.1, assuming the control type of every edge is known. A method of choosing the control type of dissipative edges so as to satisfy the realizability criterion is addressed in Section 3.2.2. This leads to Algorithm 1, which solves (i) and (ii).#### 3.2.1. A Criterion for Realizability

In Section 3.1, the set of edges $\mathsf{B}$ has been partitioned based on the differentiation between internal edges (or component edges $\{{\mathsf{B}}_{S},{\mathsf{B}}_{D}\}$, grey) and external edges (or ports edges ${\mathsf{B}}_{P}$, white), in order to build the complete graph from the netlist. In this section, we are interested in the PHS Formulation (5) associated to a given complete graph. To that end, we leave out from here the differentiation between internal and external edges in order to focus on the differentiation between voltage-controlled and current-controlled edges.

Suppose the control type of every edge is known, and the set of edges is split according to $\mathsf{B}=\{{\mathsf{B}}_{1},{\mathsf{B}}_{2}\}$, with ${\mathsf{B}}_{1}$ the set of ${n}_{1}$ voltage-controlled edges and ${\mathsf{B}}_{2}$ the set of ${n}_{2}$ current-controlled edges (see Table 2). Correspondingly, the sets of power variables are split as $\mathbf{v}={({\mathbf{v}}_{1},{\mathbf{v}}_{2})}^{\u22ba}$ and $\mathbf{i}={({\mathbf{i}}_{1},{\mathbf{i}}_{2})}^{\u22ba}$, and we define $\tilde{\mathbf{a}}={\left({\mathbf{v}}_{1},{\mathbf{i}}_{2}\right)}^{\u22ba}$ and $\tilde{\mathbf{b}}={\left({\mathbf{i}}_{1},{\mathbf{v}}_{2}\right)}^{\u22ba}$. Since the reference potential ${e}_{0}$ defined on node ${\mathsf{N}}_{0}$ does not influence the voltages nor the currents, it is not considered in the sequel, and the incidence matrix splits as follows:

$$\Gamma =\left(\frac{{\gamma}_{0}}{\begin{array}{cc}{\gamma}_{1}& {\gamma}_{2}\end{array}}\right),\phantom{\rule{1.em}{0ex}}\mathrm{with}\phantom{\rule{0.277778em}{0ex}}{\gamma}_{0}\in {\mathbb{R}}^{1\times {n}_{B}},\phantom{\rule{0.277778em}{0ex}}{\gamma}_{1}\in {\mathbb{R}}^{{n}_{N}\times {n}_{1}},\phantom{\rule{0.277778em}{0ex}}{\gamma}_{2}\in {\mathbb{R}}^{{n}_{N}\times {n}_{2}}.$$

This leads to a rewrite of the Kirchhoff laws (8) as:

$$\begin{array}{ccc}\hfill {\left({\gamma}_{1},{\gamma}_{2}\right)}^{\u22ba}\mathbf{e}& =& \mathbf{v},\hfill \end{array}$$

$$\begin{array}{ccc}\hfill \left({\gamma}_{1},{\gamma}_{2}\right)\phantom{\rule{0.166667em}{0ex}}\mathbf{i}& =& 0.\hfill \end{array}$$

**Proposition**

**1 (Realizability).**

If ${\gamma}_{2}$ is invertible, then the port-Hamiltonian structure (5) provides a realization of the graph $=\{\mathsf{N},({\mathsf{B}}_{1},{\mathsf{B}}_{2})\}$.

**Proof.**

From the relation on the voltages in (9), we get ${\mathbf{v}}_{1}={\gamma}_{1}^{\u22ba}\phantom{\rule{0.166667em}{0ex}}\mathbf{e}$ and ${\mathbf{v}}_{2}={\gamma}_{2}^{\u22ba}\phantom{\rule{0.166667em}{0ex}}\mathbf{e}$. From the relation on the currents (10), we get ${\gamma}_{2}\phantom{\rule{0.166667em}{0ex}}{\mathbf{i}}_{2}=-{\gamma}_{1}\phantom{\rule{0.166667em}{0ex}}{\mathbf{i}}_{1}$. Now, if ${\gamma}_{2}$ is invertible, we denote $\gamma ={\gamma}_{2}^{-1}{\gamma}_{1}$ and
☐

$$\underset{\tilde{\mathbf{a}}}{\underbrace{\left(\begin{array}{c}{\mathbf{v}}_{1}\\ {\mathbf{i}}_{2}\end{array}\right)}}=\underset{\tilde{\mathbf{J}}}{\underbrace{\left(\begin{array}{cc}0& {\gamma}^{\u22ba}\\ -\gamma & 0\end{array}\right)}}\underset{\tilde{\mathbf{b}}}{\underbrace{\left(\begin{array}{c}{\mathbf{i}}_{1}\\ {\mathbf{v}}_{2}\end{array}\right)}}.$$

The PHS (5) is obtained by rearranging the edges according to their role with respect to the power balance, according to the permutation of vector elements $\Pi \left(\tilde{\mathbf{a}}\right)={\left(\frac{\mathtt{d}\mathbf{x}}{\mathtt{d}t},\mathbf{w},\mathbf{y}\right)}^{\u22ba}=\mathbf{a}$ (and correspondingly $\Pi \left(\tilde{\mathbf{b}}\right)={\left(\nabla \mathcal{H},\mathbf{z},\mathbf{u}\right)}^{\u22ba}=\mathbf{b}$), which is also applied on rows and columns of $\tilde{\mathbf{J}}$ of yield $\mathbf{a}=\mathbf{J}\mathbf{b}$.

From the invertibility condition on γ

_{2}in Proposition 1, we state the following remark, which is used in the sequel to derive the realizability analysis algorithm.**Remark**

**3 (Necessary condition for realizability).**

A necessary condition for the graph G to be realizable as a PHS (5) is that it includes as many current-controlled edges as nodes ${n}_{N}$, with ${\gamma}_{2}\in {\mathbb{R}}^{{n}_{N}\times {n}_{N}}$.

#### 3.2.2. Algorithm

This section introduces an algorithm that selects the appropriate control type for each dissipative edge so that the partition $\mathsf{B}=\{{\mathsf{B}}_{1},{\mathsf{B}}_{2}\}$ satisfies Proposition 1. From Remark 3, the total number ${n}_{2}$ of current-controlled edges should be exactly equal to the number of nodes ${n}_{N}$. From the special structure of the incidence matrix Γ, this in turn ensures that the potential on each node is uniquely defined by a linear combination of the voltages ${\mathbf{v}}_{2}$ (elements in $\mathbf{a}$ associated to current-controlled edges); i.e., ${\gamma}_{2}$ is invertible.

Consider the current-controlled edge b from node i to node j in Figure 3. If the potential on node j is known, the remaining potential is obtained from ${e}_{i}={v}_{b}\pm {e}_{j}$, where the sign depends on the orientation. In this case, we say edge b imposes the potential on node i. Now, the objective is to perform this analysis globally so that $\mathbf{e}$ is a linear combination of ${\mathbf{v}}_{2}$ with $\mathbf{e}={\left({\gamma}_{2}^{\u22ba}\right)}^{-1}\xb7{\mathbf{v}}_{2}$. To that end, we introduce the realizability matrix Λ defined element-wise as follows

$$\begin{array}{ccc}\hfill {[\Lambda ]}_{n,b}& =& \left\{\begin{array}{cc}1& \mathrm{if}\mathrm{branch}b\mathrm{imposes}\mathrm{potential}\mathrm{on}\mathrm{node}n,\hfill \\ 0& \mathrm{else},\hfill \end{array}\right.\hfill \end{array}$$

$$\begin{array}{ccc}\hfill \Lambda & =& \left(\frac{{\lambda}_{0}}{\begin{array}{cc}{\lambda}_{1}& {\lambda}_{2}\end{array}}\right),\phantom{\rule{1.em}{0ex}}\mathrm{with}\phantom{\rule{0.277778em}{0ex}}{\lambda}_{0}\in {\mathbb{R}}^{1\times {n}_{B}},\phantom{\rule{0.277778em}{0ex}}{\lambda}_{1}\in {\mathbb{R}}^{{n}_{N}\times {n}_{1}}\phantom{\rule{0.277778em}{0ex}}{\lambda}_{2}\in {\mathbb{R}}^{{n}_{N}\times {n}_{2}}.\hfill \end{array}$$

Then, a given graph is realizable if the type of each resistor can be selected so that the following set of constraints is fulfilled.

**(C1)**- The potential on each node $n\in [1,\cdots ,{n}_{N}]$ is uniquely defined so that ${\sum}_{b=1}^{{n}_{B}}{[\Lambda ]}_{n,b}=1$.
**(C2)**- Each Current-controlled edge $b\in [{n}_{1}+1,\cdots ,{n}_{N}]$ propagates the knowledge of the potential on one node to the other, so that ${\sum}_{n=1}^{{n}_{N}}{[\Lambda ]}_{n,b}=1$.
**(C3)**- No edge imposes the reference potential ${e}_{0}$ so that ${\lambda}_{0}={\mathbb{0}}_{1\times {n}_{B}}$.
**(C4)**- No voltage-controlled edge $b\in [1,\cdots ,{n}_{1}]$ imposes any potential so that ${\lambda}_{1}={\mathbb{0}}_{{n}_{N}\times {n}_{2}}$.

Constraints (C1–C2) ensure that ${\gamma}_{2}$ is invertible, so that $\mathbf{e}={\left({\gamma}_{2}^{\u22ba}\right)}^{-1}\xb7{\mathbf{v}}_{2}$. Constraint (C3) ensures that the reference potential on datum does not contribute to the system’s dynamics. Constraint (C4) ensures that inputs of voltages-controlled edges are explicitly given by a linear combination of the nodes potentials so that ${\mathbf{v}}_{1}={\gamma}_{1}^{\u22ba}\phantom{\rule{0.166667em}{0ex}}\mathbf{e}={({\gamma}_{2}^{-1}\xb7{\gamma}_{1})}^{\u22ba}\xb7{\mathbf{v}}_{2}$. To build and analyze the matrix Λ, we start from the adjacency matrix $\mathsf{A}$ of the graph, defined as follows:

$${\left[\mathsf{A}\right]}_{b,n}=\left\{\begin{array}{cc}1& \mathrm{if}\mathrm{branch}b\mathrm{is}\mathrm{connected}\mathrm{to}\mathrm{node}n,\hfill \\ 0& \mathrm{else}.\hfill \end{array}\right.$$

Then the non-zero elements in $\mathsf{A}$ are analyzed to cope with the realizability constraints (C1–C4). This yields Algorithm 1. The PHS (5) is finally recovered as discussed in the proof of Property 1.

Algorithm 1: Analysis of realizability. If successfully complete, the resulting PHS structure is given by the procedure in the proof of Proposition 1 |

#### 3.2.3. Example

As an example, the realizability analysis for the system in Figure 2 with the choice of inputs/outputs in Table 1 is as follows. In step 1, the realizability matrix Λ is initialized with the adjacency matrix $\mathsf{A}$, which is built by taking the absolute value of incidence matrix ${\left[\mathsf{A}\right]}_{n,b}=\mathrm{abs}\left({[\Gamma ]}_{n,b}\right)$:

In steps 3–7, the set of edges $\mathsf{B}=\{{C}_{1},{R}_{1},{Q}_{1,bc},{Q}_{1,be},IN,Vcc,OUT,GRD\}$ is split as $\mathsf{B}=\{{\mathsf{B}}_{1},{\mathsf{B}}_{\mathbf{i}},{\mathsf{B}}_{2}\}$ according to the definition of components with voltage-controlled edges ${\mathsf{B}}_{1}=\{{Q}_{1,bc},{Q}_{1,be},OUT\}$, current-controlled edges ${\mathsf{B}}_{2}=\{{C}_{1},Vcc,IN,GRD\}$, and indeterminate edge ${\mathsf{B}}_{\mathbf{i}}=\left\{{R}_{1}\right\}$:

The realizability matrix after step 11 in Algorithm 1 is

After step 19, the algorithm concludes that the potential on node ${\mathsf{N}}_{1}$ is imposed by edge ${\mathsf{B}}_{\mathrm{IN}}$ so that the potential on node ${\mathsf{N}}_{2}$ is imposed by the capacitor ${\mathsf{B}}_{C1}$. After step 28, the algorithm concludes that the potential on node ${N}_{4}$ is imposed by the edge ${\mathsf{B}}_{\mathrm{Vcc}}$ so that the resistor is current-controlled (so as to impose the potential on node ${N}_{3}$).

This concludes the realizability analysis. To recover the associated port-Hamiltonian structure, we return to the incidence matrix Γ. With the new edges ordering $\mathsf{B}=\{{\mathsf{B}}_{1},{\mathsf{B}}_{2}\}$ prescribed by the above analysis, it is rewritten as

Finally, the structure (5) is recovered by computing the matrix $\gamma ={\gamma}_{2}^{-1}\xb7{\gamma}_{1}$ in (11) with

$${\gamma}_{1}=\left(\begin{array}{ccc}\hfill 0& \hfill 0& \hfill 0\\ \hfill 1& \hfill 1& \hfill 0\\ \hfill -1& \hfill 0& \hfill -1\\ \hfill 0& \hfill 0& \hfill 0\\ \hfill 0& \hfill -1& \hfill 0\end{array}\right);\phantom{\rule{1.em}{0ex}}{\gamma}_{2}=\left(\begin{array}{ccccc}\hfill 0& \hfill 1& \hfill 0& \hfill -1& \hfill 0\\ \hfill 0& \hfill -1& \hfill 0& \hfill 0& \hfill 0\\ \hfill 1& \hfill 0& \hfill 0& \hfill 0& \hfill 0\\ \hfill -1& \hfill 0& \hfill -1& \hfill 0& \hfill 0\\ \hfill 0& \hfill 0& \hfill 0& \hfill 0& \hfill -1\end{array}\right).$$

## 4. Guaranteed-Passive Simulation

This section is devoted to the discrete-time simulation of the algebraic-differential system (5); that is, the computation of $\mathbf{x}\left(k\right)\equiv \mathbf{x}(k\xb7T)$ from $\mathbf{u}\left(k\right)\equiv \mathbf{u}(k\xb7T)$, with $k\in \mathbb{N}$, for the constant sampling frequency ${f}_{s}=1/T$.

First, we present the design of a numerical scheme that properly transposes the power balance (1) to the discrete time domain: this choice makes the passivity property preserved, from which stability issues stem. Second, a numerical method is used to solve the implicit equations due to the numerical scheme (on $\mathbf{x}$) and the algebraic equations (on $\mathbf{w}$).

#### 4.1. Numerical Scheme

To ensure the stable simulation of stable dynamical system $\frac{\mathtt{d}\mathbf{x}}{\mathtt{d}t}=\mathbf{f}\left(\mathbf{x}\right)$, many numerical schemes focus on the approximation quality of the time derivative (or integration), combined with operation of the vector field $\mathbf{f}$. Here, we adopt an alternate point of view, by transposing the power balance (1) into the discrete time-domain to preserve passivity. This is achieved by numerical schemes that provide a discrete version of the chain rule for computing the derivative of the composite function $\mathtt{E}=\mathcal{H}\left(\mathbf{x}\right)$.

This is the case of the forward difference scheme, for which first order approximation of the differential applications $\mathtt{d}\mathbf{x}(t,\mathtt{d}t)=\frac{\mathtt{d}\mathbf{x}}{\mathtt{d}t}\left(t\right)\xb7\mathtt{d}t$ and $\mathtt{d}\mathcal{H}(\mathbf{x},\mathtt{d}\mathbf{x})=\nabla \mathcal{H}{\left(\mathbf{x}\right)}^{\u22ba}\xb7\mathtt{d}\mathbf{x}$ on the sample grid $t\equiv kT,\phantom{\rule{0.166667em}{0ex}}k\in \mathbb{Z}$ are given by
where, for mono-variate energy storing components ($\mathcal{H}\left(\mathbf{x}\right)={\sum}_{n=1}^{{n}_{S}}{h}_{n}\left({x}_{n}\right)$), the n-th coordinate is given by

$$\begin{array}{ccc}\hfill \delta \mathbf{x}(k,T)& =& \mathbf{x}(k+1)-\mathbf{x}\left(k\right),\hfill \end{array}$$

$$\begin{array}{ccc}\hfill \delta \mathcal{H}\left(\mathbf{x}\left(k\right),\delta \mathbf{x}(k,T)\right)& =& \mathcal{H}\left(\mathbf{x}\left(k\right)+\delta \mathbf{x}(k,T)\right)-\mathcal{H}\left(\mathbf{x}\left(k\right)\right)\hfill \\ & =& {\nabla}_{d}\mathcal{H}{\left(\mathbf{x}\left(k\right),\mathbf{x}\left(k\right)+\delta \mathbf{x}(k,T)\right)}^{\u22ba}\xb7\delta \mathbf{x}(k,T).\hfill \end{array}$$

$${\left[{\nabla}_{d}\mathcal{H}\left(\mathbf{x},\mathbf{x}+\delta \mathbf{x}\right)\right]}_{n}=\left\{\begin{array}{cc}\frac{{h}_{n}\left({x}_{n}+\delta {x}_{n}\right)-{h}_{n}\left({x}_{n}\right)}{\delta {x}_{n}}\hfill & \mathrm{if}\delta {x}_{n}\ne 0,\hfill \\ {h}_{n}^{\prime}\left({x}_{n}\right)\hfill & \mathrm{otherwise}.\hfill \end{array}\right.$$

A discrete chain rule is indeed recovered
so that the following substitution in (5)
leads to

$$\frac{\delta \mathtt{E}(k,T)}{T}={\nabla}_{d}\mathcal{H}{\left(\mathbf{x}\left(k\right),\mathbf{x}(k+1)\right)}^{\u22ba}\xb7\frac{\delta \mathbf{x}(k,T)}{T}$$

$$\begin{array}{ccc}\hfill \frac{\mathtt{d}\mathbf{x}}{\mathtt{d}t}\left(t\right)& \to & \frac{\delta \mathbf{x}(k,T)}{T}\hfill \\ \hfill \nabla \mathcal{H}\left(\mathbf{x}\right)& \to & {\nabla}_{d}\mathcal{H}\left(\mathbf{x}\left(k\right),\mathbf{x}(k+1)\right)\hfill \end{array}$$

$$\begin{array}{ccc}\hfill 0& =& \mathbf{b}{\left(k\right)}^{\u22ba}\xb7\mathbf{J}\xb7\mathbf{b}\left(k\right)=\mathbf{b}{\left(k\right)}^{\u22ba}\xb7\mathbf{a}\left(k\right)\hfill \\ & =& \underset{\frac{\delta \mathtt{E}(k,T)}{T}}{\underbrace{\left[{\nabla}_{d}{\mathcal{H}}^{\u22ba}\phantom{\rule{-0.166667em}{0ex}}\xb7\frac{\delta \mathbf{x}}{\delta t}\right]\left(k\right)}}+\underset{\mathtt{D}\left(k\right)}{\underbrace{\mathbf{z}{\left(\mathbf{w}\left(k\right)\right)}^{\u22ba}\phantom{\rule{-0.166667em}{0ex}}\xb7\mathbf{w}\left(k\right)}}-\underset{\mathtt{S}\left(k\right)}{\underbrace{\mathbf{u}{\left(k\right)}^{\u22ba}\phantom{\rule{-0.166667em}{0ex}}\xb7\mathbf{y}\left(k\right)}}.\hfill \end{array}$$

**Remark**

**4 (Multi-variate components).**

The case of mono-variate energy storing components covers most of the applications in electronics. Additionally, a generalization of the discrete gradient for multi-variate Hamiltonians such that Equations (20) and (21) are satisfied is given in Appendix C.

In this paper, we consider the class of the PHS composed of a collection of linear energy storing components, with quadratic Hamiltonian ${h}_{n}\left({x}_{n}\right)=\frac{{x}_{n}^{2}}{2{C}_{n}}$ (${C}_{n}$ is a capacitance or an inductance and we define $\mathbf{Q}=\mathrm{diag}{({C}_{1}\cdots {C}_{{n}_{S}})}^{-1}$). Then the discrete gradient (22) reads
which restores the midpoint rule that coincides in this case with the trapezoidal rule. For nonlinear cases, (22) leads to another numerical scheme depending on the nonlinearity, still preserving passivity (see (25) and §4.3).

$$\begin{array}{ccc}\hfill {\nabla}_{d}\mathcal{H}\left(\mathbf{x},\mathbf{x}+\delta \mathbf{x}\right)& =& \mathbf{Q}\left(\mathbf{x}\left(k\right)+\frac{\delta \mathbf{x}\left(k\right)}{2}\right),\hfill \end{array}$$

#### 4.2. Solving the Implicit Equations

Injecting the numerical scheme (26) in (5) and solving for the quantity $\delta \mathbf{x}\left(k\right)=\mathbf{x}(k+1)-\mathbf{x}\left(k\right)$ leads to the following energy-preserving numerical system:
where matrices are related to $\mathbf{J}$ in (5) as follows.

$$\left(\begin{array}{c}\delta \mathbf{x}\left(k\right)\\ \mathbf{w}\left(k\right)\\ \mathbf{y}\left(k\right)\end{array}\right)=\left(\begin{array}{ccc}{\mathbf{A}}_{\mathbf{x}}\hfill & {\mathbf{B}}_{\mathbf{x}}\hfill & {\mathbf{C}}_{\mathbf{x}}\hfill \\ {\mathbf{A}}_{\mathbf{w}}\hfill & {\mathbf{B}}_{\mathbf{w}}\hfill & {\mathbf{C}}_{\mathbf{w}}\hfill \\ {\mathbf{A}}_{\mathbf{y}}\hfill & {\mathbf{B}}_{\mathbf{y}}\hfill & {\mathbf{C}}_{\mathbf{y}}\hfill \end{array}\right)\xb7\left(\begin{array}{c}\mathbf{x}\left(k\right)\\ \mathbf{z}\left(\mathbf{w}\left(k\right)\right)\\ \mathbf{u}\left(k\right)\end{array}\right),$$

$$\begin{array}{cc}\mathbf{D}=\left(\frac{{\mathbf{I}}_{\mathbf{d}}}{T}-\frac{{\mathbf{J}}_{\mathbf{x}}\xb7\mathbf{Q}}{2}\right),\phantom{\rule{-0.166667em}{0ex}}{\phantom{\rule{-0.166667em}{0ex}}}^{-1}\hfill & {\mathbf{A}}_{\mathbf{w}}=\frac{1}{2}{\mathbf{K}}^{\u22ba}\xb7\mathbf{Q}\xb7(2{\mathbf{I}}_{\mathbf{d}}+{\mathbf{A}}_{\mathbf{x}}),\hfill \\ {\mathbf{A}}_{\mathbf{x}}=\mathbf{D}\xb7{\mathbf{J}}_{\mathbf{x}}\xb7\mathbf{Q},\hfill & {\mathbf{B}}_{\mathbf{w}}={\mathbf{J}}_{\mathbf{w}}+\frac{1}{2}{\mathbf{K}}^{\u22ba}\xb7\mathbf{Q}\xb7{\mathbf{B}}_{\mathbf{x}},\hfill \\ {\mathbf{B}}_{\mathbf{x}}=-\mathbf{D}\xb7\mathbf{K},\hfill & {\mathbf{C}}_{\mathbf{w}}=-{\mathbf{G}}_{\mathbf{w}}+\frac{1}{2}{\mathbf{K}}^{\u22ba}\xb7\mathbf{Q}\xb7{\mathbf{C}}_{\mathbf{x}},\hfill \\ {\mathbf{C}}_{\mathbf{x}}=-\mathbf{D}\xb7{\mathbf{G}}_{\mathbf{x}},\hfill & {\mathbf{A}}_{\mathbf{y}}=\frac{1}{2}{{\mathbf{G}}_{\mathbf{x}}}^{\u22ba}\xb7\mathbf{Q}\xb7(2{\mathbf{I}}_{\mathbf{d}}+{\mathbf{A}}_{\mathbf{x}}),\hfill \\ & {\mathbf{B}}_{\mathbf{y}}={{\mathbf{G}}_{\mathbf{w}}}^{\u22ba}+\frac{1}{2}{{\mathbf{G}}_{\mathbf{x}}}^{\u22ba}\xb7\mathbf{Q}\xb7{\mathbf{B}}_{\mathbf{x}},\hfill \\ & {\mathbf{C}}_{\mathbf{y}}={\mathbf{J}}_{\mathbf{y}}+\frac{1}{2}{{\mathbf{G}}_{\mathbf{x}}}^{\u22ba}\xb7\mathbf{Q}\xb7{\mathbf{C}}_{\mathbf{x}}.\hfill \end{array}$$

Given $\mathbf{u}\left(k\right)$, the solution of (27) is obtained from the solution of the static nonlinear implicit function $\mathbf{f}\left(\mathbf{w}\left(k\right)\right)=\mathbf{p}\left(\mathbf{x}\left(k\right),\mathbf{u}\left(k\right)\right)$, with

$$\begin{array}{ccc}\hfill \mathbf{f}\left(\mathbf{w}\left(k\right)\right)& =& \mathbf{w}\left(k\right)-{\mathbf{B}}_{\mathbf{w}}\xb7\mathbf{z}\left(\mathbf{w}\left(k\right)\right),\hfill \\ \hfill \mathbf{p}\left(\mathbf{x}\left(k\right),\mathbf{u}\left(k\right)\right)& =& {\mathbf{A}}_{\mathbf{w}}\xb7\mathbf{x}\left(k\right)+{\mathbf{C}}_{\mathbf{w}}\xb7\mathbf{u}\left(k\right).\hfill \end{array}$$

**Remark**

**5 (Explicit mapping).**

From the global inverse function theorem (see [34]), there exists an explicit mapping $\mathbf{w}\left(k\right)={\mathbf{f}}^{-1}\phantom{\rule{-0.166667em}{0ex}}\left(\mathbf{p}\left(\mathbf{x}\left(k\right),\mathbf{u}\left(k\right)\right)\right)$ provided the Jacobian matrix ${\mathcal{J}}_{\mathbf{f}}\left(\mathbf{w}\left(k\right)\right)={\mathbf{I}}_{\mathbf{d}}-({\mathbf{J}}_{\mathbf{w}}-\frac{1}{2}{\mathbf{K}}^{\u22ba}\xb7\mathbf{Q}\xb7\mathbf{D}\xb7\mathbf{K})\xb7{\mathcal{J}}_{\mathbf{z}}\left(\mathbf{w}\left(k\right)\right)$ is invertible for all $\mathbf{w}\left(k\right)$, connecting the proposed method to the K method [18,19]. This is true since $\mathbf{Q}$, $\mathbf{D}$ and the Jacobian of $\mathbf{z}$ (for the components of the dictionary in Table B1) prove positive definite, and ${\mathbf{J}}_{\mathbf{w}}$ is skew-symmetric.

In this paper, we use the Newton–Raphson algorithm, which iteratively approximates the nearest root of function $\mathbf{r}:\mathbf{w}\left(k\right)\in {\mathbb{R}}^{{n}_{D}}\to \mathbf{r}\left(\mathbf{w}\left(k\right)\right)\in {\mathbb{R}}^{{n}_{D}}$ with the following update rule: ${\mathbf{w}}_{n+1}\left(k\right)={\mathbf{w}}_{n}\left(k\right)-{\mathcal{J}}_{\phantom{\rule{0.277778em}{0ex}}\mathbf{r}}{\left({\mathbf{w}}_{n}\left(k\right)\right)}^{-1}.\phantom{\rule{0.166667em}{0ex}}\mathbf{r}\left({\mathbf{w}}_{n}\left(k\right)\right),$ where ${\mathcal{J}}_{\phantom{\rule{0.277778em}{0ex}}\mathbf{r}}\left(\mathbf{w}\right)$ is the Jacobian matrix of $\mathbf{r}\left(\mathbf{w}\left(k\right)\right)=\mathbf{f}\left(\mathbf{w}\left(k\right)\right)-\mathbf{p}\left(k\right)$. Once a solution $\mathbf{w}\left(k\right)$ to the implicit equation is available, the output and state updates are given by:

$$\begin{array}{ccc}\hfill \mathbf{y}\left(k\right)& =& {\mathbf{A}}_{\mathbf{y}}\xb7\mathbf{x}\left(k\right)+{\mathbf{B}}_{\mathbf{y}}\xb7\mathbf{z}\left(\mathbf{w}\left(k\right)\right)+{\mathbf{C}}_{\mathbf{y}}\xb7\mathbf{u}\left(k\right),\hfill \\ \hfill \delta \mathbf{x}\left(k\right)& =& {\mathbf{A}}_{\mathbf{x}}\xb7\mathbf{x}\left(k\right)+{\mathbf{B}}_{\mathbf{x}}\xb7\mathbf{z}\left(\mathbf{w}\left(k\right)\right)+{\mathbf{C}}_{\mathbf{x}}\xb7\mathbf{u}\left(k\right),\hfill \\ \hfill \mathbf{x}(k+1)& =& \mathbf{x}\left(k\right)+\delta \mathbf{x}\left(k\right).\hfill \end{array}$$

Finally, denoting by ${n}_{t}$ the number of time-steps and ${n}_{NR}$ the number of Newton–Raphson iterations per time-step, the simulation is performed according to Algorithm 2.

Algorithm 2: Simulation, with ${n}_{t}$ the number of time-steps and ${n}_{NR}$ the (fixed) number of Newton–Raphson iterations. |

#### 4.3. Comparison with Standard Methods

In this section, the proposed approach (PHS structure combined with the discrete gradient method) is compared with two standard methods: the trapezoidal rule (average of the vector field at $\mathbf{x}\left(k\right)$ and $\mathbf{x}(k+1)$, used in the WDF approach [7]) and the midpoint rule (evaluation of the vector field at $\frac{\mathbf{x}\left(k\right)+\mathbf{x}(k+1)}{2}$, suitable for any differential-algebraic system of equations). Both are known to preserve the passivity of linear undamped systems (see [35] for a detailed analysis). The updates associated with these three methods are given in Table 3. These methods are applied on the same conservative system $\frac{\mathtt{d}\mathbf{x}}{\mathtt{d}t}={\mathbf{J}}_{\mathbf{x}}\xb7\nabla \mathcal{H}\left(\mathbf{x}\right)$, the power balance of which is given by $\frac{\mathtt{d}\mathtt{E}}{\mathtt{d}t}=0$ (with $\mathtt{D}=\mathtt{S}=0$). The comparison measure is then the relative error on energy $\epsilon \left(k\right)=\frac{\left|\mathcal{H}\left(\mathbf{x}(k+1)\right)-\mathcal{H}\left(\mathbf{x}\left(k\right)\right)\right|}{\mathcal{H}\left(\mathbf{x}\left(0\right)\right)}$ for $k\ge 0$.

First, notice that for quadratic Hamiltonian $\mathcal{H}\left(\mathbf{x}\right)=\frac{{\mathbf{x}}^{\u22ba}\xb7\mathbf{Q}\xb7\mathbf{x}}{2}$ with linear gradient $\nabla \mathcal{H}\left(\mathbf{x}\right)=\mathbf{Q}\xb7\mathbf{x}$, the three methods yield the same update:

$$\mathbf{x}(k+1)=\mathbf{x}\left(k\right)+T\xb7{\mathbf{J}}_{\mathbf{x}}\xb7\mathbf{Q}\left(\mathbf{x}\left(k\right)+\frac{\delta \mathbf{x}\left(k\right)}{2}\right).$$

As a consequence, these three methods induce the same frequency warping (see [36] for the analysis of the bilinear transform derived from the trapezoidal rule).

We focus on the nonlinear case. For comparison, we choose a simple nonlinear conservative system with state $\mathbf{x}={({x}_{1},{x}_{2})}^{\u22ba}$, non-quadratic Hamiltonian
and canonical skew-symmetric matrix ${\mathbf{J}}_{\mathbf{x}}=\left(\begin{array}{cc}0& -1\\ 1& 0\end{array}\right)$.

$$\mathcal{H}\left(\mathbf{x}\right)=10\phantom{\rule{0.166667em}{0ex}}log\left(cosh\left({x}_{1}\right)\right)+\left(cosh\left({x}_{2}\right)-1\right),$$

In each case, the resulting implicit equations are solved by Python iterative solver (see [37]). The Python code is available at the url given in [38]. In order to exhibit the behavior of each method in the worst case, simulations are performed with an especially low sample rate of ${f}_{s}=10$ Hz. The results for each method are given in Figure 4, with comparison in Figure 4d. We see that the error of the proposed method is low (close to machine precision ≃10${}^{-16}$) compared to standard methods.

## 5. Applications

This section is devoted to the simulation of three analog audio circuits by the application of Algorithms 1 and 2. Those circuits are a diode clipper, a common-emitter BJT audio amplifier, and a wah-pedal as a full device. Results obtained with (i) the method in Section 4 and (ii) with the offline circuit simulator LT-Spice [32] are compared.

#### 5.1. Diode Clipper

Diode clipper circuits can be found in several audio-distortion devices. They are made of one resistor and two diodes (${n}_{S}=0,\phantom{\rule{0.277778em}{0ex}}{n}_{D}=3$) connected to the ground in reversed bias (see Figure 5a). The external ports are the input/output and the ground (${n}_{P}=3$). The resistor is current-controlled and the ground is removed. The vectors $(\mathbf{a},\mathbf{b})$ and the structure $\mathbf{J}$ returned by Algorithm 1 are:

$$\underset{\mathbf{a}}{\underbrace{\left(\phantom{\rule{-0.166667em}{0ex}}\phantom{\rule{-0.166667em}{0ex}}\begin{array}{c}{i}_{R}\hfill \\ {v}_{D1}\hfill \\ {v}_{D2}\hfill \\ {i}_{\mathrm{IN}}\hfill \\ {v}_{\mathrm{OUT}}\hfill \end{array}\phantom{\rule{-0.166667em}{0ex}}\phantom{\rule{-0.166667em}{0ex}}\right)}}\phantom{\rule{-0.166667em}{0ex}}=\phantom{\rule{-0.166667em}{0ex}}\underset{\mathbf{J}}{\underbrace{\left(\begin{array}{ccccc}\hfill 0& \hfill -1& \hfill 1& \hfill 0& \hfill -1\\ \hfill 1& \hfill 0& \hfill 0& \hfill 1& \hfill 0\\ \hfill -1& \hfill 0& \hfill 0& \hfill -1& \hfill 0\\ \hfill 0& \hfill -1& \hfill 1& \hfill 0& \hfill -1\\ \hfill 1& \hfill 0& \hfill 0& \hfill 1& \hfill 0\end{array}\right)}}\phantom{\rule{-0.166667em}{0ex}}\xb7\phantom{\rule{-0.166667em}{0ex}}\underset{\mathbf{b}}{\underbrace{\left(\phantom{\rule{-0.166667em}{0ex}}\phantom{\rule{-0.166667em}{0ex}}\begin{array}{c}{v}_{R}\hfill \\ {i}_{D1}\hfill \\ {i}_{D2}\hfill \\ {v}_{\mathrm{IN}}\hfill \\ {i}_{\mathrm{OUT}}\hfill \end{array}\phantom{\rule{-0.166667em}{0ex}}\phantom{\rule{-0.166667em}{0ex}}\right)}}.$$

The simulation is performed according to Algorithm 2 at the sample rate ${f}_{s}=96$ kHz, with three Newton–Raphson iterations (shown to be enough to converge in practice). We apply a linearly increasing 1 kHz sinusoidal excitation ${u}_{\mathrm{IN}}=-{v}_{\mathrm{IN}}$ during 10 ms with maximum amplitude 2 V (${i}_{\mathrm{OUT}}=0$ A, ${v}_{\mathrm{GRD}}=0$ V). The output ${y}_{\mathrm{OUT}}=-{v}_{\mathrm{OUT}}$ is given in Figure 5b. We see the signal is clamped between $\pm 0.6$ V, in accordance with LT-Spice results.

#### 5.2. Common-Emitter BJT Audio Amplifier

Common-emitter bipolar-junction transistor (BJT) amplifiers are widely used as amplification stages in analog audio processing. They are made of two capacitors (${n}_{S}=2$), two resistors, and one NPN transistor which is made of two nonlinear dissipative branches (${n}_{D}=4$, see Figure 6a and the dictionary in Table B1). The external ports are the input/output signals, the 9 V supply, and the ground (${n}_{P}=4$). Note that the ground is removed. The resistor $\mathrm{Rc}$ is current-controlled, and the resistor $\mathrm{Rf}$ is voltage-controlled. The vectors $(\mathbf{a},\mathbf{b})$ and the structure $\mathbf{J}$ returned by Algorithm 1 are given in Equations (31) and (32).

$$\begin{array}{ccc}\hfill \mathbf{a}& =& \left({i}_{\mathrm{Ci}},\phantom{\rule{0.277778em}{0ex}}{i}_{\mathrm{Co}}|{v}_{\mathrm{Rf}},\phantom{\rule{0.277778em}{0ex}}{i}_{\mathrm{Rc}},\phantom{\rule{0.277778em}{0ex}}{v}_{\mathrm{Bc}},\phantom{\rule{0.277778em}{0ex}}{v}_{\mathrm{Be}}|{i}_{\mathrm{IN}},\phantom{\rule{0.277778em}{0ex}}{v}_{\mathrm{OUT}},\phantom{\rule{0.277778em}{0ex}}{i}_{\mathrm{VCC}}\right)\hfill \\ \hfill \mathbf{b}& =& \left({v}_{\mathrm{Ci}},\phantom{\rule{0.277778em}{0ex}}{v}_{\mathrm{Co}}|{i}_{\mathrm{Rf}},\phantom{\rule{0.277778em}{0ex}}{v}_{\mathrm{Rc}},\phantom{\rule{0.277778em}{0ex}}{i}_{\mathrm{Bc}},\phantom{\rule{0.277778em}{0ex}}{i}_{\mathrm{Be}}|{v}_{\mathrm{IN}},\phantom{\rule{0.277778em}{0ex}}{i}_{\mathrm{OUT}},\phantom{\rule{0.277778em}{0ex}}{v}_{\mathrm{VCC}}\right)\hfill \end{array}$$

$$\begin{array}{ccc}\hfill \mathbf{J}& =& \left(\begin{array}{ccccccccc}\hfill 0& \hfill 0& \hfill 1& \hfill 0& \hfill -1& \hfill 1& \hfill 0& \hfill 0& \hfill 0\\ \hfill 0& \hfill 0& \hfill 0& \hfill 0& \hfill 0& \hfill 0& \hfill 0& \hfill -1& \hfill 0\\ \hfill -1& \hfill 0& \hfill 0& \hfill -1& \hfill 0& \hfill 0& \hfill -1& \hfill 0& \hfill 1\\ \hfill 0& \hfill 0& \hfill 1& \hfill 0& \hfill -1& \hfill 0& \hfill 0& \hfill 1& \hfill 0\\ \hfill 1& \hfill 0& \hfill 0& \hfill 1& \hfill 0& \hfill 0& \hfill 1& \hfill 0& \hfill -1\\ \hfill -1& \hfill 0& \hfill 0& \hfill 0& \hfill 0& \hfill 0& \hfill -1& \hfill 0& \hfill 0\\ \hfill 0& \hfill 0& \hfill 1& \hfill 0& \hfill -1& \hfill 1& \hfill 0& \hfill 0& \hfill 0\\ \hfill 0& \hfill 1& \hfill 0& \hfill -1& \hfill 0& \hfill 0& \hfill 0& \hfill 0& \hfill 1\\ \hfill 0& \hfill 0& \hfill -1& \hfill 0& \hfill 1& \hfill 0& \hfill 0& \hfill -1& \hfill 0\end{array}\right)\hfill \end{array}$$

The system is reduced according to Appendix A, and the simulation is performed according to Algorithm 2 at the sample rate ${f}_{s}=384$ kHz, with 10 Newton–Raphson iterations. The reason for increasing the sample-rate and the number of Newton–Raphson iterations is twofold. Firstly, it attenuates the effect of aliasing for input signals limited to the audio range (see [39] for details). Secondly, it ensures that the iterative solver converges, which is difficult due to the numerical stiffness of the problem; that is, the Lipschitz constant associated to the inital Cauchy problem is very high, see [40]. At first, we turn the supply ${v}_{\mathrm{VCC}}=-9$ V on, and we wait $0.3$ s for the system to reach its steady state. Then, we apply a 10 ms sinusoidal excitation ${u}_{\mathrm{IN}}=-{v}_{\mathrm{IN}}$ at 1 kHz with linearly increasing amplitude between 0 V and $0.2$ V (${i}_{\mathrm{OUT}}=0$ A). The resulting output ${y}_{\mathrm{OUT}}=-{v}_{\mathrm{OUT}}$ is given in Figure 6b. We see that the signal is amplified between 0 V and 9 V, with a strong asymmetrical saturation, in accordance with LT-Spice results. Additionally, spectrograms obtained for an exponential chirp on the audio range are given in Figure 7 (see Figure 16 in [39] for comparison).

#### 5.3. Wah Pedal

This section addresses the simulation of a full device (namely the Dunlop Cry-Baby wah pedal) to be used in real time. The circuit is given in Figure 9. It provides a continuously varying characteristic wah filtering of the input signal. This circuit has been treated with the nodal discrete K-method in [41] and with the PHS framework in [42]. It is composed of ${n}_{S}$ = 7 storage branches (6 capacitors and 1 inductor), ${n}_{D}\phantom{\rule{-0.166667em}{0ex}}=\phantom{\rule{-0.166667em}{0ex}}18$ dissipative branches (11 resistors, 1 PN diode, 2 NPN transistors and a potentiometer), and ${n}_{P}$ = 3 ports (input/output signals and battery, discarding the 5 grounds). The $wah$ parameter is the potentiometer’s coefficient α. Notice this circuit includes several edges that do not contribute to the device input-to-output behavior, as analyzed in [41]. In this work, we consider the complete original schematic. From Algorithm 1, the resistors ${R}_{1}$, ${R}_{6}\cdots {R}_{9}$ and ${R}_{11}$ are considered as conductances, and the others as resistances. The structure $\mathbf{J}$ is not shown here. The sets of PHS variables are:
where ${\mathbf{w}}_{R}$ is the set of dissipative states and ${\mathbf{z}}_{R}$ the set of characteristics according to each resistor’s type, and

$$\begin{array}{ccc}\dot{\mathbf{x}}& =& {[{i}_{C1},\cdots ,{i}_{{C}_{6}},{v}_{{L}_{1}}]}^{\u22ba},\hfill \\ \nabla \mathcal{H}\left(\mathbf{x}\right)& =& {[{v}_{C1},\cdots ,{v}_{{C}_{6}},{i}_{{L}_{1}}]}^{\u22ba},\hfill \end{array}$$

$$\begin{array}{ccc}\mathbf{w}& =& [{\mathbf{w}}_{R}|{v}_{d}|{v}_{B{C}_{1}},{v}_{B{E}_{1}}|{v}_{B{C}_{2}},{v}_{B{E}_{2}}{|{v}_{p1},{i}_{p2}]}^{\u22ba},\\ \mathbf{z}\left(\mathbf{w}\right)& =& [{\mathbf{z}}_{R}|{i}_{d}|{i}_{B{C}_{1}},{i}_{B{E}_{1}}|{i}_{B{C}_{2}},{i}_{B{E}_{2}}{|{i}_{p1},{v}_{p2}]}^{\u22ba},\end{array}$$

$$\begin{array}{ccc}\hfill \mathit{Inputs}\phantom{\rule{1.em}{0ex}}\mathbf{u}& =& {[{v}_{in},{i}_{out},{v}_{cc}]}^{\u22ba},\hfill \\ \hfill \mathit{Outputs}\phantom{\rule{1.em}{0ex}}\mathbf{y}& =& {[{i}_{in},{v}_{out},{i}_{cc}]}^{\u22ba}.\hfill \end{array}$$

The system is reduced according to Appendix A (with potentiometer’s time varying resitors kept in $\mathbf{w},\mathbf{z}$). Firstly, we realize an offline simulation (in Python) with Algorithm 2 for the sampling rate ${f}_{s}=96$ kHz, and three Newton–Raphson iterations. We apply a white noise normalised to 1 V on the input ${u}_{\mathrm{IN}}=-{v}_{\mathrm{IN}}$ (${i}_{\mathrm{OUT}}=0$ A). The magnitudes of transfer functions obtained from fast Fourier transform are given in Figure 8 for the two extreme positions of the pedal. These results are in accordance with LT-Spice.

Secondly, a VST plugin [43] to simulate the Cry-Baby in real-time is made from Algorithm 2. First, a

`C++`code is automatically generated; Second, this code is encapsulated in a Juce template to compile the audio plugin (see [44]). The sample rate ${f}_{s}$ is imposed by the host digital audio workstation (here Ableton Live!), and we force five Newton–Raphson iterations. The simulation performed well (audio examples are available at the url [45]). The CPU load on a laptop (Macbook 2.9 GHz Intel Core i7 with 8Go RAM) is 37% for ${f}_{s}=96$ kHz, and 20% for ${f}_{s}=48$ kHz.**Remark**

**6 (Time-varying stability).**

The use of the Newton–Raphson method can hamper the stability of the numerical solution for time-varying systems, especially in the case of fast variations (here, of the potentiometer) for which the Jacobian matrix of the implicit Function (28) can be ill-conditioned. For linear storage components, a solution is to use the K-method instead (see Remark 5).

## 6. Conclusions

We have established a method to automatically recast an analog audio circuit into PHS formalism, which guarantees passivity of the continuous time model. The generation of the PHS from a given schematic lies on two points:

- the graph theory to describe the interconnection network of a given circuit’s schematic,
- a dictionary of elementary components which are conformable with PHS formalism.

Then, we transposed this physical principle to the digital domain by properly defining the discrete gradient of the Hamiltonian, such that a discrete time version of the power balance is satisfied. The resulting stable numerical scheme is of second order (restoring the midpoint rule for linear systems). It has been shown that the K-method is always applicable to PHS (providing efficient implementations of the implicit relation due to the proposed numerical scheme).

Offline simulations are consistent with LT-Spice results. The whole method allows the automatic generation of

`C++`simulation code to be used in the core of a real-time VST audio plug-in simulating the Dunlop Cry-Baby wah pedal.A first perspective on this work is to consider higher-order numerical schemes (namely, the class of Runge–Kutta schemes). Moreover, it would be possible to symmetrize the roles of the voltages and the currents at the interconnection by applying the Cayley transform to the PHS structure, thus adopting wave variables, with possible connection with the WDF formalism. Additionally, an automated analysis of the original schematic could be developed, so as to identify the unimportant or degenerate states and to reduce the dimensionality of the system. Finally, it could be possible to exploit the compatibility of the proposed method with the K-method to alleviate the numerical cost due to Newton–Raphson iterations.

## Acknowledgments

The authors acknowledge the members of the french National Research Agency project HaMecMoPSys for support in port-Hamiltonian theory. Also, the authors acknowledge Robert Piechaud and Mattia Bergomi for careful proofreading.

## Author Contributions

The scientific work has been achieved by both authors at IRCAM Laboratory, Paris, within the context of the French National Research Agency sponsored project HaMecMoPSys. Further information is available at http://www.hamecmopsys.ens2m.fr. The numerical experiments have been performed by Antoine Falaize.

## Conflicts of Interest

The authors declare no conflict of interest.

## Appendix A. Reduction

The dimension of system (5) can be reduced, considering the following decomposition of variable $\mathbf{w}$ and function $\mathbf{z}$:
with ${\mathbf{z}}_{\mathbf{L}}$ a diagonal matrix whose elements are the resistance or conductance of linear dissipative components, and ${\mathbf{z}}_{\mathbf{N}}$ a collection of the nonlinear dissipative relations. Correspondingly, the structure matrices are decomposed as

$$\mathbf{w}\phantom{\rule{-0.166667em}{0ex}}=\phantom{\rule{-0.166667em}{0ex}}\left(\begin{array}{c}{\mathbf{w}}_{\mathbf{L}}\\ {\mathbf{w}}_{\mathbf{N}}\end{array}\right),\phantom{\rule{1.em}{0ex}}\mathbf{z}\left(\mathbf{w}\right)\phantom{\rule{-0.166667em}{0ex}}=\phantom{\rule{-0.166667em}{0ex}}\left(\begin{array}{c}{\mathbf{z}}_{\mathbf{L}}\xb7{\mathbf{w}}_{\mathbf{L}}\\ {\mathbf{z}}_{\mathbf{N}}\left({\mathbf{w}}_{\mathbf{N}}\right)\end{array}\right),$$

$$\mathbf{K}\phantom{\rule{-0.166667em}{0ex}}=\phantom{\rule{-0.166667em}{0ex}}\left(\begin{array}{c}{\mathbf{K}}_{\mathbf{L}}\\ {\mathbf{K}}_{\mathbf{N}}\end{array}\right),\phantom{\rule{0.277778em}{0ex}}{\mathbf{G}}_{\mathbf{w}}\phantom{\rule{-0.166667em}{0ex}}=\phantom{\rule{-0.166667em}{0ex}}\left(\begin{array}{c}{\mathbf{G}}_{\mathbf{L}}\\ {\mathbf{G}}_{\mathbf{N}}\end{array}\right),\phantom{\rule{0.277778em}{0ex}}{\mathbf{J}}_{\mathbf{w}}\phantom{\rule{-0.166667em}{0ex}}=\phantom{\rule{-0.166667em}{0ex}}\left(\begin{array}{cc}{\mathbf{J}}_{\mathbf{LL}}& -{\mathbf{K}}_{\mathbf{LN}}\\ {{\mathbf{K}}_{\mathbf{LN}}}^{\u22ba}& {\mathbf{J}}_{\mathbf{NN}}\end{array}\right).$$

Defining $\mathbf{L}\phantom{\rule{-0.166667em}{0ex}}=\phantom{\rule{-0.166667em}{0ex}}\left({{\mathbf{K}}_{\mathbf{L}}}^{\u22ba},-{\mathbf{K}}_{\mathbf{LN}},-{\mathbf{G}}_{\mathbf{L}}\right)$, and $\mathbf{M}\phantom{\rule{-0.166667em}{0ex}}=\phantom{\rule{-0.166667em}{0ex}}\left({{\mathbf{z}}_{\mathbf{L}}}^{-1}\phantom{\rule{-0.166667em}{0ex}}-{\mathbf{J}}_{\mathbf{LL}}\right){\phantom{\rule{-0.166667em}{0ex}}}^{-1}$ a positive definite matrix, the system (5) is reduced to
with $\tilde{\mathbf{J}}$ a skew-symmetric matrix given by
and $\mathbf{R}$ is a symmetric positive definite matrix given by

$$\underset{{\mathbf{b}}_{\mathbf{N}}}{\underbrace{\left(\begin{array}{c}\frac{\mathtt{d}\mathbf{x}}{\mathtt{d}t}\\ {\mathbf{w}}_{\mathbf{N}}\\ -\mathbf{y}\end{array}\right)}}=\left(\tilde{\mathbf{J}}-\mathbf{R}\right)\xb7\underset{{\mathbf{a}}_{\mathbf{N}}}{\underbrace{\left(\begin{array}{c}\nabla \mathcal{H}\left(\mathbf{x}\right)\\ {\mathbf{z}}_{\mathbf{N}}\left({\mathbf{w}}_{\mathbf{N}}\right)\\ \mathbf{u}\end{array}\right)}}$$

$$\begin{array}{ccc}\hfill \tilde{\mathbf{J}}& =& \left(\begin{array}{ccc}{\mathbf{J}}_{\mathbf{x}}& -{\mathbf{K}}_{\mathbf{N}}& -{\mathbf{G}}_{\mathbf{x}}\\ {{\mathbf{K}}_{\mathbf{N}}}^{\u22ba}& {\mathbf{J}}_{\mathbf{NN}}& -{\mathbf{G}}_{\mathbf{N}}\\ {{\mathbf{G}}_{\mathbf{x}}}^{\u22ba}& {{\mathbf{G}}_{\mathbf{N}}}^{\u22ba}& {\mathbf{J}}_{\mathbf{y}}\end{array}\right)-\frac{1}{2}{\mathbf{L}}^{\u22ba}\xb7\left(\mathbf{M}-{\mathbf{M}}^{\u22ba}\right)\xb7\mathbf{L}\hfill \end{array}$$

$$\begin{array}{ccc}\hfill \mathbf{R}& =& \left(\begin{array}{ccc}\hfill {\mathbf{R}}_{\mathbf{x}}& \hfill {\mathbf{R}}_{\mathbf{xn}}& \hfill {\mathbf{R}}_{\mathbf{xy}}\\ \hfill {{\mathbf{R}}_{\mathbf{xn}}}^{\u22ba}& \hfill {\mathbf{R}}_{\mathbf{n}}& \hfill {\mathbf{R}}_{\mathbf{ny}}\\ \hfill & & \\ \hfill {{\mathbf{R}}_{\mathbf{xy}}}^{\u22ba}& \hfill {{\mathbf{R}}_{\mathbf{ny}}}^{\u22ba}& \hfill {\mathbf{R}}_{\mathbf{y}}\end{array}\right)=\frac{1}{2}{\mathbf{L}}^{\u22ba}\xb7\left(\mathbf{M}+{\mathbf{M}}^{\u22ba}\right)\xb7\mathbf{L}.\hfill \end{array}$$

Indeed, matrix $\tilde{\mathbf{J}}$ (respectively $\mathbf{R}$) corresponds to the conservative (respectively resistive) interconnection of dynamical storage components, nonlinear dissipative components, and sources.

The system (A1) is simulated by Algorithm 2, with

$$\begin{array}{ccc}\hfill \tilde{\mathbf{D}}& =& {\left(\frac{{\mathbf{I}}_{\mathbf{d}}}{T}-\frac{({\tilde{\mathbf{J}}}_{\mathbf{x}}-{\mathbf{R}}_{\mathbf{x}})\xb7\mathbf{Q}}{2}\right)}^{-1}\hfill \\ \hfill {\tilde{\mathbf{A}}}_{\mathbf{x}}& =& \tilde{\mathbf{D}}\xb7({\tilde{\mathbf{J}}}_{\mathbf{x}}-{\mathbf{R}}_{\mathbf{x}}),\hfill \\ \hfill {\tilde{\mathbf{B}}}_{\mathbf{x}}& =& -\tilde{\mathbf{D}}\xb7(\tilde{\mathbf{K}}+{\mathbf{R}}_{\mathbf{xn}}),\hfill \\ \hfill {\tilde{\mathbf{C}}}_{\mathbf{x}}& =& -\tilde{\mathbf{D}}\xb7({\tilde{\mathbf{G}}}_{\mathbf{x}}+{\mathbf{R}}_{\mathbf{xy}}),\hfill \\ \hfill {\tilde{\mathbf{A}}}_{\mathbf{w}}& =& \frac{1}{2}{(\tilde{\mathbf{K}}-{\mathbf{R}}_{\mathbf{xn}})}^{\u22ba}\xb7\mathbf{Q}\xb7(2{\mathbf{I}}_{\mathbf{d}}+{\tilde{\mathbf{A}}}_{\mathbf{x}}),\hfill \\ \hfill {\tilde{\mathbf{B}}}_{\mathbf{w}}& =& {\tilde{\mathbf{J}}}_{\mathbf{w}}-{\mathbf{R}}_{\mathbf{n}}+\frac{1}{2}{(\tilde{\mathbf{K}}-{\mathbf{R}}_{\mathbf{xn}})}^{\u22ba}\xb7\mathbf{Q}\xb7{\tilde{\mathbf{B}}}_{\mathbf{x}},\hfill \\ \hfill {\tilde{\mathbf{C}}}_{\mathbf{w}}& =& -{\tilde{\mathbf{G}}}_{\mathbf{w}}-{\mathbf{R}}_{\mathbf{ny}}+\frac{1}{2}{(\tilde{\mathbf{K}}-{\mathbf{R}}_{\mathbf{xn}})}^{\u22ba}\xb7\mathbf{Q}\xb7{\tilde{\mathbf{C}}}_{\mathbf{x}},\hfill \\ \hfill {\tilde{\mathbf{A}}}_{\mathbf{y}}& =& \frac{1}{2}{({\tilde{\mathbf{G}}}_{\mathbf{x}}-{\mathbf{R}}_{\mathbf{xy}})}^{\u22ba}\xb7\mathbf{Q}\xb7(2{\mathbf{I}}_{\mathbf{d}}+{\tilde{\mathbf{A}}}_{\mathbf{x}}),\hfill \\ \hfill {\tilde{\mathbf{B}}}_{\mathbf{y}}& =& {({\tilde{\mathbf{G}}}_{\mathbf{w}}-{\mathbf{R}}_{\mathbf{ny}})}^{\u22ba}+\frac{1}{2}{({\tilde{\mathbf{G}}}_{\mathbf{x}}-{\mathbf{R}}_{\mathbf{xy}})}^{\u22ba}\xb7\mathbf{Q}\xb7{\tilde{\mathbf{B}}}_{\mathbf{x}},\hfill \\ \hfill {\tilde{\mathbf{C}}}_{\mathbf{y}}& =& {\tilde{\mathbf{J}}}_{\mathbf{y}}-{\mathbf{R}}_{\mathbf{y}}+\frac{1}{2}{({\tilde{\mathbf{G}}}_{\mathbf{x}}-{\mathbf{R}}_{\mathbf{xy}})}^{\u22ba}\xb7\mathbf{Q}\xb7{\tilde{\mathbf{C}}}_{\mathbf{x}},\hfill \end{array}$$

**Remark**

**A1 (Reduced explicit mapping).**

From the global inverse function theorem (see [34]), there exists an explicit mapping ${\mathbf{w}}_{\mathbf{N}}\left(k\right)={\mathbf{f}}^{-1}\phantom{\rule{-0.166667em}{0ex}}\left(\mathbf{p}\left(\mathbf{x}\left(k\right),\mathbf{u}\left(k\right)\right)\right)$ provided

$$det\left({(\tilde{\mathbf{K}}-{\mathbf{R}}_{\mathbf{xn}})}^{\u22ba}\xb7\mathbf{Q}\xb7\tilde{\mathbf{D}}\xb7(\tilde{\mathbf{K}}+{\mathbf{R}}_{\mathbf{xn}})\right)>0.$$

## Appendix B. Dictionary of Elementary Components

The dictionary is given in Table B1. We choose variables $\mathbf{x}$ and $\mathbf{w}$ so that matrices of the PHS (5) are canonical, that is, they do not involve any physical constants.

2-Ports | |||||

Storage | Diagram | $\mathit{x}$ | Stored Energy ${E}=\mathit{h}(\mathit{x})$ | Voltage $\mathit{v}$ | Current $\mathit{i}$ |

Inductance | ϕ | $\frac{{\varphi}^{2}}{2L}$ | $\frac{d\varphi}{dt}$ | $\frac{dh}{d\varphi}$ | |

Capacitance | q | $\frac{{q}^{2}}{2C}$ | $\frac{dh}{dq}$ | $\frac{dq}{dt}$ | |

Dissipative | Diagram | $\mathit{w}$ | Dissipated Power ${D}(\mathit{w})=\mathit{w}.\mathit{z}(\mathit{w})$ | Voltage $\mathit{v}$ | Current $\mathit{i}$ |

Resistance | i | $R.{i}^{2}$ | $z\left(w\right)$ | w | |

Conductance | v | ${v}^{2}/R$ | w | $z\left(w\right)$ | |

PN Diode | v | $v.{I}_{S}\left(exp\left(\frac{v}{\mu {v}_{0}}\right)-1\right)+{v}^{2}.{G}_{min}$ | w | $z\left(w\right)$ | |

3-Ports | |||||

Dissipative | Diagram | $\mathbf{w}$ | $\mathbf{z}(\mathbf{w})$ | ||

NPN Transistor | $\left(\begin{array}{c}{v}_{BC}\\ {v}_{BE}\end{array}\right)$ | $\left(\begin{array}{c}{i}_{BC}\\ {i}_{BE}\end{array}\right)=\left(\begin{array}{cc}{\alpha}_{R}& -1\\ -1& {\alpha}_{F}\end{array}\right).\left(\begin{array}{c}{I}_{S}\left({e}^{{v}_{BC}/{v}_{t}}-1\right)+{v}_{BC}.{G}_{min}\\ {I}_{S}\left({e}^{{v}_{BE}/{v}_{t}}-1\right)+{v}_{BE}.{G}_{min}\end{array}\right)$ | |||

Potentiometer | $\left(\begin{array}{c}{v}_{p1}\\ {i}_{p2}\end{array}\right)$ | $\left(\begin{array}{c}{i}_{p1}\\ {v}_{p2}\end{array}\right)=\left(\begin{array}{c}{v}_{p1}/(1+\alpha .{R}_{p})\\ {i}_{p2}.(1+(1-\alpha ).{R}_{p})\end{array}\right)$ |

#### Appendix B.1. Storage Components

Such components are defined by their storage function h associated with the constitutive laws of Table B1. In this paper, all storage components are linear dipoles. However, nonlinear components can also be considered if appropriate state and corresponding energy function can be found.

#### Appendix B.2. Linear Dissipative Components

The characteristics of dissipative components are algebraic relations on $\mathbf{w}$. Potentiometers are modeled as two time-varying resistors, the sum of which is ${R}_{p}$. To avoid 0 value of the resistors, 1 Ω have been added to those characteristics. The modulation parameter is $\alpha \in [0,1]$. As an example, in Table B1, we choose a conductance between ${N}_{1}$ and ${N}_{2}$, and a resistance between ${N}_{2}$ and ${N}_{3}$, so that ${\mathbf{w}}_{P}={[{v}_{p1},{i}_{p2}]}^{\u22ba}$.

#### Appendix B.3. Nonlinear Dissipative Components

PN junctions are modeled as voltage-controlled components by the Shockley equation: ${z}_{D}\left({w}_{D}\right)={i}_{D}={I}_{S}\left({e}^{\frac{{v}_{D}}{\mu {v}_{0}}}-1\right)$, where ${I}_{S}$ is the saturation current, μ is an ideality factor, and ${v}_{0}$ the reference voltage, specified for each diode type. Note that the passivity property is fulfilled (${\mathbf{z}}_{D}{\left({\mathbf{w}}_{D}\right)}^{\u22ba}.{\mathbf{w}}_{D}\ge 0$). As in LT-Spice simulators, a minimal conductance ${G}_{min}$ is added in Table B1. This helps convergence in the simulation process.

NPN junctions are passive 3-ports, with dissipated power ${\mathtt{D}}_{Q}={v}_{B}\xb7{i}_{B}+{v}_{C}\xb7{i}_{C}+{v}_{E}\xb7{i}_{E}\ge 0$. Here we use the Ebers–Moll model, which preserves this passivity property. ${I}_{S}$ is the saturation current, ${\beta}_{R}$ and ${\beta}_{F}$ are respectively the reverse and forward common emitter current gains, and ${v}_{t}$ is the thermal voltage. The corresponding voltage-controlled dissipative characteristic ${\mathbf{z}}_{Q}\left({\mathbf{w}}_{Q}\right)={[{i}_{BC},{i}_{BE}]}^{\u22ba}$ is given in Table B1, denoting ${\alpha}_{R}=\frac{{\beta}_{R}+1}{{\beta}_{R}}$, ${\alpha}_{F}=\frac{{\beta}_{F}+1}{{\beta}_{F}}$, and including minimal conductances. Note that ${\mathtt{D}}_{Q}={\mathbf{z}}_{Q}{\left({\mathbf{w}}_{Q}\right)}^{\u22ba}.{\mathbf{w}}_{Q}\ge 0$. In our final simulations, some resistors are added to model the resistance of contacts in the nonlinear components, choosing the same values as in LT-Spice models.

#### Appendix B.4. Incidence Matrices Γ

Incidence matrices for 2-ports, potentiometer P, and transistor Q with conventions of Table B1.

$${\Gamma}_{2-\mathrm{port}}=\begin{array}{cc}\hfill B& \\ \hfill \left(\begin{array}{c}\hfill 1\\ \hfill -1\end{array}\right)& \begin{array}{c}{N}_{1}\\ {N}_{2}\end{array}\end{array},{\Gamma}_{\mathrm{P}}=\begin{array}{c}\begin{array}{cc}{B}_{p1}& {B}_{p2}\end{array}\\ \hfill \left(\begin{array}{cc}1& 0\\ -1& 1\\ 0& -1\end{array}\right)& \begin{array}{c}{N}_{1}\\ {N}_{2}\\ {N}_{3}\end{array}\end{array},{\Gamma}_{\mathrm{Q}}=\begin{array}{c}\begin{array}{ccc}{B}_{BC}& {B}_{BE}& \end{array}\\ \hfill \left(\begin{array}{cc}1& 1\\ -1& 0\\ 0& -1\end{array}\right)& \begin{array}{c}{N}_{B}\\ {N}_{C}\\ {N}_{E}\end{array}\end{array}$$

## Appendix C. Discrete Gradient for Multi-Variate Hamiltonian

A generalization of the discrete gradient for multi-variate Hamiltonians such that Equations (20) and (21) are satisfied is given by replacing definition (22) by (see [46]):
with

$${[\nabla {\phantom{\rule{-0.166667em}{0ex}}}_{d}\mathcal{H}\left(\mathbf{x},\mathbf{x}+\delta \mathbf{x}\right)]}_{n}=\frac{{\Delta}_{n}\mathcal{H}(\mathbf{x},\mathbf{x}+\delta \mathbf{x})}{\delta {x}_{n}}.$$

$$\begin{array}{ccc}\hfill {\Delta}_{n}\mathcal{H}(\mathbf{x},\tilde{\mathbf{x}})& =& \phantom{\rule{0.277778em}{0ex}}\mathcal{H}({\tilde{x}}_{1},\cdots ,{\tilde{x}}_{n-1},{\tilde{x}}_{n},{x}_{n+1},\cdots ,{x}_{{n}_{S}})\hfill \\ & & -\mathcal{H}({\tilde{x}}_{1},\cdots ,{\tilde{x}}_{n-1},{x}_{n},{x}_{n+1},\cdots ,{x}_{{n}_{S}}).\hfill \end{array}$$

For mono-variate components, (22) and (C1) coincide and yield (discrete) constitutive laws that are insensible to the ordering of the state variables. For multi-variate components, this last property is lost, but can be restored by replacing (C2) by the averaged operator: ${\Delta}_{n}\mathcal{H}(\mathbf{x},\tilde{\mathbf{x}})=\frac{1}{{n}_{S}!}{\sum}_{\pi \in \mathcal{P}\left({n}_{S}\right)}{\Delta}_{n}{\mathcal{H}}^{\pi}({\mathbf{x}}_{\pi},{\tilde{\mathbf{x}}}_{\pi})$, where for all permutation $\pi \in \mathcal{P}\left({n}_{S}\right)$, ${\mathbf{x}}_{\pi}={({x}_{\pi \left(1\right)},\cdots ,{x}_{\pi \left({n}_{S}\right)})}^{\u22ba}$, and ${\mathcal{H}}^{\pi}\left({\mathbf{x}}_{\pi}\right)=\mathcal{H}\left(\mathbf{x}\right)$.

## References

- Bilbao, S. Sound Synthesis and Physical Modeling. In Numerical Sound Synthesis: Finite Difference Schemes and Simulation in Musical Acoustics; John Wiley & Sons Ltd.: Chichester, UK, 2009. [Google Scholar]
- Bilbao, S. Conservative numerical methods for nonlinear strings. J. Acoust. Soc. Am.
**2005**, 118, 3316–3327. [Google Scholar] [CrossRef] - Chabassier, J.; Joly, P. Energy preserving schemes for nonlinear Hamiltonian systems of wave equations: Application to the vibrating piano string. Comput. Methods Appl. Mech. Eng.
**2010**, 199, 2779–2795. [Google Scholar] [CrossRef] - Välimäki, V.; Pakarinen, J.; Erkut, C.; Karjalainen, M. Discrete-time modelling of musical instruments. Rep. Prog. Phys.
**2006**, 69, 1–78. [Google Scholar] [CrossRef] - Petrausch, S.; Rabenstein, R. Interconnection of state space structures and wave digital filters. IEEE Trans. Circuits Syst. II Express Br.
**2005**, 52, 90–93. [Google Scholar] [CrossRef] - Yeh, D.T.; Smith, J.O. Simulating guitar distortion circuits using wave digital and nonlinear state-space formulations. In Proceedings of the 1st International Conference on Digital Audio Effects (DAFx’08), Espoo, Finland, 1–4 September 2008; pp. 19–26.
- Fettweis, A. Wave digital filters: Theory and practice. Proc. IEEE
**1986**, 74, 270–327. [Google Scholar] [CrossRef] - Sarti, A.; De Poli, G. Toward nonlinear wave digital filters. IEEE Trans. Signal Process.
**1999**, 47, 1654–1668. [Google Scholar] [CrossRef] - Pedersini, F.; Sarti, A.; Tubaro, S. Block-wise physical model synthesis for musical acoustics. Electron. Lett.
**1999**, 35, 1418–1419. [Google Scholar] [CrossRef] - Pakarinen, J.; Tikander, M.; Karjalainen, M. Wave digital modeling of the output chain of a vacuum-tube amplifier. In Proceedings of the 12th International Conference on Digital Audio Effects (DAFx’09), Como, Italy, 1–4 September 2009; pp. 1–4.
- De Paiva, R.C.D.; Pakarinen, J.; Välimäki, V.; Tikander, M. Real-time audio transformer emulation for virtual tube amplifiers. EURASIP J. Adv. Signal Process.
**2011**, 2011, 1–15. [Google Scholar] [CrossRef] - Fettweis, A. Pseudo-passivity, sensitivity, and stability of wave digital filters. IEEE Trans. Circuit Theory
**1972**, 19, 668–673. [Google Scholar] [CrossRef] - Bilbao, S.; Bensa, J.; Kronland-Martinet, R. The wave digital reed: A passive formulation. In Proceedings of the 6th International Conference on Digital Audio Effects (DAFx-03), London, UK, 8–11 September 2003; pp. 225–230.
- Schwerdtfeger, T.; Kummert, A. A multidimensional approach to wave digital filters with multiple nonlinearities. In Proceedings of the 22nd European Signal Processing Conference (EUSIPCO), Lisbon, Portugal, 1–5 September 2014; pp. 2405–2409.
- Werner, K.J.; Nangia, V.; Bernardini, A.; Smith, J.O., III; Sarti, A. An Improved and Generalized Diode Clipper Model for Wave Digital Filters. In Proceedings of the 139th Convention of the Audio Engineering Society (AES), New York, NY, USA, 29 October–1 November 2015.
- Khalil, H.K. Nonlinear Systems; Prentice Hall: Upper Saddle River, NJ, USA, 2002; Volume 3. [Google Scholar]
- Cohen, I.; Helie, T. Real-time simulation of a guitar power amplifier. In Proceedings of the 13th International Conference on Digital Audio Effects (DAFx-10), Graz, Austria, 6–10 September 2010.
- Yeh, D.T.; Abel, J.S.; Smith, J.O. Automated physical modeling of nonlinear audio circuits for real-time audio effects—Part I: Theoretical development. IEEE Trans. Audio Speech Lang. Process.
**2010**, 18, 728–737. [Google Scholar] [CrossRef] - Borin, G.; De Poli, G.; Rocchesso, D. Elimination of delay-free loops in discrete-time models of nonlinear acoustic systems. IEEE Trans. Audio Speech Lang. Process.
**2000**, 8, 597–605. [Google Scholar] [CrossRef] - Hélie, T. Lyapunov stability analysis of the Moog ladder filter and dissipativity aspects in numerical solutions. In Proceedings of the 14th International Conference on Digital Audio Effects DAFx-11, Paris, France, 19–23 September 2011; pp. 45–52.
- Maschke, B.M.; Van der Schaft, A.J.; Breedveld, P.C. An intrinsic Hamiltonian formulation of network dynamics: Non-standard Poisson structures and gyrators. J. Frankl. Inst.
**1992**, 329, 923–966. [Google Scholar] [CrossRef] - Van der Schaft, A.J. Port-Hamiltonian systems: An introductory survey. In Proceedings of the International Congress of Mathematicians, Madrid, Spain, 22–30 August 2006; pp. 1339–1365.
- Stramigioli, S.; Duindam, V.; Macchelli, A. Modeling and Control of Complex Physical Systems: The Port-Hamiltonian Approach; Springer: Berlin, Germany, 2009. [Google Scholar]
- Marsden, J.E.; Ratiu, T.S. Introduction to Mechanics and Symmetry: A Basic Exposition of Classical Mechanical Systems; Springer: New Yor, NY, USA, 1999; Volume 17. [Google Scholar]
- Itoh, T.; Abe, K. Hamiltonian-conserving discrete canonical equations based on variational difference quotients. J. Comput. Phys.
**1988**, 76, 85–102. [Google Scholar] [CrossRef] - Tellegen, B.D.H. A general network theorem, with applications. Philips Res. Rep.
**1952**, 7, 259–269. [Google Scholar] - Desoer, C.A.; Kuh, E.S. Basic Circuit Theory; Tata McGraw-Hill Education: Noida, India, 2009. [Google Scholar]
- Karnopp, D. Power-conserving transformations: Physical interpretations and applications using bond graphs. J. Frankl. Inst.
**1969**, 288, 175–201. [Google Scholar] [CrossRef] - Breedveld, P.C. Multibond graph elements in physical systems theory. J. Frankl. Inst.
**1985**, 319, 1–36. [Google Scholar] [CrossRef] - Falaize, A.; Hélie, T. Guaranteed-passive simulation of an electro-mechanical piano: A port-Hamiltonian approach. In Proceedings of the 18th International Conference on Digital Audio Effects (DAFx), Trondheim, Norway, 30 November–3 December 2015.
- Falaize, A.; Lopes, N.; Hélie, T.; Matignon, D.; Maschke, B. Energy-balanced models for acoustic and audio systems: A port-Hamiltonian approach. In Proceedings of the Unfold Mechanics for Sounds and Music, Paris, France, 11–12 September 2014.
- Vladimirescu, A. The SPICE Book; John Wiley & Sons, Inc.: New Yor, NY, USA, 1994. [Google Scholar]
- Diestel, R. Graph Theory, 4th ed.; Springer: New Yor, NY, USA, 2010. [Google Scholar]
- Do Carmo, M.P. Differential Geometry of Curves and Surfaces; Prentice-Hall: Englewood Cliffs, NJ, USA, 1976; Volume 2, p. 131. [Google Scholar]
- Hairer, E.; Lubich, C.; Wanner, G. Geometric Numerical Integration: Structure-Preserving Algorithms for Ordinary Differential Equations; Springer Science & Business Media: New York, NY, USA, 2006; Volume 31. [Google Scholar]
- Oppenheim, A.V.; Schafer, R.W. Discrete-Time Signal Processing, 3rd ed.; Pearson Higher Education: San Francisco, CA, USA, 2010. [Google Scholar]
- Jones, E.; Oliphant, E.; Peterson, P. SciPy: Open Source Scientific Tools for Python, function scipy.optimize.root. Available online: http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.root.html#scipy.optimize.root (accessed on 22 September 2016).
- Falaize, A. A comparison of numerical methods. Available online: http://recherche.ircam.fr/anasyn/falaize/applis/comparisonnumschemes/ (accessed on 22 September 2016).
- Yeh, D.T. Automated physical modeling of nonlinear audio circuits for real-time audio effects—Part II: BJT and vacuum tube examples. IEEE Trans. Audio Speech Lang. Process.
**2012**, 20, 1207–1216. [Google Scholar] [CrossRef] - Butcher, J.C. Numerical Methods for Ordinary Differential Equations; John Wiley & Sons, Ltd.: London, UK, 2008; p. 26. [Google Scholar]
- Holters, M.; Zölzer, U. Physical Modelling of a Wah–Wah Effect Pedal as a case study for Application of the nodal DK Method to circuits with variable parts. In Proceedings of the 14th International Conference on Digital Audio Effects (DAFx-11), Paris, France, 19–23 September 2011.
- Falaize-Skrzek, A.; Hélie, T. Simulation of an analog circuit of a wah pedal: A port-Hamiltonian approach. In Proceedings of the 135th Convention of the Audio Engineering Society, New York, NY, USA, 17–20 October 2013.
- Steinberg Media Technologies GmbH. Virtual Studio Technology. Available online: http://www.steinberg.net/en/company/technologies/vst3.html (accessed on 22 September 2016).
- ROLI Ltd. The JUCE framework. Available online: http://www.juce.com (accessed on 22 September 2016).
- Falaize, A. Companion web-site to the present article entitled "Passive Guaranteed Simulation of Analog Audio Circuits: A port-Hamiltonian Approach". Available online: http://recherche.ircam.fr/anasyn/falaize/applis/analogcircuits/ (accessed on 22 September 2016).
- Aoues, S. Schémas d’intégration dédiés à l’étude, l’analyse et la synthèse dans le formalisme Hamiltonien à ports. Ph.D. Thesis, INSA, Lyon, France, December 2014; pp. 32–35. [Google Scholar]

**Figure 2.**Schematic and corresponding graph of a simple bipolar-junction transistor (BJT) amplifier with feedback. The grey part corresponds to the components, and the outer elements correspond to the external ports, or sources (as in Table 1).

**Figure 3.**Definitions and orientations for a single current-controlled edge b from node i to node j, with nodes potentials ${e}_{i}$ and ${e}_{j}$, respectively. The knowledge of the potential ${e}_{j}$ is transferred to node i with ${e}_{i}={v}_{b}+{e}_{j}$.

**Figure 4.**Simulation results and comparison of the methods in Table 3, for a nonlinear conservative system $\frac{\mathtt{d}\mathbf{x}}{\mathtt{d}t}={\mathbf{J}}_{\mathbf{x}}\xb7\nabla \mathcal{H}\left(\mathbf{x}\right)$ with $\mathcal{H}\left(\mathbf{x}\right)$ given in (30): (

**a**) Trapezoidal rule; (

**b**) Midpoint rule; (

**c**) PHS combined with discrete gradient; (

**d**) Relative error on energy balance.The comparison measure is the relative error on the power balance defined by $\u03f5\left(k\right)=\frac{\left|\mathcal{H}\left(\mathbf{x}(k+1)\right)-\mathcal{H}\left(\mathbf{x}\left(k\right)\right)\right|}{\mathcal{H}\left(\mathbf{x}\left(0\right)\right)}$. We see from Figure 4d that the error associated with the proposed method (PHS approach combined with the discrete gradient method) is low compared to the two other methods (with machine precision ≃10${}^{-16}$). The accumulation of these errors is responsible for the apparently unstable behavior of the trapezoidal rule.

**Figure 5.**Simulation (Figure 5b) of a dissipative diode clipper (Figure 5a) at the sample rate ${f}_{s}=96$ kHz, with three Newton–Raphson iterations, for a 10 ms sinusoidal excitation at 1 kHz with linearly increasing amplitude between 0 V and 2 V. (

**a**) Diode clipper schematic; (

**b**) Simulation of the diode clipper of Figure 5a.

**Figure 6.**Simulation (Figure 6b) of the common-emitter bipolar-junction transistor (BJT) amplifier with feedback (Figure 6a) at the sample rate ${f}_{s}=384$ kHz, with 10 Newton–Raphson iterations, for a 10 ms sinusoidal excitation at 1 kHz with linearly increasing amplitude between 0 V and $0.2$ V. (

**a**) Schematic of a common-emitter BJT amplifier with feedback; (

**b**) Simulation of the BJT amplifier in Figure 6a.

**Figure 7.**Results for the common-emitter BJT amplifier with feedback (Figure 6a). The input voltage signal is a 4 s exponential chirp on the audio range (20 Hz–20 kHz) with amplitude $0.05$ V (logarithmic frequency scale). Simulation starts at 0.3 s (after the switching transient). (

**a**) Spectrogram of output ${v}_{\mathrm{OUT}}$ obtained with the proposed method; (

**b**) Spectrogram of output ${v}_{\mathrm{OUT}}$ obtained with LT-Spice.

**Figure 8.**Simulations of the Cry-Baby’s circuit of Figure 9, for the potentiometer parameter $\alpha =0$ (

**a**) and $\alpha =1$ (

**b**) in the frequency domain, compared with LT-Spice simulations on the audio range 20 Hz–20 kHz.

**Figure 9.**Schematic of the Cry-Baby wah pedal. Note the IN/OUT terminals and the 9 V supply. The potentiometer $\mathtt{P}$ controls the effect.

Line | Label | Node List | Type | Parameters |
---|---|---|---|---|

${\ell}_{1}$ | $C1$ | ${N}_{1},{N}_{2}$ | CapaLin | $20{e}^{-9}$ |

${\ell}_{2}$ | $R1$ | ${N}_{3},{N}_{4}$ | Resistor | $1.5{e}^{3}$ |

${\ell}_{3}$ | $Q1$ | ${N}_{2},{N}_{3},{N}_{5}$ | NPN_ Type1 | List of parameters |

${\ell}_{4}$ | Vcc | ${N}_{4}$ | Voltage | 9 |

${\ell}_{5}$ | IN | ${N}_{1}$ | Voltage | ∼ |

${\ell}_{6}$ | OUT | ${N}_{3}$ | Current | 0 |

${\ell}_{7}$ | GRD | ${N}_{5}$ | Voltage | 0 |

Component type | Current-Controlled | Voltage-Controlled |
---|---|---|

${[\mathbf{a}]}_{\mathit{b}}={\mathit{i}}_{\mathit{b}}$ | ${[\mathbf{a}]}_{\mathit{b}}={\mathit{v}}_{\mathit{b}}$ | |

${[\mathbf{b}]}_{\mathit{b}}={\mathit{v}}_{\mathit{b}}$ | ${[\mathbf{b}]}_{\mathit{b}}={\mathit{i}}_{\mathit{b}}$ | |

storages | capacitor | inductor |

resistors | resistance | conductance |

nonlinear | diodes, transistors | |

sources | voltage source | current source |

Method | Update |
---|---|

Trapezoidal rule | $\mathbf{x}(k+1)=\mathbf{x}\left(k\right)+T\xb7{\mathbf{J}}_{\mathbf{x}}\xb7\frac{\nabla \mathcal{H}\left(\mathbf{x}\left(k\right)\right)+\nabla \mathcal{H}\left(\mathbf{x}(k+1)\right)}{2}$ |

Midpoint rule | $\mathbf{x}(k+1)=\mathbf{x}\left(k\right)+T\xb7{\mathbf{J}}_{\mathbf{x}}\xb7\nabla \mathcal{H}\left(\frac{\mathbf{x}\left(k\right)+\mathbf{x}(k+1)}{2}\right)$ |

PHS with discrete gradient | $\mathbf{x}(k+1)=\mathbf{x}\left(k\right)+T\xb7{\mathbf{J}}_{\mathbf{x}}\xb7{\nabla}_{d}\mathcal{H}\left(\mathbf{x}\left(k\right),\mathbf{x}(k+1)\right)$ |

© 2016 by the authors; licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC-BY) license (http://creativecommons.org/licenses/by/4.0/).