A Variational Formulation of Nonequilibrium Thermodynamics for Discrete Open Systems with Mass and Heat Transfer

We propose a variational formulation for the nonequilibrium thermodynamics of discrete open systems, i.e., discrete systems which can exchange mass and heat with the exterior. Our approach is based on a general variational formulation for systems with time-dependent nonlinear nonholonomic constraints and time-dependent Lagrangian. For discrete open systems, the time-dependent nonlinear constraint is associated with the rate of internal entropy production of the system. We show that this constraint on the solution curve systematically yields a constraint on the variations to be used in the action functional. The proposed variational formulation is intrinsic and provides the same structure for a wide class of discrete open systems. We illustrate our theory by presenting examples of open systems experiencing mechanical interactions, as well as internal diffusion, internal heat transfer, and their cross-effects. Our approach yields a systematic way to derive the complete evolution equations for the open systems, including the expression of the internal entropy production of the system, independently on its complexity. It might be especially useful for the study of the nonequilibrium thermodynamics of biophysical systems.


Introduction
The goal of this paper is to present a variational formulation for the nonequilibrium thermodynamics of open discrete (finite dimensional) systems, i.e., systems which may exchange heat as well as matter with their surrounding. Such systems, out of equilibrium, appear naturally in engineering, physics, biology, chemistry, social science, the archetypal example being the case of the living cell. A first step in the study of the nonequilibrium thermodynamics of a given system is the determination of the complete set of evolution equations, written as a system of ordinary differential equations (ODEs) for discrete systems, which allows to determine the values of all the variables of the system, at all times. Such a step is nontrivial, even for closed systems, especially in the case when several irreversible processes of various nature interact with each other. A celebrated example being the case of the adiabatic piston, whose final configuration can only be determined by solving the ODEs of evolution, see [1].
In our previous works [2,3], we developed a Lagrangian variational formulation for the nonequilibrium thermodynamics of closed discrete systems by following Stueckelberg's phenomenological approach, in which we extended the Lagrange-d'Alembert principle for nonlinear nonholonomic mechanics to encode the internal entropy production due to irreversible processes into a class of nonlinear constraints of thermodynamic type. Such a variational approach is based on two types of constraints, a phenomenological constraint imposed on the solution curve and a variational constraint imposed on the variations to be considered. As shown in [2,3], these two constraints are related to each other in a very systematic way. In addition, our approach gives a form of the entropy production equation that is extremely well-suited for the choice of the phenomenological relation between the thermodynamic fluxes and the thermodynamic forces, in accordance with the second law of thermodynamics. To obtain such a systematic variational framework, the notion of thermodynamical displacements is introduced, which enables us to systematically obtain the variational constraint from the phenomenological constraint. As shown in [2,3], the stationarity condition for a certain action functional involving the total Lagrangian of the system yields the complete evolution equations for all the variables of the system. The variational formulation in [2,3] is illustrated with several examples of discrete systems such as mechanical systems with friction, electric circuits with resistor, chemical reactions, diffusions across membranes, etc.
In this paper, we extend this variational formulation of nonequilibrium thermodynamics to the case of discrete open systems. Passing from closed to open systems introduces an important change in the constraints to be used, namely, the constraints generically become explicitly time-dependent, due to the power transfer associated to matter and heat exchange with the exterior through the ports. More precisely, we have to extend our phenomenological constraint to a kinematic constraint by taking into account of the time-dependent boundary conditions associated to exchanges with the exterior for the derivation of the entropy production. To reach this goal, we first propose a novel theory of Lagrangian variational formulation for systems with time-dependent nonlinear nonholonomic constraints, in which the kinematic and variational constraints are related in a very specific way. When applying this variational formulation to open thermodynamic system, the constraint is given by the equation for the rate of entropy production of the system. Our approach can handle systems with several ports, experiencing mechanical interactions, internal diffusion, internal heat transfer, and their cross-effects.
The variational approach presented in this paper enables an effective derivation of the complete evolution equations for all the variables of the open system. It thus represents an essential modeling tool for the dynamics of open systems, in the same way as the Hamilton principle in Lagrangian mechanics (see, e.g., [4], for a recent use of the Hamilton variational principle as a modeling tool for fluid-structure interaction) and its Lagrange-d'Alembert version for nonholonomic mechanical systems. A preliminary illustration of this variational feature is considered in Section 3.3, where we propose a model for an open system which takes into account of the exchange of mechanical momentum in the dynamics and in the entropy production equation. We project to extensively exploit our formulation as a modeling tool for the nonequilibrium thermodynamics of open systems in the future. Besides its modeling purpose, our approach is also useful for the derivation of structure preserving numerical schemes for nonequilibrium thermodynamics, based on discrete versions of the variational formulation, in a similar way with the case of variational integrators in classical mechanics [5]. We made initial steps in this direction in [6]. The description of the nonequilibrium thermodynamics used in this paper assumes the local thermodynamic equilibrium condition.
Our paper is organized as follows. In Section 2, we review the fundamental ingredients for the nonequilibrium thermodynamic of discrete open systems by following Stueckelberg's formulation of the two laws. In Section 3, we first introduce an abstract variational formulation for systems with nonlinear time-dependent nonholonomic constraints. Then, we present the simplest case of an open system with a single chemical component in a single compartment with matter transfer through one entry port. Further, we explore the case of a discrete open system with a single chemical component experiencing internal diffusion through several compartments, as well as matter transfer through multi-entry ports, without heat transfer. We illustrate our theory with some examples such as an open system of flowing fluid carrying kinetic energy into a piston device, an open system with a single chemical component experiencing diffusion between two compartments and transfer into or out of the system through two ports, as well as open systems with diffusion of a single chemical species through a composite membrane made of both serial and parallel arrays of several elements. In Section 4, we investigate the more general class of discrete open systems, having several entropy variables, and hence several temperatures. For instance, we consider a single chemical component in several compartments, experiencing internal diffusion, heat transfer, the associated cross-effects, as well as matter exchange through multi-entry ports, and external heating. We illustrate our variational formulation with several examples such as an open system experiencing diffusion and heat conduction between two compartments, and an open system with heat conduction and diffusion of a single chemical species through a composite membrane made of a parallel array of several elements. In Section 5, some concluding remarks are given as well as future directions.

Simple Discrete Open Systems
In this paper, we shall follow the phenomenological approach to nonequilibrium thermodynamics developed by Stueckelberg [7], which provides a macroscopic dynamic theory for accounting for irreversible processes. Such an approach enables the treatment of nonequilibrium thermodynamics as a natural extension of classical mechanics. Before going into details, we make a brief review on the fundamental notions that we employ by following Stueckelberg's approach (see, for instance, [1,8,9]), where two state functions, the energy and the entropy, are used to obey the two fundamental laws of thermodynamics, which are formulated as first order differential equations.
Let us denote by Σ a physical system and by Σ ext its exterior. The state of the system is defined by a set of mechanical variables and a set of thermal variables. State functions are functions of these variables. Stueckelberg's formulation of the two laws is given as follows.
First law: For every system Σ, there exists an extensive scalar state function E, called energy, which satisfies d dt where t denotes time, P ext W is the power associated to the work done on the system, P ext H is the power associated to the transfer of heat into the system, and P ext M is the power associated to the transfer of matter into the system (As we will recall below, to matter transfer is associated a transfer of work and heat. By convention, P ext W and P ext H denote uniquely the power associated to work and heat that is not associated to a transfer of matter.). Given a thermodynamic system, the following terminology is generally adopted: • A system is said to be closed if there is no exchange of matter, i.e., P ext M (t) = 0. When P ext M (t) = 0 the system is said to be open.

•
A system is said to be adiabatically closed if it is closed and there is no heat exchanges, i.e., P ext A system is said to be isolated if it is adiabatically closed and there is no mechanical power exchange, i.e., P ext From the first law, it follows that the energy of an isolated system is constant.
Second law: For every system Σ, there exists an extensive scalar state function S, called entropy, which obeys the following two conditions (see [7], p. 23).
(a) Evolution part: If the system is adiabatically closed, the entropy S is a non-decreasing function with respect to time, i.e., d dt where I(t) is the entropy production rate of the system accounting for the irreversibility of internal processes.
(b) Equilibrium part: If the system is isolated, as time tends to infinity the entropy tends towards a finite local maximum of the function S over all the thermodynamic states ρ compatible with the system, i.e., By definition, the evolution of an isolated system is said to be reversible if I(t) = 0, namely, the entropy is constant. In general, the evolution of a system Σ is said to be reversible, if the evolution of the total isolated system with which Σ interacts is reversible.

Expression of the first law for open systems.
Consider an open system with several ports, a = 1, ..., A, through which matter can flow into or out of the system. We suppose, for simplicity, that the system involves only one chemical species and denote by N the number of moles of this species. The mole balance equation is where J a is the molar flow rate into the system through the a-th port, so that J a > 0 for flow into the system and J a < 0 for flow out of the system. As matter enters or leaves the system, it carries its internal, potential, and kinetic energy. This energy flow rate at the a-th port is the product E a J a of the energy per mole (or molar energy) E a and the molar flow rate J a at the a-th port. In addition, as matter enters or leaves the system it also exerts work on the system that is associated with pushing the species into or out of the system. The associated energy flow rate is given at the a-th port by p a V a J a , where p a and V a are the pressure and the molar volume of the substance flowing through the a-th port.
From this we can write the first law of thermodynamics as We refer, for instance, to [10,11] for the detailed explanations of the first law for open systems. We recall below the definition of simple systems, which plays a main role in our variational formulation.
Discrete simple systems. A discrete system Σ is a collection Σ = ∪ N A=1 Σ A of a finite number of interacting simple systems Σ A . By definition, following [7], a simple system Σ is a macroscopic system for which one (scalar) thermal variable and a finite set of mechanical variables are sufficient to describe entirely the state of the system. From the second law of thermodynamics, we can always choose the thermal variable as the entropy S.

Variational Formulation of Discrete Open Simple Systems
In this section, we develop a variational formulation for discrete open thermodynamic systems that are assumed to be simple. As recalled above, such systems are described by a single entropy variable and, therefore, a uniform temperature can be attributed to the system. The case of non-simple systems will be treated in Section 4.
We start in Section 3.1 by introducing a general variational approach for the treatment of time-dependent nonholonomic systems with nonlinear constraints. This abstract variational formulation extends to the time dependent case the one developed in [2] for closed thermodynamic systems and provides the general framework for the variational formulation for open systems. In Section 3.2 we develop the variational formulation for open simple systems in a particular setting, before extending it to include mechanical interactions in Section 3.3 and internal diffusion in Section 3. 4. Several examples are treated in Section 3.6, while Section 3.5 tightens the link between the variational formulation for open systems and the abstract formulation presented in Section 3.1.

A Variational Formulation for Time-Dependent Nonlinear Nonholonomic Systems
In this section, we present an abstract variational framework for the treatment of open thermodynamic systems later. The variational formulation consists in computing the critical curve of an action functional by imposing two kinds of constraints: a variational constraint which imposes constraints on the variations of the curve, and a kinematic constraint which imposes constraints on the critical curves. These two constraints are related in a very specific way. This setting extends the variational formulation proposed in [2,3,12] for closed thermodynamic systems. In the particular case of linear constraints, the variational formulation recovers the Lagrange-d'Alembert principle for nonholonomic mechanics, see, e.g., [13]. A general variational setting for mechanics with nonlinear constraints is described in [14,15]. For the variational formulation of time-dependent nonholonomic mechanics with affine constraints, see [16].
Let Q be a configuration manifold with local coordinates q = (q 1 , ..., q n ) and let R × Q be the extended configuration manifold which includes the time variable t ∈ R. We consider the vector bundle R × TQ over R × Q with local coordinates (t, q, v) and the vector bundle Such a constraint defines for each (t, q, v) ∈ R × TQ an affine subspace C V (t, q, v) ⊂ T q Q. By considering, for each (t, q, v), the linear subspace C V (t, q, v) associated to C V (t, q, v), we get the time-dependent variational constraint Given C V , we define the associated nonlinear nonholonomic kinematic constraint C K ⊂ R × TQ by We note that the function A r i and B r may depend explicitly on the time t. Let L be a time-dependent Lagrangian defined on R × TQ and let F ext : R × TQ → T * Q be an external force field assumed to be fiber preserving, i.e., F ext (t, q, v) ∈ T * q Q. We consider the following Lagrange-d'Alembert principle with time-dependent nonlinear constraints and external force field: with δq(t 1 ) = δq(t 2 ) = 0, and where the curve q(t) satisfies the the nonlinear constraint C K , i.e, By a direct computation, using Lagrange multipliers λ r , r = 1, ..., m, we get that a curve q(t) is critical for the variational formulations (2)-(4) if and only if it is a solution of the time-dependent nonlinear nonholonomic Lagrange-d'Alembert equations with external forces: Associated to the Lagrangian L(t, q,q), we define the energy E(t, q,q) on R × TQ as Along a solution of the evolution Equation (5), we get the energy balance equation As we shall show later, the variational formulation for discrete open thermodynamic systems falls into the abstract variational setting (2)-(4). In this case, the system (5) yields the complete evolution equations for the thermodynamic system, in which the nonlinear constraint gives the entropy equation, in agreement with the second law of thermodynamics. The energy balance Equation (6) yields the first law of thermodynamics.

Foundations of the Variational Formulation for Discrete Open Thermodynamic Systems
In order to introduce our variational formulation, we shall first consider a particular case of simple discrete system, namely, the case of a system with a single chemical component N in a single compartment with constant volume V. Recall that, since the system is assumed to be simple, there is a single entropy S attributed to the whole system. Non-simple systems will be considered in Section 4. We assume that there is no heat and work exchanges, except the ones associated to the transfer of matter. We also ignore all the mechanical effects, which will be included later in Section 3.3. In this particular situation, the energy of the system is given by the internal energy written as U = U(S, N), since V = V 0 is constant. The balance of mole and the balance energy, i.e., the first law, are respectively given by see (1), where H a = U a + p a V a is the molar enthalpy at the a-th port and where U a , p a , and V a are respectively the molar internal energy, the pressure and the molar volume at the a-th port. From these equations and the second law, one obtains the equations for the rate of change of the entropy of the system as d dt where S a is the molar entropy at the a-th port and I is the rate of internal entropy production of the system given by with T = ∂U ∂S the temperature and µ = ∂U ∂N the chemical potential. For our variational treatment, it is useful to rewrite the rate of internal entropy production as where we defined the entropy flow rate J a S := S a J a and also used the relation H a = U a + p a V a = µ a + T a S a . The thermodynamic quantities known at the ports are usually the pressure and the temperature p a , T a , from which the other thermodynamic quantities, such as µ a = µ a (p a , T a ) or S a = S a (p a , T a ) are deduced from the state equations of the gas.

Remark 1.
There are several representations for the rate of internal entropy production given in (8), namely, we can rewrite I as as well as Variational formulation. Let W, Γ, and Σ be the molar and thermal displacements and the internal entropy of the system respectively, each of whose interpretation will be explained later. Given the molar flow rates J a , the entropy flow rates J a S , the temperatures T a , and the chemical potentials µ a at the ports a = 1, ..., A, we consider the following variational formulation: where the curves satisfy the nonlinear nonholonomic constraint and with respect to variations subject to the constraint with δW(t 1 ) = δW(t 2 ) = 0.

Remark 2.
We call the internal entropy production (10) the kinematic constraint, where the internal entropy production, in general, is described not only by the phenomenological relations obtained experimentally but also by the pressures, temperatures and the molar flow rates at the external ports. This equation is generally a nonlinear nonholonomic constraint imposed on the curve of the state variables in the variational formulation, see (4). We will later explain in details how the variational formulations (9)-(11) arises as a special instance of the abstract variational formulations (2)-(4).

Remark 3.
In the variational formulations given in (9)-(11), we employed the new variables, Γ and W, such thaṫ Γ = T andẆ = µ, where we interpreted the variable Γ as the thermal displacement and the variable W as the molar displacement. These are examples of thermodynamic displacements, as introduced in [2,3], whose rate of change coincide with the affinity of the irreversible process. In particular, note that Γ is a monotonically increasing real function of time t since the temperature T takes positive real values. Note also that the notion of thermal displacement was first used by [17] and in the continuum setting by [18]. Refer to the Appendix of [19] for an historical account. The variables Γ and W are conjugate variables with S (or Σ) and N, respectively. Moreover, we used another new variable Σ, which is not the same object as the entropy of the system S but it denotes the internal entropy of the system. In fact,Σ indicates the internal entropy production, withΣ ≥ 0, sometimes denoted as d i S/dt in the standard literature (see, for instance, Equation (8) on page 21 in [20]), while the total rate of entropy production of the system,Ṡ, does not need to be positive if the system is not adiabatically closed.
Comments and structure of the variational formulation. Before applying the variational formulation to derive the equations, we first comment on the variables and functions involved as well as on the structure of this formulation. The variational formulations (9)- (11) gives conditions on the curve c(t) = Γ(t), W(t), N(t), S(t), Σ(t) that ultimately gives a system of ODEs for this curve. This curve involves the thermodynamic quantities associated to the system, namely, the number of moles N(t), the total entropy S(t) and the internal entropy Σ(t) of the system, as well as the thermodynamic displacements Γ(t), W(t) whose interpretation will be explained below. Recall that the knowledge of a fundamental state equation of the system, such as U = U(S, V, N), allows one to deduce all the other thermodynamic relations. The thermodynamic quantities associated to the ports a = 1, ..., A, are denoted with an exponent a, such as T a and µ a . They satisfy the same relations as the corresponding quantities of the system, because they represent the same species, i.e., are obtained from the same state equation. Given a port a, if matter is flowing into the system through that port from the exterior, then we will assume that the thermodynamic quantities are known from the experimenter, i.e., they are prescribed functions of time such as T a = T a (t) and µ a = µ a (t), which characterise the thermodynamic properties of the matter flowing into the system. On the other hand, if matter is flowing out of the system through that port, then the associated thermodynamic quantities coincide with the thermodynamic quantities of the system, such as, T a = T, µ a = µ. In this case, these quantities are not assumed to be known, they are computed via the evolution equations. If matter is flowing in, we shall assume that the molar flow rate J a > 0 is a prescribed function of time, i.e., J a = J a (t). If matter is flowing out of the system then J a < 0 is usually computed from the other variables and the physical properties of the system. In the system considered here, since there is no additional heating from the exterior, the entropy flow rate J a S is given in terms of the molar flow rate J a as J a S = S a J a , where S a is the molar entropy at the a-th port. We note that the variational constraint (11) follows from the phenomenological constraint (10) by formally replacing the rates by the corresponding virtual displacements, i.e.,ẋ → δx, and by removing all the terms that depend uniquely on the ports a = 1, ..., A, i.e., the terms µ a J a and T a J a S . Such a systematic correspondence between the phenomenological and variational constraints, was already verified for the variational formulation of closed systems, see [2,3]. It is extended here to the case of open systems. Note that the action functional in (9) has the same form as that in the case of closed systems, see [2]. As will be shown later, this systematic relation between the two constraints still holds in the more general cases of discrete open systems considered below.
Application of the variational formulation. By taking the variation of the integral in (9) and using the variational constraint (11), we get the following evolution equations: From the first and third equations in (12), temperature is obtained as the rate of the thermal displacement Γ while the chemical potential as the rate of the molar displacement W, since the temperature and the chemical potential of the system are defined by T := ∂U ∂S and µ := ∂U ∂N . By inserting the conditions (12) into the phenomenological constraint (10), we get the desired entropy equation in terms of T and µ, namely in accordance with (7) and (8). From the entropy equation and the last relation in (12), we have the rate of internal entropy production of the system as To summarize, from the variational formulations (9)-(11), we have obtained the following evolution equations for the open system with A ports: From this system, the first law is computed as Equations (14) yield a system of two ODEs for the two variables S(t) and N(t), for given molar flow rates J a (t). The evolution of the other variables, such as p(t), T(t), µ(t), can be computed from S(t) and N(t) by using the state equation. In some situations, the molar flow rates are chosen in order the system to respect some properties, such as, for example a constant pressure p(t) = p 0 .
Example 1 (Filling an Insulated Tank with Pressurized Gas). In order to illustrate our formalism, we consider an insulated tank containing an amount of N moles of ideal gas at temperature T and pressure p. We assume that gas with temperature T 1 (t) and pressure p 1 (t), which are given prescribed functions of time t, is entering the tank at a single port with the molar flow rate J 1 (t), ([21], Section 8).
From the fundamental state equation of the ideal gas U = U(S, V, N), all the thermodynamic relations can be obtained. For instance, we have where R is the universal gas constant, c V , c p are the heat capacity coefficients at constant volume and constant pressure of the ideal gas, and the index () ref indicates reference values. The constants R, c V , and c p are expressed in units J K·mole . Note that U, V, S denote the molar quantities associated to U, V, S. The above variational formulation yields the equations of motioṅ where, for an ideal gas, I is computed as This expression recovers the entropy production for this system, see, e.g., ([21] Equation (8.74)). It is associated to the irreversible process of mixing ideal gas at different temperature and pressure. By rewriting the entropy production as we see that I ≥ 0 for all T, T 1 , and for p 1 ≥ p. The second law thus consistently requires that p 1 ≥ p.

Variational Formulation of Open Simple Systems with Mechanical Interactions
We shall now extend the previous variational formulation to the case of an open system including exchange of mechanical momenta. Such a system is described by two kind of mechanical variables. Mechanical variables q = (q 1 , ..., q n ) describe the motion of the mechanical devices involved in the system, while x = (x 1 , ..., x m ) are mechanical variables which indicate the motion of the species of the system. Through each of the A ports, we have the flow rates J a , J a x , and J a S , a = 1, ..., A, corresponding to exchange of matter, mechanical momentum, and entropy. We assume that the species flows through the a-th port at velocity v a = (v a,1 , ..., v a,m ). We also assume that the mechanical components are subject to friction and external forces, respectively, denoted by F fr x , F fr q and F ext x , F ext q .
Application of the variational formulation. By taking the variation of the integral in (16) and using the variational constraint (18), we get the following conditions: Extending the earlier definitions, we define the temperature and the chemical potential of the system in tersm of the Lagrangian as T := − ∂L ∂S and µ := − ∂L ∂N . By inserting the conditions (19) into the phenomenological constraint (17), we get the following entropy equation To summarize, from the variational formulations (16)-(18), we have obtained the following evolution equations for the open system with mechanical interaction and A ports: This system of ODEs completely determines the time evolution of all the variables. In absence of thermodynamic effects, it reduces to the Euler-Lagrange equations of classical mechanics. Defining the total energy of the system as we have the energy balance Example 2 (Flowing Fluid Carrying Kinetic Energy into a Piston Device). We consider a piston with mass M moving in a cylinder containing a species with internal energy U = U(S, V, N). We assume that the cylinder has A ports through which species is injected into or flows out of the cylinder with molar flow rates J a and velocities v a . The momentum and entropy flow rates accompanying these exchanges of species are given by where M 0 is the molecular weight of the species. We assume that the species flows through the a-th port and in the cylinder in a one dimensional direction, i.e., v a is parallel to v =ẋ, for each a = 1, ..., A, as illustrated in Figure 1. The variable q characterizes the one-dimensional motion of the piston, such that the volume occupied by the species is V = Aq, with A the sectional area of the cylinder. The Lagrangian of the system is the sum of the kinetic energy of the piston and the species, to which is subtracted the internal energy of the species L(q,q, x,ẋ, S, N) = 1 2 By a direct application of the variational formulation, the evolution equations are obtained aṡ where p = − ∂U ∂V is the pressure, together with the entropy Equation (20) which yields where we used the expression of the temperature T := − ∂L ∂S = ∂U ∂S and the expression of the chemical potential By translational invariance, the frictions forces verify F fr q = −F fr x , thus from this equation and the last equation in (19), we get the rate of entropy production within the system as where the first term represents the entropy production associated to the friction experiencing by the moving piston and the moving species, the second term is the entropy production associated to the mixing of gas at different velocities, and the last term is the entropy production associated to the mixing of gas at different pressure, and temperature. The second law requires that each of these terms is positive. Concerning the first term, the friction force must be dissipative and can be chosen as F fr q = −λ(q −ẋ), where the phenomenological coefficient λ(q, x, S) is determined experimentally, see [1]. The second term is positive as long as J a > 0 and it is zero if J a < 0 since in this case v a = v. Finally, the last term has been already analyzed in the previous example in the case of the ideal gas, see (15).
The energy balance is deduced from (22) as where E a = 1 2 M 0 (v a ) 2 + U a denotes the total (kinetic plus internal) molar energy of the species flowing at the a-th port. This balance of energy is consistent with the general expression of the first law in (1). Potential energies can be easily included in this example. External heating will be considered in Section 4.

Variational Formulation of an Open Simple System with Internal Matter Diffusion
In this subsection, we extend the previous variational formulation to the case of open discrete systems experiencing internal diffusion processes. Diffusion is particularly important in biology where many processes depend on the transport of chemical species through bodies.
As illustrated in Figure 2, we consider a thermodynamic system made of K compartments that can exchange matter by diffusion across their common boundaries. Some compartments may have external ports, through which species can flow into or out of the compartment. We denote by A k the number of ports of compartment k, and by J a,k , the molar flow rate flowing into or out of compartment k through the a-th port. We assume that the system has a single species and denote by N k the number of moles of the species in the k-th compartment, k = 1, ..., K. We assume that the thermodynamic system is simple; i.e., a uniform entropy S, the entropy of the system, can be attributed to all the compartments. As earlier, the thermodynamic quantities known at the ports are usually the pressure and the temperature p a,k , T a,k , from which the other thermodynamic quantities, such as µ a,k or S a,k , are deduced from the state equations of the gas. The setting that we develop is well appropriate for the description of diffusion across composite membranes, e.g., composed of different elements arranged in a series or parallel array, which occur frequently in living systems and have remarkable physical properties, see [22][23][24][25].
For each compartment k = 1, ..., K, the mole balance equation is where J l→k = −J k→l is the molar flow rate from compartment l to compartment k due to diffusion of the species, and J a,k is the molar flow rate flowing into or out of compartment k through the a-th port.
Lagrangian variational formulation. By extending the variational formulation developed in Section 3.2, we consider an open simple system with Lagrangian L(q,q, S, N 1 , ..., N K ), where q = (q 1 , ..., q n ) are the mechanical variables of the system, S the single entropy, and N k the number of moles in the k-th compartment, k = 1, ..., K. The additional mechanical interactions described in Section 3.3, via the motion x of the species, can be easily included in the present setting, too. Given the external and friction forces F ext , F fr , the molar flow rates J l→k , J a,k , and the entropy flow rates J l→k S , J a,k S , the variational formulation is expressed as where the curves satisfy the nonlinear nonholonomic constraint and with respect to variations subject to the constraint J a,k δW k + J a,k S δΓ , VARIATIONAL CONSTRAINT (28) with δq(t 1 ) = δq(t 2 ) = 0 and δW k (t 1 ) = δW k (t 2 ) = 0. By taking the variation of the integral in (26) and using the variational constraint (28), we get the following conditions: J a,k , k = 1, ..., K, As before, we define the temperature and the chemical potential potentials of each compartment as T := − ∂L ∂S and µ k := − ∂L ∂N k . By inserting the conditions (29) into the phenomenological constraint (27) and using we get the following entropy equation The rate of entropy production within the system is given by where the three terms correspond to the entropy production associated to mechanical friction, diffusion, and mixing of gas flowing into the system through the ports. By the second law, the entropy production must be always positive and hence suggests the phenomenological relations where λ ij , i, j = 1, ..., n and G kl , k, l = 1, ..., K are functions of the state variables, with λ ij positive semi-definite and G kl ≥ 0, for all k, l. The third term has been already discussed earlier.
The total energy associated to the Lagrangian L(q,q, S, N 1 , ..., N K ) is defined as E(q,q, S, N 1 , ..., N K ) := ∂L ∂q ,q − L, and satisfies the energy balance J a,k µ a,k + J a,k S T a,k = P ext W + P ext M .
Recall that the abstract variational formulation consists of two kind of constraints, the variational constraint C V in (3) and the kinematic constraints C K in (4), which are related with each other in a very specific way. Concerning the variational formulations (16)- (18), for instance, the curve q(t) of the abstract setting is given by the collection of curves (q(t), x(t), S(t), N(t), Γ(t), W(t), Σ(t)), and the Lagrangian L(t, q,q) of the abstract setting is time independent and given by the function L(q,q, x,ẋ, S, N) +Ẇ N + (S − Σ)Γ. The linear function δq i → A i (t, q,q) r δq i is given by the linear map   (δq, δx, δS, δN, δΓ, δW, δΣ) where one notes that the explicit time dependence may come from the time dependence of the functions J a x , J a , J a p . On the other hand, the functions B r (t, q,q) in (4) correspond to Then, one notices that with these choices, the variational constraint C V in (3) yields (18), and the kinematic constraint C K in (4) yields (17). Similarly, the variational formulations in (26)-(28) can be also seen as a special instance of the abstract formulations (9)-(11).

Examples of Open Simple Systems with Matter Transfer
Here we consider three examples of open simple systems with matter diffusion of a single chemical species. The first example is a simple system with matter diffusion between two compartments. The other two examples are diffusion processes appearing in composite membranes, consisting of serial or parallel interconnections of membranes ( [22][23][24]; [25], Section 4.7).

Example 3 (Diffusion between Two Compartments)
. Consider an open system with matter diffusion of a single species between two compartments and transfer of matter into or out of the compartments via two external ports with molar flow rate J a k , temperature T a k and pressure p a k , k = 1, 2 as shown in Figure 3. In this case, the Lagrangian is just given by the internal energy of the system, L(q,q, S, N 1 , N 2 ) = −U(S, N 1 , N 2 ). From the above developments it follows the evolution equations as with T = ∂U ∂S and µ k = ∂U ∂N k , k = 1, 2, together with the relations where we introduced the molar enthalpy at the ports a k , k = 1, 2, as H a k = U a k + p a k V a k = µ a k + T a k S a k , k = 1, 2.
In (30), the first two equations are the mole balance in each compartment, where J 1→2 is the molar flow rate due to diffusion between the compartments. The last equation is the entropy equation, where we note that the rate of entropy production within the system reads From the second law, the internal entropy production must be always positive. In particular, for the process of diffusion, this suggests the phenomenological relation where G 12 ≥ 0 is a function of the state variables.
One verifies that along a solution curve (S(t), N 1 (t), N 2 (t)) of (30), we have the balance of energy  Figure 4, in which diffusion occurs through a composite membrane made of different elements arranged in a series-array. In this example, matter also transfers into or out of the compartments via two external ports with molar flow rate J a k , temperature T a k and pressure p a k , k = 1, 2. In this case, the variational formulation yields the evolution equations together with the relations where H a k is the molar enthalpy at the port a k . From the second law, the entropy production must be always positive and hence dictates the choice of the phenomenological equations for positive state functions G 1,m 1 , G m i ,m i+1 , G 2,m K . The energy balance equation holds as

Example 5 (Diffusion through a Parallel Array of Membranes)
. We now consider diffusion through a composite membrane made of different elements arranged in a parallel-array, as illustrated in Figure 5. As before, matter also transfers into or out of the compartments via two external ports. In this case, the variational formulation directly yields the evolution equations together with the relations As earlier, the second law suggests the phenomenological equations for positive state functions G 1,m i , G 2,m i . The energy balance equation is the same as in (33).

Open Discrete Systems with Heat and Matter Transfer
In this section, we extend the variational formulation developed above to the case of non-simple open thermodynamic systems. Such systems are composed of several subsystems, each of them being simple and open, and exchanges heat and matter with other subsystems.

Variational Formulation for Discrete Open Systems
Consider an open system with a single species experiencing internal diffusion and heat transfer between several compartments, and transfer into or out of the system through several ports. The walls between compartments can be adiabatic, impermeable, or permeable. An example of such a system is illustrated in Figure 6. For simplicity, we do not consider the mechanical interactions. They can be included as in Section 3.3.
We denote by S k and N k the entropies and the number of moles N k of the k-th compartment, k = 1, ..., K. We assume that the system also involves mechanical variables q = (q 1 , ..., q n ). We denote by P k→l the total power exchange due to the heat and matter transfer between the k-th and l-th compartments and by J k→l the molar flow rate of the chemical species between the k-th and l-th compartments. When the power exchange is only associated to heat transfer, it is denoted by P k→l H . We assume that the Lagrangian of the system is of the form L(q,q, S 1 , ..., S K , N 1 , ..., N K ), i.e., it depends on the mechanical variables q, as well as the number of moles and the entropies of each compartment. As earlier, we denote by F ext and F fr the external and friction forces. We also introduce the fluxes J lk , k = l such that J kl = J lk . The relation between the fluxes J kl and the total power exchanges P k→l will be given later. For the purpose of the variational formulation, it is convenient to define the flux J kl for k = l as As earlier, the k-th compartment may have A k ports, through which species can flow out or into the system. In addition, we also assume that the k-th compartment may have B k heat sources of temperature T b,k H . Given the Lagrangian L, the external and friction forces F ext , F fr k , the fluxes J kl , the molar flow rates J l→k , J a,k , and the entropy flow rates J l→k S , J a,k S , the variational formulation is expressed as δ t 2 t 1 L(q,q, S 1 , ..., S K , N 1 , ..., where the curves satisfy the nonlinear nonholonomic constraint and with respect to variations subject to the constraint
Taking the variations, we get With the help of the variational constraint we replace δΣ k and we get From these conditions, together with (35), we get the evolution equations for the open system with internal diffusion and heat transfer as J a,k , k = 1, ..., K, With this expression ofΣ, the balance of total entropy S = ∑ K k=1 S k iṡ where the symmetric part of the n × n matrices λ k and of the 2 × 2 matrices L kl are positive. The entries of these matrices are phenomenological coefficients determined experimentally, which may in general depend on the state variables. From Onsager's relation, the 2 × 2 matrix is symmetric, for all k, l. The matrix elements L kl HH and L kl MM are related to the processes of heat conduction and diffusion between the k-th and l-th compartments. The coefficient L kl MH and L kl HM describe the cross-effects, and hence are associated to discrete versions of the process of thermal diffusion and the Dufour effect. Thermal diffusion is the process of matter diffusion due to the temperature difference between the compartments. The Dufour effect is the process of heat transfer due to difference of chemical potentials between the compartments.

Examples of Non-Simple Systems with Heat and Matter Transfer
Here we consider two examples of open non-simple systems experiencing heat conduction, diffusion, and their cross-effects. While the first example concerns the simple case of two compartment, the second describes the case of a composite membrane.

Example 6 (Heat Conduction and Diffusion between Two Compartments).
We consider the open system consisting of two compartments as illustrated in Figure 7. The compartments are separated by a permeable wall through which heat conduction and diffusion is possible. They also have ports a 1 , a 2 through which matter can flow into or out of the system. The fluxes, temperatures, and pressures at the ports are J a k , T a k , p a k , k = 1, 2. where U(S, N) is the internal energy of the chemical species. In this case, the system (36) specifies to where T k = ∂U ∂S k , µ k = ∂U ∂N k , k = 1, 2, are the temperatures and chemical potentials of the k-th compartments.
In (40), the first and second equations are the mole balances in each compartment, while the third and fourth equations are the entropy equations in each compartment. Thus it follows that the equation for the total entropy S = S 1 + S 2 of the system iṡ In this case, the system (36) specifies to N m i = J 1→m i + J 2→m i , i = 1, ..., N, The internal entropy production is found from (37) as from which the entropy equation follows asṠ =Σ + 2 ∑ k=1 S a k J a k .

Conclusions
In this paper, we have developed a Lagrangian variational formulation of nonequilibrium thermodynamics for discrete open systems. We have introduced an abstract variational formulation for systems with time-dependent nonlinear nonholonomic constraints, based on a variational constraint and a kinematic constraint, related in a specific way. We have first shown how the proposed variational formulation applies to simple open systems, i.e., system well described by a single entropy variable. We have considered simple systems experiencing mechanical interactions, internal diffusion process between several compartments, as well as matter transfer with the exterior through several ports. We have illustrated the proposed framework with several examples such as diffusion through serial and parallel composite membranes. Then, we have extended our variational formulation to the case of non-simple open systems, experiencing mechanical interactions, internal diffusion, internal heat transfer, and their cross-effects, which are the discrete versions of thermal diffusion and of the Dufour effect. In each case, our approach yields a systematic and efficient way to derive the complete evolution equations for the open discrete system, independently on its complexity. Such an approach may be especially useful for the treatment of nonequilibrium thermodynamic of biophysical systems.
The approach presented in this paper provides the foundational step for further developments on the nonequilibrium thermodynamics of open systems. As our future works, we project to consider (i) the associated Dirac dynamical system formulation for discrete open systems, see [12] for closed systems; (ii) the variational numerical discretization for discrete open systems, see [6] for closed system; (iii) the treatment of continuum open systems.