\section{Discretization and Solution Approach for the Solid Mechanics Model}\label{sec:discretization_mechanics}

After the formulation of linear and nonlinear models for solid mechanics in \cref{sec:model_muscle_contraction}, this section discusses their discretization and derives finite element formulations for the linearized model and the nonlinear model, both static and dynamic.
We also describe the algorithms used to obtain a numerical solution.

The implementation of a solver for generic hyperelastic descriptions is an interdisciplinary endeavor, if parallel execution is exploited and the model is integrated in a multi-scale biomechanics model.
Therefore, we give a comprehensive derivation of the formulas used to numerically solve the equations matching the implementation in OpenDiHu, such that the implementation is also comprehensible for readers that are not specialized in the field of continuum mechanics. More details on finite element discretizations for solid mechanics models can be found in the literature \cite{zienkiewicz1977finite,SUSSMAN1987357,zienkiewicz2005finite}.

% ---x---
\subsection{Discretization of the Linear Model}\label{sec:linearized_mechanics_model}

In this section, we discuss the linearized and static model. Besides the nonlinear model, our software OpenDiHu also implements the linearized description. The linear model exhibits better numerical properties and can be solved faster than the generic model. Thus, it can serve as a toy problem or can be used for mechanical systems, where the linearization assumptions are valid.

By assuming small strains, we can use the linearized kinematic relation in \cref{eq:linearized_helper3} to express the linear strain tensor $\bfeps$. The material model is Hooke's law formulated in \cref{eq:linearized_helper2}. It relates the strain to the Cauchy stress by $\bfsigma = \C:\bfeps$.

% linear, static and dynamic

Using variational calculus, the system response of external forces and infinitesimal, compatible, virtual displacements $δ\bfu$ is studied.
We start with the \emph{principle of virtual work}, which states that in equilibrium the virtual work $δW$ performed by external forces along any virtual displacements $δ\bfu$ is zero. Equivalently, the external virtual work $δW_\text{ext}$ is equal to the internal virtual work $δW_\text{int}$:
\begin{align}
  δW_\text{int}(\bfu,δ\bfu) &= δW_\text{ext}(δ\bfu) \qquad && \forall δ\bfu \in H^1_0(\Omega)\label{eq:linearized_helper1}.
\end{align}
Here, the external virtual work $δW_\text{ext}$ is given by the product of external forces $\bft$ and the virtual displacements $δ\bfu$ at the same location. The internal virtual work $δW_\text{int}$ is the body's response in terms of stresses $\bfsigma$ and virtual strains $\bfeps$.
In summary, \cref{eq:linearized_helper1} is equivalent to the following equilibrium equation:
\begin{align}
  \ds\int_\Omega \bfsigma(\bfu) : δ\bfeps\,\d\bfx &= \ds\int_{∂\Omega} \bft : δ\bfu\,\d \bfx &&\forall δ\bfu \in H^1_0(\Omega).\label{eq:linearized_helper1b}
\end{align}
The vectors contain the degrees of freedom of a finite element discretization. The operator \say{:} denotes the component-wise product.

Often, it is easier to write the equations in component form. Indices $a,b,c,\dots$ are used to specify a dimension index in $\{1,\dots,d\}$. The letters $L,M \in \{1,\dots,N\}$ denote indices over degrees of freedom in a mesh with $N$ nodes. The Einstein sum convention is used where repeated indices implicitly indicate summation, except when the indices are in parentheses.
Thus, the right-hand side $\bff$ of \cref{eq:linearized_helper1b} with ansatz functions $\phi^L$ and the degrees of freedom $δu_a^L$ of $δ\bfu$ can be written as:
\begin{align}\label{eq:linearized_mechanics_rhs}
  \bff_a = \ds\int_{∂\Omega} t_{(a)}\,δu_{(a)}^L\,\phi^L \,\d \bfx.
\end{align}

By combining the kinematic relation between displacements $\bfu$ and linearized strains $\bfeps$ in \cref{eq:linearized_helper3}, the material relation between $\bfeps$ and the stress $\bfsigma$ in \cref{eq:linearized_helper2}, the equilibrium relation between $\bfsigma$ and the right-hand side vector $\bff$ in \cref{eq:linearized_helper1b} and after discretizing displacements and virtual displacements, we get the following linear matrix equation:
\begin{align}\label{eq:linearized_helper4}
  \bfK\,\bfu = \bff.
\end{align}
The stiffness matrix $\bfK$ has rows and columns for every combination of degree of freedom $L,M \in \{1,\dots,N\}$ and dimension indices $a,b \in \{1,2,3\}$. The entries are given by:
\begin{align*}
  \bfK_{LaMb} = \ds\int_{\Omega} \mathbb{C}_{adbc}\p{\phi^L(\bfx)}{x_{d}}\p{\phi^M(\bfx)}{x_{c}}\,\d \bfx.
\end{align*}
%
The resulting model in \cref{eq:linearized_helper4} describes the passive behavior of a body under the linearization assumptions and can be used in an appropriate biomechanical application.

However, for contracting muscle tissue, we also need to incorporate active stresses that are generated at the sarcomeres in the muscle. As described in \cref{eq:active_stress_linear}, an active stress term $\bfsigma^\text{active}$  can be considered. Because this active term is prescribed by the activation dynamics and the subcellular model, it has to appear on the right-hand side of the linear model.
We add the active stress term $\bfsigma^\text{active}$ to the external virtual work in \cref{eq:linearized_helper1}, yielding the equation:
\begin{align}\label{eq:linearized_helper5}
  δW_\text{int}(\bfu,δ\bfu) &= \bff + \ds\int_\Omega \bfsigma^\text{active} : δ\bfeps_{-}\,\d\bfx &&\forall δ\bfu \in H^1_0(\Omega).
\end{align}
%
The active stress is associated with compression, i.e., negative virtual strains $δ\bfeps < 0$. Therefore, we use $δ\bfeps_{-}$ which is defined equal to $δ\bfeps$ for $δ\bfeps < 0$ and zero otherwise.
From \cref{eq:linearized_helper5}, we get the same discretized linear system as in \cref{eq:linearized_helper4}, but with an additional term $\bff^\text{ active}$ in the right-hand side that contains the discretized prescribed active stress field $\bfsigma^\text{active}_{ab}(\bfx)$:
\begin{align}\label{eq:linearized_helper6}
  \bff^\text{ active}_{La} = \ds\int_{Ω}\bfsigma^\text{active}_{ab}(\bfx)\,\p{\phi^L(\bfx)}{x_{b}} \,\d\bfx.
\end{align}
%
\subsection{Discretization of the Nonlinear Static Hyperelastic Model}\label{sec:static_hyperelastic_fe_model}

Next, we discuss the discretization of the nonlinear solid mechanics model, which uses the model equations introduced in \cref{sec:model_muscle_contraction}.
We begin with the discretization of a static, incompressible problem, where no velocities have to be considered.
The discretization is extended to the dynamic model in \cref{sec:solver_dynamic_hyperelasticity_fe_model}.

%In \cref{sec:linearized_mechanics_model}, the ingredients of a solid mechanics model derivation consisting of equilibrium, material and kinematic equations were outlined and used to derive a linearized description. For the nonlinear model, the material equations were discussed in \cref{sec:material_modeling} and the stress and elasticity tensors were derived in \cref{sec:stress_and_elasticity}. This section uses these building blocks and presents the full derivation for the generic hyperelastic finite element model.
As described in \cref{sec:assumptions_and_model_equations}, the equilibrium equation can be formulated in terms of the \emph{Hellinger-Reissner energy functional} $\Pi_L(\bfu,p) = \Pi_\text{int}(\bfu,p) + \Pi_\text{ext}(\bfu)$
given in \cref{eq:hellinger_reissner}.
It consists of the external energy functional, given in \cref{eq:pi_ext} as
\begin{align*}
  \Pi_\text{ext}(\bfu) = -\ds\int_{\Omega_0} \bfB\, \bfu\,\d V - \ds\int_{∂\Omega_0^t}\bar{\bfT}\,\bfu\,\d S,
\end{align*}
with body force $\bfB$ and surface traction $\bar{\bfT}$, and of the internal energy functional
\label{sec:section_with_pi_int}
\begin{align}\label{eq:mechanics_helper1}
  \Pi_\text{int}(\bfu,p) = \ds\int_{\Omega_0} \Psi_\text{iso}\big(\bar\bfC(\bfu)\big)\,\d V
    + \ds\int_{\Omega_0} p\,\big(J(\bfu) - 1\big)\,\d V.
\end{align}
%
Here, $\Psi_\text{iso}$ is the isochoric strain-energy density function introduced in \cref{eq:psi_iso} in terms of the reduced right Cauchy-Green tensor $\bar{\bfC}$ defined in \cref{eq:reduced_fc}.
The first term in \cref{eq:mechanics_helper1} describes the isochoric elastic response of the material, the second term adds the incompressibility constraint $J=1$ with the Lagrange multiplier $p$. The value of $p$ is computed as part of the model and can be identified as the hydrostatic pressure. Therefore, the second term is interpreted as the elastic response to compression and is included in the internal energy functional $\Pi_\text{int}$.

According to the \emph{principle of stationary potential energy}, the system is in equilibrium, if the potential energy functional is stationary.
This is the case, if the first variation $δ\Pi_L$ is zero.
Using the additive structure of $\Pi_L$, we can express the principle of stationarity as
\begin{subequations}
  \begin{align}
    D_{δ\bfu}\Pi_L(\bfu, p) &= D_{δ\bfu}\Pi_\text{int}(\bfu,p) + D_{δ\bfu}\Pi_\text{ext}(\bfu) \overset{!}{=} 0, & \forall δ\bfu  \label{eq:variations_functional_zero_a}\\[4mm]
    D_{δp}\Pi_L(\bfu, p) &= D_{δp}\Pi_\text{int}(\bfu,p) \overset{!}{=} 0 & \forall δp. \label{eq:variations_functional_zero_b}
  \end{align}
\end{subequations}
The variations of the internal and external energy functionals are defined as
\begin{align}\label{eq:def_variation}
  D_{δ\bfu}\Pi(\bfu) &= \d{\eps} \Pi(\bfu + \epsδ\bfu)\big|_{\eps=0}, &
  D_{δp}\Pi(p) &= \d{\eps} \Pi(p + \epsδp)\big|_{\eps=0}.
\end{align}
They can be identified as the internal and external virtual work,
\begin{align*}
  D_{δ\bfu}\Pi_\text{int}(\bfu,p) &= δW_\text{int}, & D_{δ\bfu}\Pi_\text{ext}(\bfu) &= -δW_\text{ext}.
\end{align*}
Thus, \cref{eq:variations_functional_zero_a} can be expressed as
\begin{align*}
  δW_\text{int} - δW_\text{ext} &= 0,
\end{align*}
which is the form of the equilibrium equation that was used in \cref{eq:linearized_helper1} in the derivation of the linearized model in \cref{sec:linearized_mechanics_model} . The Euler-Lagrange equations corresponding to the variational problem are the local incompressibility constraint and the partial differential equation of balance of momentum presented in \cref{eq:contraction_1,eq:contraction_2}.

Executing the derivative in the definitions of the variations in \cref{eq:def_variation} yields the following terms:
\begin{align*}
  &D_{δ\bfu}\Pi_\text{int}(\bfu,p)  = \ds\int_{\Omega_0} \bfS(\bfu,p): δ\bfE(δ\bfu)\,\d V,
  \qquad D_{δp}\Pi_\text{int}(\bfu,p) =\ds\int_{\Omega_0} \big(J(\bfu) - 1\big)δp\,\d V, \\[4mm]
  &D_{δ\bfu}\Pi_\text{ext}(\bfu) = -\ds\int_{\Omega_0} \bfB\cdot δ\bfu\,\d V - \ds\int\limits_{∂\Omega^t_0} \bar{\bfT}\cdot δ\bfu\,\d S,
\end{align*}
where the variational variables $δp,δ\bfu$ and $δ\bfE$ are the virtual pressure, virtual displacements, and virtual strains.

% discretization
We discretize the solutions of the functional for the displacements $\bfu(\bfx)$ and pressure $p(\bfx)$ and their variations using different ansatz functions $\phi^L$, $L=1,\dots,N_u$ and $\psi^L$, $L=1,\dots,N_p$:
\begin{align*}
   u_a &= \hat{u}_a^L \phi_{(a)}^L, & δu_a &= δ\hat{u}_a^L \phi_{(a)}^L,   & p &= \hat{p}^L \psi^L, & δp &= δ\hat{p}^L \psi^L.
\end{align*}
Again, Einstein summation over repeated indices, in this case the index $L$, is used. The displacements function is vector-valued and given by $\bfu(\bfx) = (u_1(\bfx), u_2(\bfx), u_3(\bfx))^\top$. The vectors containing the degrees of freedom are denoted by $\hat{\bfu} = (\hat{u}^L)_{L=1,\dots,N_u}$ and $\hat{\bfp} = (\hat{p}^L)_{L=1,\dots,N_p}$.

The kinematics equation to compute virtual strains from virtual displacements follows from \cref{eq:green_lagrange_u} in Lagrangian description and is given by $δ\bfE = \sym(\bfF^\top ∇\bfu)$. Its discretized form is given as follows, where the subscript comma $\square_{,A}$  indicates the derivative with respect to the indexed coordinate $\bfX_A$:
\begin{align*}
  δE_{AB} &= \dfrac12\left(F_{aB}\, \phi_{(a),A}^M + F_{aA}\, \phi_{(a),B}^M\right)δ\hat{u}_{a}^M.
\end{align*}
%
% summarize equations
In summary, the resulting set of discretized nonlinear equations can be formulated as:
\begin{align}\label{eq:mechanics_static_system}
  δW_\text{int}(\bfu,p) - δW_\text{ext} &= 0 \qquad \forall\,δ\bfu, \\[4mm]
  D_{δp}\Pi_L(\bfu) &= 0 \qquad \forall\,δp,
\end{align}
with the following discretized terms:
\begin{subequations}\label{eq:mechanics_static_system2}
  \begin{align}
    δW_\text{int}(\hat{\bfu},\hat{\bfp})  = \ds\int_{\Omega}\dfrac12  S_{AB}(\hat{\bfu},\hat{\bfp})\, \left(F_{aB}\, \phi_{(a),A}^M + F_{aA}\, \phi_{(a),B}^M\right)δ\hat{u}_{a}^M \,\d V,\\[4mm]
    δW_\text{ext}  = \ds\int_{\Omega} B_a \phi_{(a)}^M\,δ\hat{u}^M_a \,\d V +\ds\int_{∂\Omega}  \bar{T}^L_a\,\phi_{(a)}^L\, \phi_{(a)}^M\,δ\hat{u}^M_a\,\d S, \\[4mm]
    D_{δp}\Pi_L(\hat{\bfu}) = \ds\int_\Omega \big(J(\hat{\bfu}) - 1\big)\,δp\,\d V .
  \end{align}
\end{subequations}

The nonlinear system of equations in \cref{eq:mechanics_static_system,eq:mechanics_static_system2} can now be solved for the unknown vectors  $\hat{\bfu}$ and $\hat{\bfp}$ of degrees of freedom using a Newton scheme.

\subsection{Discretization of the Nonlinear Dynamic Hyperelastic Model}

We extend the discretization of the static model in the last section for the dynamic model.
The vector of unknowns is extended by a velocity function $\bfv: \Omega_t \to \R^3$. The additional equation $\dot{\bfu} = \bfv$ relates the displacements and the velocity.

As noted in the derivation of the equilibrium equation in \cref{sec:assumptions_and_model_equations}, the body force term $\bfB$ in the external energy functional also includes the inertial forces $\bfB_\text{inertial} = \rho_0\,\dot{\bfv}$ to describe the dynamic behavior.

The resulting nonlinear system of equations is given as follows:
\begin{subequations}\label{eq:mechanics_dynamic0}
  \begin{align}
    δW_\text{int}(\bfu,p) - δW_\text{ext}(\bfv) &= 0 \qquad \forall\,δ\bfu,\label{eq:mechanics_dynamic_system1}\\[4mm]
    \bfv &= \dot{\bfu},\label{eq:mechanics_dynamic_system2}\\[4mm]
    D_{δp}\Pi_L(\bfu) &= 0 \qquad \forall\,δp.\label{eq:mechanics_dynamic_system3}
  \end{align}
\end{subequations}

\subsection{Computation of the Stress Tensor and the Elasticity Tensor}\label{sec:stress_and_elasticity}

In the Newton solver, we need to compute the stress tensor $\bfS$ and its derivative $\C$, called the elasticity tensor, given the current displacement field $\bfu$. The relations are defined by the material model given by the strain energy function. This section presents the algorithm how to obtain the values of $\bfS$ and $\C$ from the displacements $\bfu$. While the derivation is formulated in terms of the displacement function $\bfu$, it is also valid for the finite element discretization, i.e., using the vector $\hat{\bfu}$  of degrees of freedom instead.

Following \cref{eq:material_model_helper1}, the second Piola-Kirchhoff stress $\bfS$ is given by the derivative of the strain energy function $\Psi$ with respect to $\bfC$.
For the representation using the invariants, the chain rule has to be used:%
\begin{align*}
   \bfS &= 2\,\p{\Psi(\bfC)}{\bfC} = \p{\Psi}{I_a}\p{I_a}{\bfC}.
\end{align*}
Using the decoupled form, the resulting stresses are also decoupled as $\bfS = \bfS_\text{vol}+\bfS_\text{iso}$. The volumetric stress $\bfS_\text{vol}$ describes the elastic response to compression, the isochoric stress $\bfS_\text{iso}$ describes the response to the deviatoric deformation. In the following, all steps to compute these stresses are listed. The rationale is to give a condensed reference of the implemented algorithm in OpenDiHu to facilitate further development.
For the derivation of all intermediate steps, we refer to the literature \cite{holzapfel2000nonlinear}.

At first, the reduced stress tensor $\bar{\bfS}$ that neglects the volumetric change is formulated as:%
%
\begin{align*}
  \bar{\bfS} = 2\p{\Psi_\text{iso}(\bar{I}_1,\bar{I}_2,\bar{I}_4,\bar{I}_5)}{\bar{\bfC}} &= \bar{\gamma}_1\,\bfI + \bar{\gamma}_2\,\bar{\bfC}
  + \bar{\gamma}_4\, \bfa_0 \otimes \bfa_0 + \bar{\gamma}_5\,(\bfa_0 \otimes \bar{\bfC}\,\bfa_0 + \bfa_0\bar{\bfC}\otimes \bfa_0).
\end{align*}
In case of an isotropic material, the terms involving $\bfa_0$ are not needed. The prefactors are given by derivatives of the strain energy function with respect to the reduced invariants:
%
\begin{align*}
  \bar{\gamma}_1 &= 2\left(\p{\Psi_\text{iso}(\bar{I}_1, \bar{I}_2)}{\bar{I}_1} + \bar{I}_1\,\p{\Psi_\text{iso}(\bar{I}_1, \bar{I}_2)}{\bar{I}_2}\right),
  &\bar{\gamma}_2 &= -2\p{\Psi_\text{iso}(\bar{I}_1, \bar{I}_2)}{\bar{I}_2},
  &\bar{\gamma}_4 &= 2\p{\Psi_\text{iso}}{\bar{I}_4}\\[4mm]
  \bar{\gamma}_5 &= 2\p{\Psi_\text{iso}}{\bar{I}_5}
\end{align*}
%
Using the fourth order identity tensor $\mathbb{I}$ and the projection tensor $\mathbb{P}$,%
\begin{align*}
  (\mathbb{I})_{abcd} &= \delta_{ac}\,\delta_{bd}, &
  \mathbb{P} &= \mathbb{I} - \dfrac13 \bfC^{-1} \otimes \bfC,
\end{align*}
the stress tensors can finally be computed as
\begin{align*}
  \bfS_\text{iso} &= J^{-2/3}\mathbb{P}:\bar{\bfS}, &
  \bfS_\text{vol} &= J\,p\,\bfC^{-1}, &
  \bfS &= \bfS_\text{iso} + \bfS_\text{vol}.
\end{align*}
In the compressible case including the penalty method, the value of $p$, that is needed for $\bfS_\text{vol}$, is given by the constitutive model as $p = \d \Psi_\text{vol}(J)/\d J$. In the incompressible case, $p$ is the unknown Lagrange multiplier that gets computed as part of the numerical solution. In that case, $p$ has the physical meaning of the hydrostatic pressure.

Using the present algorithm, the stress tensor $\bfS$ can, thus, be computed from derivatives of the strain energy function $\Psi$ and the right Cauchy Green tensor $\bfC$, which can be calculated from the displacement field $\bfu$.

Another important quantity for the numerical solution of the nonlinear system is the fourth order elasticity tensor $\C$, which is defined as
\begin{align*}
  \C = 2\p{\bfS(\bfC)}{\bfC} = 4\dfrac{\partial^2 \Psi(\bfC)}{\partial\bfC\partial\bfC}.
\end{align*}
It is the derivative of the stress tensor and is required in the Jacobian matrix of an iteration of the nonlinear Newton solver. Like the material tensor in \cref{eq:symmetries}, it shows major and minor symmetries and has 21 independent entries.

Like the stress tensor, the elasticity tensor is also additively composed into a volumetric term $\C_\text{vol}$ and an isochoric term $\C_\text{iso}$. The volumetric term can be computed by:%
\begin{align*}
  \mathbb{C}_\text{vol} &= J\,\tilde{p}\,\bfC^{-1} \otimes \bfC^{-1} - 2\,J\,p\,\bfC^{-1} \odot \bfC^{-1}, &
  \big(\bfC^{-1} \odot \bfC^{-1}\big)_{abcd} &= \dfrac12\big(C^{-1}_{ac}\,C^{-1}_{bd} + C^{-1}_{ad}\,C^{-1}_{bc}\big).
\end{align*}
The term includes two pressure variables $\tilde{p}$ and $p$. In the incompressible formulation, both variables equals the Lagrange multiplier $p$. For the compressible formulation, $\tilde{p}$ is derived as $\tilde{p} = p + J\,\d p/\d J$ and $p$ is computed from the volumetric strain energy function as stated above.

The isochoric term $\mathbb{C}_\text{iso}$ of the elasticity tensor follows from the following algorithm listing the quantities to compute:%
\begin{align*}
  &\bar{\delta}_1 = 4\left(\dfrac{∂^2\Psi_\text{iso}}{∂\bar{I}_1\,∂\bar{I}_1} + 2\,\bar{I}_1\dfrac{∂^2\Psi_\text{iso}}{∂\bar{I}_1\,∂\bar{I}_2} +\dfrac{∂\Psi_\text{iso}}{∂\bar{I}_2} + \bar{I}_1^2\,\dfrac{∂^2\Psi_\text{iso}}{∂\bar{I}_2\,∂\bar{I}_2}\right), \,
  \bar{\delta}_2 = -4\left(\dfrac{∂^2\Psi_\text{iso}}{∂\bar{I}_1\,∂\bar{I}_2} + \bar{I}_1\,\dfrac{∂^2\Psi_\text{iso}}{∂\bar{I}_2\,∂\bar{I}_2}\right),\\[4mm]
  &\bar{\delta}_3 = 4\dfrac{∂^2\Psi_\text{iso}}{∂\bar{I}_2\,∂\bar{I}_2}, \quad
  \bar{\delta}_4 = -4\dfrac{∂\Psi_\text{iso}}{∂\bar{I}_2}, \quad
  \bar{\delta}_5 = 4\left(\dfrac{∂^2\Psi_\text{iso}}{∂\bar{I}_1\,∂\bar{I}_4} +\bar I_1 \dfrac{∂^2\Psi_\text{iso}}{∂\bar{I}_2\,∂\bar{I}_4}\right),\\[4mm]
  &\bar{\delta}_6 = -4\dfrac{∂^2\Psi_\text{iso}}{∂\bar{I}_2\,∂\bar{I}_4}, \,\,\,\,
  \bar{\delta}_7 = 4\dfrac{∂^2\Psi_\text{iso}}{∂\bar{I}_4\,∂\bar{I}_4}, \,\,\,\,
  \mathbb{I}_{abcd} = δ_{ac}\,δ_{bd}, \,\,\,\,
  \bar{\mathbb{I}}_{abcd} = δ_{ad}\,δ_{bc}, \,\,\,\,
  \mathbb{S} = (\mathbb{I} + \bar{\mathbb{I}}) / 2, \\[4mm]
  &\p{\bar I_5}{\bar\bfC} = \bfa_0 \otimes \bar\bfC\,\bfa_0 + \bfa_0\,\bar\bfC \otimes \bfa_0, \quad
  \dfrac{∂^2\bar{I}_5}{∂\bar{\bfC}∂\bar{\bfC}} = \p{\bar{\bfC}}(\bfa_0 \otimes \bar\bfC\,\bfa_0 + \bfa_0\,\bar\bfC \otimes \bfa_0),
\end{align*}
\begin{align*}
  &\bar{\mathbb{C}} = J^{-4/3}\bigg(\bar{\delta}_1\,\bfI \otimes \bfI + \bar{\delta}_2\,\big(\bfI \otimes \bar{\bfC} + \bar{\bfC} \otimes \bfI\big) + \bar{\delta}_3\bar{\bfC} \otimes \bar{\bfC} + \bar{\delta}_4\,\mathbb{S}
  +\bar{δ}_5\,(\bfI \otimes \bfa_0 \otimes \bfa_0 + \bfa_0 \otimes \bfa_0 \otimes \bfI)\\[4mm]
  &\hspace*{1cm} +\bar{δ}_6\,(\bar{\bfC} \otimes \bfa_0 \otimes \bfa_0 + \bfa_0 \otimes \bfa_0 \otimes \bar{\bfC})
  +\bar{δ}_7\,(\bfa_0 \otimes \bfa_0 \otimes \bfa_0 \otimes \bfa_0) \\[4mm]
  &\hspace*{1cm} + \bar{δ}_8\,\Big(\bfI \otimes \p{\bar{I}_5}{\bar{\bfC}} + \p{\bar{I}_5}{\bar{\bfC}} \otimes \bfI \Big)
  + \bar{δ}_9\,\Big(\bar{\bfC} \otimes \p{\bar{I}_5}{\bar{\bfC}} + \p{\bar{I}_5}{\bar{\bfC}} \otimes \bar{\bfC} \Big) + \bar{δ}_{10}\Big(\p{\bar{I}_5}{\bar{\bfC}} \otimes \p{\bar{I}_5}{\bar{\bfC}}\Big) \\[4mm]
  &\hspace*{1cm}+ \bar{δ}_{11} \Big(\bfa_0 \otimes \bfa_0 \otimes \p{\bar{I}_5}{\bar{\bfC}} + \p{\bar{I}_5}{\bar{\bfC}} \otimes \bfa_0 \otimes \bfa_0 \Big) + \bar{δ}_{12} \dfrac{∂^2\bar{I}_5}{∂\bar{\bfC}∂\bar{\bfC}}\bigg)\\[4mm]
  &\tilde{\mathbb{P}} = \bfC^{-1} \odot \bfC^{-1} - \dfrac13 \bfC^{-1} \otimes \bfC^{-1} \\[4mm]
  &\mathbb{C}_\text{iso} = \mathbb{P} : \bar{\mathbb{C}} : \mathbb{P}^\top + \dfrac23 J^{-2/3} \bar{\bfS} : \bfC\,\tilde{\mathbb{P}} - \dfrac23\big(\bfC^{-1}\otimes \bfS_\text{iso} + \bfS_\text{iso}\otimes \bfC^{-1}\big)
\end{align*}
Then, $\C = \C_\text{vol} + \C_\text{iso}$ can be calculated.
%
%

% invariants: I1-I5
% transversely isotropic
% reduced invariants, reduced quantities for compressible materials
% strain energy function, derivative
% elasticity tensor
% -> computation of S and C

\subsection{Nonlinear Solver for the Solid Mechanics Model}\label{sec:solver_static_hyperelastic_fe_model}

% Newton solver
The governing nonlinear system of equations is solved by a Newton scheme. We define the vector of the unknown degrees of freedom as $(\hat{\bfu},\hat{p}) =: \bfz$. Then, the nonlinear equation takes the general form $\bfW(\bfz) = 0$. By linearization around a value $\bfz$, we get%
\begin{align*}
  \bfW(\bfz+Δ\bfz) = \bfW(\bfz) + \bfJ\,Δ\bfz + o(\bfz + Δ\bfz),
\end{align*}
with the increment $Δ\bfz = (Δ\hat\bfu, Δ\hat{p})$ and the Jacobian matrix $\bfJ = \partial {\bfW}/\partial {\bfz}$.
Neglecting the sublinear error term $o(z + Δz)$, we can start from an initial guess $\bfz^{(0)}$ and proceed to find the root of $\bfW$ using the following iterative Newton scheme:%
\begin{subequations}\label{eq:newton_scheme}
  \begin{align}
    \bfJ\,Δ\bfz^{(n)} = -\bfW(\bfz^{(n)}),\label{eq:mechanics_linear_system}\\[4mm]
    \bfz^{(n+1)} = \bfz^{(n)} + Δ\bfz^{(n)}.
  \end{align}
\end{subequations}
\Cref{eq:mechanics_linear_system} is a linear system of equations with the system matrix given by $\bfJ$, which has to be solved in every iteration step $n$. The linear system of equations can be expressed as follows:
\begin{align}\label{eq:static_newton_iteration}
  \matt{\bfk_{δ\bfu,Δ\bfu} & \bfk_{δp,Δ\bfu}^\top \\[2mm]
  \bfk_{δp,Δ\bfu} & \bfzero} \, \matt{Δ\hat{\bfu} \\[2mm] Δ\hat{p}}
  =
  \matt{-\bfR_{δ\bfu} \\[2mm] -\bfR_{δp}}.
\end{align}
The definition of the right-hand sides $\bfR_{δ\bfu} = δW_\text{int} - δW_\text{ext}$ and $\bfR_{δp}=D_{δp}\Pi_L$ is given in \cref{eq:mechanics_static_system}. The system matrix is composed as follows. The upper left part consists of 3 times 3 blocks of submatrices, each with size $N_u \times N_u$ and the entries given by:
\begin{align*}
  \bfk_{δ\bfu,Δ\bfu,(L,a),(M,b)} &= \ds\int_\Omega \phi_{(a),B}^L\tilde{k}_{abBD}\phi_{(b),D}^M\,\d V &\text{with}\quad
  \tilde{k}_{abBD} &= δ_{ab}\,S_{BD} + F_{aA}\,F_{bC}\,\mathbb{C}_{ABCD}.
\end{align*}
Here, $S_{BD}$ and $\mathbb{C}_{ABCD}$ are entries of the second Piola-Kirchhoff stress tensor $\bfS$ and the elasticity tensor $\mathbb{C}$. The computation of these terms uses the description in \cref{sec:stress_and_elasticity}.

The lower left part of the system matrix in \cref{eq:static_newton_iteration} is given by 1 times 3 blocks of submatrices, each with size $N_p \times N_u$ and entries given by:
\begin{align*}
  \bfk_{δp,Δ\bfu,L,(M,a)} = \ds\int_\Omega J\,\psi^L\,(F^{-1})_{Ba}\,\phi_{(a),B}^M \,\d V.
\end{align*}
The upper right part equals the transposed lower left block such that the system matrix is symmetric. Solving the system in \cref{eq:static_newton_iteration} in every iteration of the Newton scheme in \cref{eq:newton_scheme} converges to the solution of the static solid mechanics problem.

\subsection{Discretization and Solution of the Dynamic Hyperelastic Model}\label{sec:solver_dynamic_hyperelasticity_fe_model}
% dynamic hyperelasticity (6.9.2)

The dynamic model is given by the system of nonlinear equations in \cref{eq:mechanics_dynamic}. In addition to the spatial discretization with finite elements, we need to discretize the temporal derivatives of the displacement field $\bfu$ and the velocity field $\bfv$.
The time derivatives are discretized to timesteps $t=i\cdot \dt$ with an implicit Euler scheme:
\begin{align*}
  \dot{\bfu} &\leadsto \dfrac1{\dt}(\bfu^{(i+1)} - \bfu^{(i)}), & \dot{\bfv} &\leadsto \dfrac1{\dt}(\bfv^{(i+1)} - \bfv^{(i)}).
\end{align*}
%

Because of the added inertial body force, the external virtual work now depends on the vector of unknowns.
In consequence, we split the external virtual work $δW_\text{ext}$ into a dead part $δW_\text{ext,dead}$ that solely depends on external forces and an inertial part:%
\begin{align*}
  δW_\text{ext} = δW_\text{ext,dead} + \ds\int_{\Omega} \rho_0\,\dfrac{v^{(i+1),L}_{(a)} - v^{(i),L}_{(a)}}{\dt}\,\phi_{(a)}^L\, \phi_{(a)}^M\,δ\hat{u}^M_a \,\d V = 0.
\end{align*}
In summary, the system of equations to proceed from timestep $i$ to $(i+1)$ is given as:
\begin{subequations}\label{eq:mechanics_dynamic}
  \begin{align}
    δW_\text{int}({\bfu^{(i+1)}},p^{(i+1)}) - δW_\text{ext}(\bfv^{(i)},\bfv^{(i+1)}) &= 0 \qquad &&\forall\,δ\bfu,\label{eq:mechanics_dynamic1}\\[4mm]
    \dfrac1{\dt}(\bfu^{(i+1)} - \bfu^{(i)}) - \bfv^{(i+1)} &= 0,\label{eq:mechanics_dynamic2}\\[4mm]
    D_{δp}\Pi_L(\bfu^{(i+1)}) &= 0 \qquad &&\forall\,δp.\label{eq:mechanics_dynamic3}
  \end{align}
\end{subequations}
Here, \cref{eq:mechanics_dynamic1} is the principle of virtual work, \cref{eq:mechanics_dynamic2} relates displacements $\bfu$ and velocities $\bfv$ and \cref{eq:mechanics_dynamic3} is the incompressibility constraint.

The system is again solved using the Newton scheme presented in \cref{sec:solver_static_hyperelastic_fe_model}.
The linear system for each Newton iteration takes the following form:
\begin{align*}
  \matt{
    \bfk_{δ\bfu,Δ\bfu} & \bfl_{δ\bfu,Δ\bfv} & \bfk_{δp,Δ\bfu}^\top \\[2mm]
    \bfl_{δ\bfv,Δ\bfu} & \bfl_{δ\bfv,Δ\bfv} & \bfzero \\[2mm]
    \bfk_{δp,Δ\bfu} & \bfzero & \bfzero
  } \,
  \matt{Δ\hat{\bfu} \\[2mm] Δ\hat{\bfv} \\[2mm] Δ\hat{p}}
  =
  \matt{-\bfR_{δ\bfu} \\[2mm] -\bfR_{δ\bfv} \\[2mm] -\bfR_{δp}}.
\end{align*}
The entries $\bfk_{δ\bfu,Δ\bfu}$ and $\bfk_{δp,Δ\bfu}$ are the same as in the static case in \cref{eq:static_newton_iteration}.
The other non-zero entries are given by
\begin{align*}
  \bfl_{δ\bfu,Δ\bfv,(L,a),(M,b)} &= \dfrac1{\dt}\delta_{ab} \ds\int_{\Omega} \rho_0\,\,\phi_{(b)}^M \,\phi_{(a)}^L \,\d V, &
  \bfl_{δ\bfv,Δ\bfu,(L,a),(M,b)} &= \dfrac{1}{\dt}\delta_{ab}\,\delta^{LM},\\[4mm]
  \bfl_{δ\bfv,Δ\bfv,(L,a),(M,b)} &= -\delta_{ab}\,\delta^{LM}.
\end{align*}

Note that in the dynamic problem, the system matrix is unsymmetric. It would be symmetric if the entries $\bfl_{δ\bfu,Δ\bfv}$ and $\bfl_{δ\bfv,Δ\bfu}^\top$ were the same. This would be the case for a density of one, $\rho_0 = 1$, and if the term $\int_{\Omega} \phi_{b}^M \phi_{a}^L \,\d V$ would be replaced by $\delta_{ab}\delta^{LM}$. The second condition means that a lumped mass matrix would be used where the diagonal entries are set to the row sums of the original matrix.

We discretize the finite element solution in space by \emph{Taylor-Hood} elements. This type of element uses quadratic ansatz functions $\phi$ for the displacements and velocities and linear ansatz functions $\psi$ for the Lagrange multiplier or hydrostatic pressure $p$ on a 3D hexahedral mesh. This choice was proven to exhibit no locking \cite{zienkiewicz2005finite}. Locking is a phenomenon of degraded convergence of the finite element method for solid mechanics problems and occurs for improper discretization schemes.

For a compressible material, the incompressibility constraint which is the last equation in the systems \cref{eq:mechanics_static_system} or \cref{eq:mechanics_dynamic} is removed. Instead of solving for the pressure $p$ as a Lagrange multiplier, the value is given by the constitutive model as described in \cref{sec:material_modeling}. In consequence, the system matrix of the linear system of equations that is solved in the Newton iterations has a smaller size for compressible materials.

Moreover, the size varies depending on whether the static or the dynamic problem given in \cref{sec:solver_static_hyperelastic_fe_model,sec:solver_dynamic_hyperelasticity_fe_model} is solved. Assuming a linear mesh with $N_p$ degrees of freedom and a quadratic mesh with $N_u$ degrees of freedom, the square system matrix has $3\,N_u$ rows and columns for a static compressible formulation, $3\,N_u + N_p$ for a static incompressible formulation, $6\,N_u$ for a dynamic compressible model, and $6\,N_u+N_p$ for a dynamic incompressible model.

% static compressible:    3*N_u
% static incompressible:  3*N_u + N_p
% dynamic compressible:   3*N_u + 3*N_u
% dynamic incompressible: 3*N_u + 3*N_u + N_p

In any case, the mechanics model can be linked to the subcellular model by defining the active stress as given in \cref{eq:active_stress_term}. Since the active stress does not depend directly on the passive behavior, the active stress term can be added as a constant to the passive stress term. This constant also has no influence on the Jacobian matrix $\bfJ$. As the subcellular model depends on the fiber stretch $\lambda_f = \sqrt{I_4}$, there is a feedback loop between the subcellular and the solid mechanics model.

Details on the connection to the subcellular model as well as details on the numerical solution schemes are given in \cref{sec:solid_mechanics_solver}.

%, including the solver schemes, how initial values are chosen and measures to speed up convergence such as load stepping are discussed in the implementation and result sections.
%To speed up the computation, the initial guess of the vector of unknowns in every timestep is linearly extrapolated from the two previous timesteps.


\section{Task 5}
% We Consider a 16-by-16 torus (tori) interconnection network and determine the following
% interconnection network properties:
Given a n-by-n tori where $n=16$, we determine the requested properties.

\subsection{Task 5.a}
% network diameter
The network diameter is $16$.

\subsection{Task 5.b}
% bisection bandwidth, assuming that each link has a bandwidth of 100 Mbits/s
Bisection width is $2n = 32$, this is multiplied by the link bandwidth giving a
bisection bandwidth of $32 \cdot 100\text{Mb/s} = 3200\text{Mb/s}$.

\subsection{Task 5.c}
% the bandwidth per node
The tori have switch degree 4, so each node have 4 links with each
$100\text{Mb/s}$ this bandwidth needs to be split in both directions so the
bandwith available for each node in each link is $50\text{Mb/s}$ giving a total
bandwidth per node of
\[
\text{switch degree} \cdot \frac{bandwidth}{2} = 4 \cdot
\frac{100}{2} = 200\text{Mb/s}.
\]


\documentclass{article}
\usepackage{fullpage}
\usepackage{nopageno}
\usepackage{amsmath}
\usepackage{amssymb}
\allowdisplaybreaks

\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}

\begin{document}
\title{Notes}
\date{April 9, 2014}
\maketitle
generating function is $\sum\limits_{n=0}^\infty{h_nx^n}$ and exponential generating function is $\sum\limits_{n=0}^\infty{h_nx^n/n!}$

\subsection*{theorem}
let $S$ be the multiset $\{n_1\cdot a_1,n_2\cdot a_2,n_3\cdot a_3,dots,n_k\cdot a_k,\}$and $h_n$ be the number of $n$-permutations of $S$. The exponential generating function for $h_0,h_1,\dots$ is $g^{(e)}(x)=f_{n1}(x)f_{n2}(x)f_{n3}(x)\dots f_{nk}(x)$
\subsubsection*{proof}
see page 224(237)

\subsection*{example}
find $g^{(e)}$ for the number of n-digit numbers with digits 3,5,7 where the \# of 5's is even the number of 3's is more than 1 and at most 4 sevens.

\begin{align*}
  (x^2/2!+x^3/3!+\dots)(1+x^2/2!+x^4/4!+\dots)(1+x+x^2/2!+x^3/3!+x^4/4!)\\
  \left(\sum\limits_{n=0}^\infty{x^{n+2}/(n+2)!}\right)\left(\sum\limits_{n=0}^\infty{x^{2n}/(2n)!}\right)\left(\sum\limits_{n=0}^4{x^{n}/(n)!}\right)\\
  (e^x-1-x)(\frac{e^x+e^{-x}}{2})(...)
\end{align*}

find the number of ways to color the squares of a 1xn chessboard with red blue and green so that the number of red squares is odd and the number of green squares is positive.

\begin{align*}
  \frac{e^x-e^{-x}}{2}e^x\sum\limits_{n=0}^\infty{x^{n+1}/(n+1)!}\\
  \frac{e^x-e^{-x}}{2}e^x(e^x-1)\\
  \frac{e^x-e^{-x}}{2}(e^{2x}-e^x)\\
  \frac{e^{3x}-e^{2x}-e^{x}+1}{2}\\
  \frac{1}{2}+\frac{1}{2}\sum\limits_{n=0}^\infty{(3x)^n/n!-(2x)^n/n!-x^n/n!}\\
\end{align*}
\end{document}


%% Chapter 4 : Model for PV Energy Evaluation

\section{Air Mass Ratio}
\
\
\
\
Solar radiation has to pass through the earth's atmosphere to reach the surface. The atmosphere consists of various components which absorb a part of the solar radiation, hence reducing the solar radiation reaching the earth's surface. Moreover, longer the path the solar radiation takes through the atmosphere, more is the absorption and eventually more is the reduction in the amount of radiation reaching the surface.

\begin{figure}[H]
\centering
\includegraphics[scale=0.5]{m1}
\caption{The Air Mass Ratio is a measure of the amount of atmosphere the Sun's rays must pass through to reach Earth's surface [5]}
\label{figc3h1} %% to refer use, \ref{}
\end{figure}

From the Fig (\ref{figc3h1}) we say that the air mass ratio is the ratio of the actual distance h_{2} taken by the radiation through the atmosphere to reach the surface and the shortest distance h_{1} when the sun is directly overhead, with an additional assumption that the earth's surface is flat. The eq (\ref{amr}) give the air mass ratio formula.

\begin{equation}
\label{amr}
\text{Air Mass Ratio}\quad m=\frac{h_{2}}{h_{1}}=\frac{1}{•\sin{\beta}}
\end{equation}\\
where,\\
$ m $ = Air Mass Ratio \\
$ \beta $ = Altitude angle of the Sun $ (Degrees) $ \\

\section{Earth's Orbit}
\
\
\
\
Earth revolves around the sun in an elliptical orbit, as the eccentricity of this orbit is small the orbit is fairly circular. However, the distance of the earth from the sun varies everyday, with the furthest distance being when earth is at the aphelion and the shortest distance being when earth is at the perihelion. The Fig (\ref{figc4h2}) illusrates this variation in the distance of earth from the sun. We see that the summer solstice and the winter solstice happen at the aphelion and perihelion respectively; whereas the equinoxes happen visually in the midway between aphelion and perihelion.

\begin{figure}[H]
\centering
\includegraphics[scale=0.5]{m2}
\caption{Tilt of the Earth's Axis with respect to the Ecliptic Plane [5]}
\label{figc4h2} %% to refer use, \ref{}
\end{figure}

The eq (\ref{eo}) gives the variation of earth's distance from the sun.

\begin{equation}
\label{eo}
d=1.5\times10^{8}\left \{\ 1+0.017 \sin \left[ \frac{360(n-93)}{365} \right] \right\} \ \text{km}
\end{equation}\\
where,\\
$ d $ = Distance of Earth from Sun $ (km) $\\
$ n $ = Julian day number\\

\section{Solar Declination}
\
\
\
\
Earth revolves around the sun in an elliptical orbit with its north-south axis tilted at an angle of 23.45^{\circ}; this causes the apparent movement of the sun in the day sky to be the highest during the summers in the northern-hemisphere and lowest during the winters. Hence, the sun reaches the highest point in the sky on summer solstice (21^{st} June) when it is directly overhead the Tropic of cancer at latitude 23.45^{\circ}, and it is at the lowest point on the winter solstice (21^{st} December) when it is directly overhead the Tropic of Capricorn at the latitude 23.45^{\circ} below the equator. Moreover on both the vernal (21^{st} March) and autumnal (21^{st} September) equinox the sun is directly overhead the equator at latitude 0^{\circ}. The Fig (\ref{figc4h3}) illustrates this apparent movement of the sun in the day sky.

\begin{figure}[H]
\centering
\includegraphics[scale=0.5]{m3}
\caption{The angle between Sun and Equator is called Solar Declination [5]}
\label{figc4h3} %% to refer use, \ref{}
\end{figure}

Hence, the angle formed between the plane of the equator and the line drawn from the center of the sun to the earth's center is called the solar declination angle, which is given by eq (\ref{sdec}).

\begin{equation}
\label{sdec}
\delta =23.45 \sin \left[ \frac{360}{365} (n-81)\right]
\end{equation}\\
where,\\
$ \delta $ = Declination angle $ (Degrees) $\\

\begin{figure}[H]
\centering
\includegraphics[scale=0.5]{m4}
\caption{A south-facing collector tipped up to an angle equal to its latitude is perpendicular to the Sun's rays at noon during Equinoxes [5]}
\label{figc4h4} %% to refer use, \ref{}
\end{figure}

The Fig (\ref{sdec}) shows the utilization of the solar declination angle in setting up the tilt of a solar panel for maximum energy output. It can be seen that, if the PV module located at a particular latitude is given a tilt equal to the tilt of the latitude the sun would be directly overhead the PV module on the equinoxes giving the best annual energy output performance for a fixed tilt PV module system.

\newpage

\section{Sun's Position}
\
\
\
\
The position of the sun in the sky is described using two angles: the azimuth and the altitude angle. The azimuth angle by convention is the angle made by the with respect to the south, being positive in the eastern direction and negative in the western direction. The altitude angle is angle made by the sun as measured positively from the earth's surface to its vertical position in the sky. The Fig (\ref{figc4h7}) illustrates the azimuth and altitude angles clearly.

\begin{figure}[H]
\centering
\includegraphics[scale=0.5]{m7}
\caption{Sun's Position described by its Altitude angle and Azimuth angle [5]}
\label{figc4h7} %% to refer use, \ref{}
\end{figure}

The eq (\ref{alt},\ref{azm}) give the formulas for the altitude and azimuth angles, it can be seen that they are functions of the latitude, the hour angle and the solar declination.

\begin{equation}
\label{alt}
\sin{(\beta)}=\cos{(L)}\cos{(\delta)}cos{(H)}+\sin{(L)}\sin{(\delta)}
\end{equation}\\
where,\\
$ \beta  $ = Altitude Angle $ (Degrees) $\\
$ H $ = Hour Angle $ (Degrees) $\\

\begin{equation}
\label{azm}
\sin{(\phi_{S})}=\frac{\cos{(\delta)}\sin{(H)}}{\cos{(\beta)}}
\end{equation}\\
where,\\
$ \phi_{S}  $ = Azimuth Angle of Sun $ (Degrees) $\\

The Fig (\ref{figc4h9}) illustrates the concept of the hour angle. It is the number of degrees that the earth rotates before the sun is directly overhead the local meridian (line of longitude passing through the location). So, the hour angle can also be defined as the difference between the local and the sun's meridian; it is positive till the point the sun crosses the local meridian ,and then on negative.

\begin{figure}[H]
\centering
\includegraphics[scale=0.5]{m8}
\caption{The Hour angle is he number of degrees the Earth must turn before the sun is directly over the local meridian [5]}
\label{figc4h9} %% to refer use, \ref{}
\end{figure}

The eq (\ref{ha}) gives the formula for the hour angle. The factor $ \frac{15^{\circ}}{\text{hour}} $ appears as the earth takes one hour to rotate through 15^{\circ}.

\begin{equation}
\label{ha}
\text{Hour Angle}\ H=\left(\frac{15^{\circ}}{\text{hour}}\right).(\text{hours before solar noon})
\end{equation}

The eq (\ref{hac}) provides the necessary condition for determining if th azimuth is greater or lesser than 90^{\circ} away from the south. This condition has to be applied because during spring and summer in the early morning and late afternoon the magnitude of sun's azimuth is liable to be more than 90^{\circ}; which would eventually cause the ambiguous nature of the inverse sine in eq (\ref{azm})to manifest itself.

\begin{equation}
\label{hac}
\text{if,} \quad \cos{(H)}\geq\frac{\tan{(\delta)}}{\tan{(L)}}; \hspace{1cm} \text{then,} \ |\phi_{S}|\leq90^{\circ}; \hspace{1cm} \text{otherwise,} \ |\phi_{S}|>90^{\circ}
\end{equation}

\begin{figure}[H]
\centering
\includegraphics[scale=0.5]{SunPath1}
\caption{Sun Path Diagram generated by the Sun Path Diagram App}
\label{figc4h8} %% to refer use, \ref{}
\end{figure}

The Fig (\ref{figc4h8}) shows the output of the Sun Path Diagram App developed, for the Julian Day of 100 at 30^{\circ} latitude at a resolution of 5 minutes. The red and blue curves show the summer and winter soltice sun paths, whereas the green curve shows the sun path during the two equinoxes, and the black curve shows the sun path for the 100^{th} Julian Day.

\newpage

\section{Relationship Between Solar Time and Civil Time}

The Solar Time (ST) is the time, where everything is measured relative to the solar noon at the longitude of the location, whereas the  Civil Time (CT) is the time, where everything is measured relative to the solar noon at the longitude of the regional time zone. In order to have a connection between the ST and CT two adjustments : Longitude adjustment and Equation of time adjustment, have to be done.\\

The Longitude correction involves the time taken by the sun to travel between the regional time meridian and the location time meridian. It takes 4 minutes for the sun to pass through 1^{\circ} of longitude due to the earth's rotation. Hence the factor of $ \frac{4 \ \text{min}}{\text{degree}} $ is seen in the eq (\ref{solartime}).\\

The Equation of time adjustment is required due to the earth's ellipticl orbit around the sun, which causes the length of the solar day (Solar noon to solar noon) to vary throughout the year. The Fig (\ref{figc4h10}) shows the variation in the length of the solar day throughout the year.

\begin{figure}[H]
\centering
\includegraphics[scale=0.5]{m10}
\caption{The Equation of Time plotted for all year days [5]}
\label{figc4h10} %% to refer use, \ref{}
\end{figure}

The eq (\ref{eot}) give the expression for calculating equation of time.

\begin{equation}
\label{eot}
E= 9.87\sin{(2B)} -7.53\cos{(B)} -1.5\sin{(B)} \quad \text{minutes}
\end{equation}

\begin{equation}
\label{eotb}
B=\frac{360}{364}(n-81)
\end{equation}\\
where,\\
$ E $ = Equation of Time $ (mins) $ \\
$ B $ = Equation of Time angle $ (Degrees) $

Finally, combining everything together we get the connection between the ST and CT whic is given by the eq (\ref{solartime}).

\begin{equation}
\label{solartime}
\text{Solar Time (ST)}= \text{Clock Time (CT)}+ \frac{4 \ \text{min}}{\text{degree}}(\text{Local Time Meridian}- \text{Local Logitude})^{\circ} + E(\text{min})
\end{equation}

\subsection{Sunrise and Sunset Times}
\
\
\
\
The eq (\ref{alt},\ref{azm}) can be used to calculate the sun rise and sun set times approximately. These equations are solve for the hour angle  by putting \beta = 0 (since altitude angle of the sun during sun rise and set is zero) as seen in eq (\ref{sunrise1},\ref{sunrise2}),

\begin{equation}
\label{sunrise1}
\sin{(\beta)}=\cos{(L)}\cos{(\delta)}cos{(H)}+\sin{(L)}\sin{(\delta)}=0
\end{equation}

\begin{equation}
\label{sunrise2}
\cos{(H)}=-\frac{\sin{(L)}\sin{(\delta)}}{\cos{(L)}\cos{(\delta)}}=-\tan{(L)}\tan{(\delta)}
\end{equation}

Hence, solving for the hour angle we get eq (\ref{sunrise3}).

\begin{equation}
\label{sunrise3}
H_{SR}=\cos^{-1}{(-\tan{(L)}\tan{(\delta)})} \quad \text{(+ for sunrise)}
\end{equation}

As we have see that the earth rotates $ 15^{\circ}/h $, we get the geometric sunrise time from the eq (\ref{sunrise4}).

\begin{equation}
\label{sunrise4}
\text{Sunrise(geometric)}= 12:00-\frac{H_{SR}}{15^{\circ}/h}
\end{equation}

The geometric sun rise time refers to the point in time when the sun's center has crossed the horizon, which makes it inaccurate. Moreover, the the refraction caused by the atmosphere causes the sun to appear to rise about 2.4 minutes earlier than the geometric sun rise time and set 2.4 minutes later. Also, the definition of sun rise and sun set according to the weather services is the time when the upper limb of the sun crosses the horizon, but for us it is the crossing of the sun's center across the horizon. Futhermore, this is complicated even more by the fact that the sun rises and sets quicker around equinoxes than around the solstices due to the additional sideward movement the the later periods. Hence, in order to adjust to all these factors an adjustment factor called Q is given by the U.S. Department of Energy in 1978, its expression is given in eq (\ref{sunrise5}).

\begin{equation}
\label{sunrise5}
Q=\frac{3.467}{\cos{(L)}\cos{(\delta)}\sin{(H_{SR})}} \quad (\text{min})
\end{equation}\\
where,\\
$ Q $ = Adjustment factor $ (mins) $ \\

\newpage

\section{Types of Radiation}
\
\
\
\
The solar radiation striking the solar collector (PV modules) consists of three components. The first one is the direct-beam radiation, it is the part of solar radiation which passes in a straight line through the atmosphere to the solar collector. The second is the diffuse radiation, it the part of solar radiation which is scattered by the molecules and aerosols. Finally the last one is the reflected radiation, it is the part of solar radiation which gets reflected from the ground surface onto the solar collectors surface. The Fig (\ref{figc4h11}) illustrates the three components of solar radiation which are impressed on the solar collectors surface.\\

Note: All the equations henceforth for solar radiation were developed by Threkeld and Jordan (1958), these equations are used in the AHRAE Clear-Day Solar Flux Model.

\begin{figure}[H]
\centering
\includegraphics[scale=0.5]{m11}
\caption{Solar radiation striking a collector is a combination of Direct Beam, Diffuse and Reflected radiations [5]}
\label{figc4h11} %% to refer use, \ref{}
\end{figure}

The Fig (\ref{figc4h12}) shows the extra-terrestrial solar insolation, which is the the solar radiation passing perpendicularly through an imaginary surface just outside earth's surface.

\begin{figure}[H]
\centering
\includegraphics[scale=1]{m12}
\caption{Extraterrestrial Solar Flux [5]}
\label{figc4h12} %% to refer use, \ref{}
\end{figure}

The eq (\ref{etr1}) gives the expression for the extra-terrestrial solar insolation.

\begin{equation}
\label{etr1}
I_{0}=\text{SC}. \left[1+0.0334\cos\left(\frac{360n}{365}\right)\right] \quad (\text{W/m}^{2})
\end{equation}\\
where,\\
$ I_{0} $ = Extra Terrestrial Solar Insolation $ (W/m^{2}) $ \\
$ SC $ = Solar constant $ (1.353 kW/m^{2}) $ \\

\subsection{Beam Radiation}
\
\
\
\
The earth's atmosphere with its mixture of gases and man made aerosols causes the attenuation of the beam radiation passing through it due to absorption. The attenuation can be quantified approximately using the exponential decal function as given in the eq (\ref{beam1}).

\begin{equation}
\label{beam1}
I_{B}=Ae^{-km}
\end{equation}\\
where,\\
$ I_{B} $ = Beam portion of the radiation $ (W/m^{2})$\\
$ A $ = Apparent Extra-Terrestrial Flux $ (kW/m^{2})$\\
$ k $ = Optical depth \\

A given in eq (\ref{beam2}) is the apparent extra-terrestrial insolation, k given in eq (\ref{beam3}) is a dimenionless quantity called the optical depth and m is the air mass ratio as computed in eq (\ref{amr}).

\begin{equation}
\label{beam2}
A=1160+75\sin\left[\frac{360}{365}(n-275)\right] \quad (\text{W/m}^{2})
\end{equation}

\begin{equation}
\label{beam3}
k=0.174+0.035\sin\left[\frac{360}{365}(n-100)\right]
\end{equation}

The Fig (\ref{figc4h13}) illustrayes the direct beam radiation incident on a solar collector.

\begin{figure}[H]
\centering
\includegraphics[scale=1]{m14}
\caption{Incidence angle between Sun and normal to the collector surface [5]}
\label{figc4h13} %% to refer use, \ref{}
\end{figure}

The component of direct beam radiation striking the surface of the solar collector is given by the eq (\ref{beamc1}).

\begin{equation}
\label{beamc1}
I_{BC}=I_{B}\cos{(\theta)}
\end{equation}\\
where,\\
$ I_{BC} $ = Beam radiation normal to the surface of the collector $ (W/m^{2})$\\
$ \theta $ = Angle between a line drawn normal to the solar collector face and the incoming beam
radiation $ (Degrees)$\\

The Fig (\ref{figc4h14) shows a complicated geometry of a solar collector, which has a tilt angle and an azimuth angle. The computation of the incidence angle of this solar collector is done with the eq (\ref{beamc3).

\begin{figure}[H]
\centering
\includegraphics[scale=1]{m13}
\caption{Solar and Collector Azimuth and Altitude angles along with Collector Tilt angle [5]}
\label{figc4h14} %% to refer use, \ref{}
\end{figure}

\begin{equation}
\label{beamc3}
\cos{(\theta)}=\cos{(\beta)}\cos{(\phi_{S}-\phi_{C})}\sin{(\Sigma)}+\sin{(\beta)}\cos{(\Sigma)}
\end{equation}\\
where,\\
$ \phi_{S} $ = Azimuth angle of Sun $ (Degrees)$\\
$ \phi_{C} $ = Azimuth angle of the solar collector $ (Degrees)$\\
$ \sigma $ = Tilt angle of the solar collector $ (Degrees)$\\

\subsection{Diffuse Radiation}
\
\
\
\
The computation of diffuse radiation is complicated, as it includes the radiation scattered by the atmospheric gases, moisture etc., and also the radiation reflected from the surface which is scattered again by the atmosphere back to ground. It is fair to assume diffuse radiation to be coming with equal intensity from all directions (sky is assumed to be isotropic).

\begin{figure}[H]
\centering
\includegraphics[scale=1]{m16}
\caption{Diffuse radiation on a collector is assumed to be proportional to the fraction of the sky that the collector "sees"  [5]}
\label{figc4h15} %% to refer use, \ref{}
\end{figure}

The eq (\ref{diff1})shows that the diffuse radiation is always dirrectly proportional to the beam radiation, where C given by the eq (\ref{diff2}) is the sky diffuse factor.

\begin{equation}
\label{diff1}
I_{DH}=CI_{B}
\end{equation}\\
where,\\
$ I_{DH} $ = Diffused portion of the radiation striking the solar collector $ (W/m^{2})$\\
$ C $ = Sky-Diffuse factor\\

\begin{equation}
\label{diff2}
C=0.095+0.04\sin\left[\frac{360}{365}(n-100)\right]
\end{equation}

The diffuse radiation striking the solar collector depend on the amount of sky seen by the collector. Moreover, the amount of sky seen by a solar collector is inversely proportional to its tilt angle in a sinusoidal way as given in the eq (\ref{diffc1}).

\begin{equation}
\label{diffc1}
I_{DC}=I_{DH}\left(\frac{1+\cos{(\Sigma)}}{2}\right)=CI_{B}\left(\frac{1+\cos{(\Sigma)}}{2}\right)
\end{equation}

\subsection{Reflected Radiation}
\
\
\
\
The beam and diffuse radiation incident on the ground surface, results in the their reflection off the ground surface. In the simplest model it is fair to assume that the reflected radiation from the ground surface is in equal intensity in all directions as illustrated in the Fig (\ref{figc4h16}).

\begin{figure}[H]
\centering
\includegraphics[scale=1]{m15}
\caption{The ground is assumed to reflect radiation with equal intensities in all directions [5]}
\label{figc4h16} %% to refer use, \ref{}
\end{figure}

The Component reflected radiation striking the solar collector depend on the amount of ground seen by the collector. Moreover, the amount of ground seen by a solar collector is directly proportional to its tilt angle in a sinusoidal way as given in the eq (\ref{ref1}).

\begin{equation}
\label{ref1}
I_{RC}=\rho(I_{BH}+I_{DH})\left(\frac{1-\cos{(\Sigma)}}{2}\right)
\end{equation}\\
where,\\
$ I_{RC} $ = Reflected portion of the radiation striking the solar collector $ (W/m^{2})$\\
$ \rho $ = Ground Reflectance $ (Range 0.1-0.8) $\\

\subsection{Total Solar Radiation}
\
\
\
\
The total radiation striking the solar collector is the sum of the beam, diffuse and reflected  as computed in the previous sections, it is given in the eq (\ref{totc1}).

\begin{equation}
\label{totc1}
I_{C}=I_{BC}+I_{DC}+I_{RC}
\end{equation}\\
where,\\
$ I_{C} $ = Total radiation incident on the solar collector $ (W/m^{2})$

A more detailed equation for the total solar radiation striking the solar collector is given by eq (\ref{totc2}), by substituting eq (\ref{beamc1},\ref{diffc1},\ref{ref1}) in eq (\ref{totc1}).

\begin{equation}
\label{totc2}
    \begin{aligned}
        I_{C}
        =Ae^{-km} \left[ \cos{(\beta)}\cos{(\phi_{S}-\phi_{C})}\sin{(\Sigma)} \\
       & + \sin{(\beta)}\cos{(\Sigma)} + C\left(\frac{1+\cos{(\Sigma)}}{2}\right) \\ \\
      & +\rho(\sin{(\beta)}+C)\left.\left(\frac{1-\cos{(\Sigma)}}{2}\right)\right]
   \end{aligned}
\end{equation}

\section{Radiation Equations}
\
\
\
\
The equation developed in the previous sections for the beam, diffuse and reflected radiation striking the solar collector are modified for different orientations of the Solar PV modules: Fixed Tilt, Seasonal Tilt, Single Axis Tracker and Double Axis Tracker, and presented in the following sub-sections\\

\subsection{Fixed Tilt Equations}
\
\
\
\
The equations for the beam, diffuse and reflected radiations striking the solar PV module in a fixed tilt orientation are given by eq (\ref{ft1},\ref{ft2},\ref{ft3}).

\begin{equation}
\label{ft1}
I_{BC}=I_{B}\cos{(\theta)}
\end{equation}

\begin{equation}
\label{ft2}
I_{DC}=CI_{B}\left(\frac{1+\cos{(\Sigma)}}{2}\right)
\end{equation}

\begin{equation}
\label{ft3}
I_{RC}=\rho I_{B}(\sin{(\beta)}+C)\left(\frac{1-\cos{(\Sigma)}}{2}\right)
\end{equation}

\subsection{Seasonal Tilt Equations}
\
\
\
\
The equations for the beam, diffuse and reflected radiations striking the solar PV module in a fixed tilt orientation are given by eq (\ref{st1},\ref{st2},\ref{st3}).

\begin{equation}
\label{st1}
I_{BC}=I_{B}\cos{(\theta_{\text{Tilt Type}})}
\end{equation}

\begin{equation}
\label{st2}
I_{DC}=CI_{B}\left(\frac{1+\cos{(\Sigma_{\text{Tilt Type}})}}{2}\right)
\end{equation}

\begin{equation}
\label{st3}
I_{RC}=\rho I_{B}(\sin{(\beta)}+C)\left(\frac{1-\cos{(\Sigma_{\text{Tilt Type}})}}{2}\right)
\end{equation}\\
where,\\
\textit{Tilt Type} = It can be normal, summer and winter season tilt\\

\subsection{Single-Axis Tracker Equations}
\
\
\
\
The equations for the beam, diffuse and reflected radiations striking the solar PV module in a fixed tilt orientation are given by eq (\ref{sat1},\ref{sat2},\ref{sat3}).

\begin{equation}
\label{sat1}
\Sigma_{effective}=90^{\circ}-\beta +\delta
\end{equation}

\begin{equation}
\label{sat2}
I_{BC}=I_{B}\cos{(\delta)}
\end{equation}

\begin{equation}
\label{sat3}
I_{DC}=CI_{B} \left[ \frac{1+\cos{(90^{\circ}-\beta+\delta)}}{2} \right]
\end{equation}

\begin{equation}
\label{sat4}
I_{RC}=\rho(I_{BH}+I_{DH})\left[\frac{1-\cos{(90^{\circ}-\beta+\delta)}}{2}\right]
\end{equation}

\subsection{Dual Axis Tracker Equations}
\
\
\
\
The equations for the beam, diffuse and reflected radiations striking the solar PV module in a fixed tilt orientation are given by eq (\ref{tda1},\ref{tda2},\ref{tda3}).

\begin{equation}
\label{tda1}
I_{BC}=I_{B}
\end{equation}

\begin{equation}
\label{tda2}
I_{DC}=CI_{B} \left[ \frac{1+\cos{(90^{\circ}-\beta)}}{2} \right]
\end{equation}

\begin{equation}
\label{tda3}
I_{RC}=\rho(I_{BH}+I_{DH})\left[\frac{1-\cos{(90^{\circ}-\beta)}}{2}\right]
\end{equation}

\newpage

\section{Power Flow through Solar PV Grid Connected System}
\
\
\
\
The Fig (\ref{PVloss}) illustrates the power flow through a solar PV system and the various losses incurred in the this system.

\begin{figure}[H]
\centering
\includegraphics[scale=0.80]{SolarPVLosses}
\caption{Flow Diagram - Power Flow and Power Losses in a Solar PV System}
\label{PVloss} %% to refer use, \ref{}
\end{figure}

\subsection{Shading, Soiling and Incidence Angle Loss}
\
\
\
\
As seen in chapter 2, shading of PV modules leads to a reduction in power output. Shading is caused by shadows cast by near (near shading) as well as far (far shading) objects. These shadows inhibit the solar radiation from striking the PV modules causing shading and the subsequent loss in power output. To avoid shading losses the design of the PV arrays should be such that they do not cast shadow on each other, any near by objects casting shadow on the PV modules should be removed and the location of the PV system should be such that no far away objects cast shadow on the modules.\\

Soil and dust accumulated on the PV module surface reduces the amount of radiation striking the module surface causing reduction in power output. To avoid power loss due to soiling regular cleaning of PV modules has to be performed.\\

As the surface of the PV module is made out of glass there is some radiation which is reflected from the PV module surface. This results in module power loss due to the incidence angle of the beam radiation. The ASHRAE (American Society of Heating, Refrigeration, and Air Conditioning ) model for IAM is given in eq (\ref{iam}). It is a simple model as it requires only one parameter $ b_{0} $, however its accuracy reduces at higher incidence angles (Abella et al., 2003).

\begin{equation}
\label{iam}
IAM=1-b_{0} \left[ \frac{1}{ \cos {(\theta)}}-1 \right]
\end{equation}\\
where,\\
$ IAM $ = Incidence Angle Modifier\\
$ b_{0} $ = Constant\\
$ \theta $ = Incidence Angle  $ ^{\circ}C $\\

From the Fig (\ref{IAM1}) it can be observed that the IAM factor reduces from 1 at 0^{\circ} to 0 at 90^{\circ}. Hence, more the incident angle more is the reflection of the radiation from the PV module surface  . To avoid IAM losses significantly single axis or double axis tracker systems are used as they always try to maintain the incidence angle to the minimum (especially the double axis tracking system).

\begin{figure}[H]
\centering
\includegraphics[scale=0.75]{IAM}
\caption{ASHRAE IAM Model}
\label{IAM1} %% to refer use, \ref{}
\end{figure}

\subsection{Effect of Temperature and Irradiance on PV Module Output}
\
\
\
\
The PV module rated power given in the datasheet by the manufacturer is experimentally measured under the STC conditions of 25^{\circ}C temperature and 1000 W/m^{2} of irradiance. However, in the field the temperature and irradiance levels change continuously throughout the day and the year depending on the location of the PV system.\\

As we have seen in chapter 2, PV module power is directly proportional to solar irradiance and inversely proportional to temperature, there will be conditions when the module will generate lower than rated power (due to either high temperature or low irradiance or both), similarly thre can be conditions when the module generates more than the rated power (due to either low temperature or high irradiance or both). The eq (\ref{PVTemIrr}) gives the relationship between the actual power generated by the module.

\begin{equation}
\label{PVTemIrr}
P_{Actual}=P_{Rated}\left[1-\bigtriangledown T]\right \frac{G_{Actual}}{G_{STC}}
\end{equation}\\
where,\\
$ P_{Actual} $ = Actual Power generated by the PV mdule $ (W) $\\
$ P_{Rated} $ = PV module rated power $ (W) $\\
$ \bigtriangledownT $ = Difference between Ambient and STC temperatures $ (^{\circ}C)$\\
$ G_{Actual} $ = Actual Solar Irradiance $ (W/m^{2}) $\\
$ G_{STC} $ = STC Solar Irradiance $ (W/m^{2}) $\\

\subsection{Array Mismatch and Ohmic Loss}
\
\
\
\
Mismatch losses are caused due to the differences in the outputs of individual modules in an array. The major differences between modules are of two kinds: difference in open-circuit voltage and/or difference in short-circuit current. These difference eventually cause lowering of the power output of the entire array.\\

The Fig (\ref{MisMat1}) shows two modules connected in series. In a series connection voltages of individual modules get added to give the total voltage of the string and the current flowing through all modules is the same. Hence, in this case the difference in open-circuit voltage is benign, but difference in short-circuit current is severe as the current output of the entire string is determined by the lowest current producing module, this severely reduces the the power output of the entire string.

\begin{figure}[H]
\centering
\includegraphics[scale=0.75]{Mismatch1}
\caption{Mismach in Series Connnected Modules [PV Education]}
\label{MisMat1} %% to refer use, \ref{}
\end{figure}

Similarly in Fig (\ref{MisMat2}) shows two modules connected in parallel. In a parallel connection currents of individual modules get added to give the total current of the array and the voltage across all modules is the same. Hence, in this case the difference in  short-circuit curren is benign, but difference in open-circuit voltage is severe as the voltage output of the entire array is determined by the lowest voltage producing string, this severely reduces the the power output of the entire array. Hence, in order to reduce these array mismatch losses the manufacturing process of the modules have to be standardized so that every module is exactly similar to the other.

DC cabling is done to interconnect modules within a string, strings within an array and then to connect the PV system to the inverters. Also, AC cabling is done to connect the inverters to the transformer, and the transformer to the grid. All these cables add to the resistance of the PV system causing huge ohmic losses. Hence, in order to reduce these ohmic losses, the length of the cabling should be minimum and the cable material should be of low resistivity.

\begin{figure}[H]
\centering
\includegraphics[scale=1]{Mismatch2}
\caption{Mismach in Parallely Connnected Modules [PV Education]}
\label{MisMat2} %% to refer use, \ref{}
\end{figure}

\subsection{Inverter and Transformer Loss}
\
\
\
\
Inverters are at the heart of the Solar PV system, the convert the DC power generated in modules to AC power which can be fed to the grid. Additionally they perform the task of maximum power point tracking, which helps in extracting maximum power from the PV modules at every temperature and irradiance condition. However, inverters are power electronic devices which are made up of high frequency switching devices; the switching devices cause loss in power due to the swtiching losses. To avoid inverter losses, a good quality inverter should be used.\\

Transformers are another indispensible part of the grid connected Solar PV systems. They step-up the voltage of the PV system output, so that the power can be transferred to the transmission lines operating at higher voltages. But, transformers consist of copper coils which result in resistive losses, and iron cores which cause iron losses. Hence, the power output to the grid from the the PV system is reduced.

\newpage

\section{MATLAB Model Algorithm}

The Fig (\ref{figc4halgo1} ) depicts an simplified flowchart of the algorithm which is developed for the creation of the Solar Energy Estimation Application.

\begin{figure}[H]
\centering
\includegraphics[scale=0.5]{SolarAppAlgorithm}
\caption{Solar Photovoltaic Energy Estimation Schematic}
\label{figc4halgo1} %% to refer use, \ref{}
\end{figure}

\newpage

Based on the theory discussed in previous sections of this chapter and the algorithm developed as illustrated in Fig (\ref{PVloss},\ref{figc4halgo1}); a GUI based application for energy estimation solar photovoltaic grid connected power plants is developed in MATLAB, the results of which are presented in the next section and the application GUIs can be found in the Appendix.

\section{Results of Solar Energy Estimation App}
\
\
\
\
This section illustrates the results of energy simulation of Backbone 5MW Solar Power Plant. It is situated in Samakhiyali, Kutch, Gujarat. It has 145W Thin Film modules which are supported by a single axis east-west tracker system. Monthly enrgy output of the plant have been simulated in both the developed MATLAB application and then a comparison is done with the PVsyst simulation of the same plant.\\

The Table (\ref{SolarAppTab1}) gives the site and PV module information of the Backbone 5MW SPVP.

\begin{table}[H]
  \centering
  \caption{BackBone 5MW SPVP Information Table}
    \begin{tabular}{|l|c|}
    \hline
    \multicolumn{2}{|c|}{\textbf{BackBone 5MW SPVP}} \bigstrut\\
    \hline
    \multicolumn{2}{|c|}{\textbf{SITE INFORMATION}} \bigstrut\\
    \hline
    \textbf{LATITUDE} & 23.357 \bigstrut\\
    \hline
    \textbf{LONGITUDE} & 70.358 \bigstrut\\
    \hline
    \textbf{PLANT CAPACITY (MW)} & 5 \bigstrut\\
    \hline
    \multicolumn{2}{|c|}{\textbf{PV MODULE INFORMATION}} \bigstrut\\
    \hline
    \textbf{DESCRIPTION} & Micro-Amorphous \bigstrut\\
    \hline
    \textbf{MAKE} & NT-145AX \bigstrut\\
    \hline
    \textbf{CRYSTALLINE/THIN-FILM} & Thin-Film \bigstrut\\
    \hline
    \textbf{RATING (W)} & 145 \bigstrut\\
    \hline
    \textbf{Vmpp (V)} & 64.2 \bigstrut\\
    \hline
    \textbf{Impp (A)} & 2.26 \bigstrut\\
    \hline
    \textbf{Voc (V)} & 85.5 \bigstrut\\
    \hline
    \textbf{Isc (A)} & 2.54 \bigstrut\\
    \hline
    \textbf{TEMP COEFF OF Voc} & -0.32 \bigstrut\\
    \hline
    \textbf{TEMP COEFF OF Isc} & 0.07 \bigstrut\\
    \hline
    \textbf{TEMP COEFF OF Pmp} & -0.28 \bigstrut\\
    \hline
    \textbf{TOTAL MODULES} & 34485 \bigstrut\\
    \hline
    \textbf{LENGTH (mm)} & 1412 \bigstrut\\
    \hline
    \textbf{BREADTH (mm)} & 1112 \bigstrut\\
    \hline
    \textbf{AREA (m2)} & 1.570144 \bigstrut\\
    \hline
    \end{tabular}%
  \label{SolarAppTab1}%
\end{table}%

Figure (\ref{PVG1}) hows the graph of energy output of Backbone 5MW SPV for the year 2014 for different orientations computed using developed MATLAB application.

\begin{figure}[H]
\centering
\includegraphics[scale=1]{PVresultG1}
\caption{MATLAB Application Backbone Plant month-wise energy estimation for different orientations}
\label{PVG1} %% to refer use, \ref{}
\end{figure}

In the tables FT-Fixed Tilt, ST-Seasonal Tilt, E-W-Single Axis East-West Tracker, N-S-Single Axis North-South Tracker and D-A- Double Axis Tracker\\

\begin{table}[H]
  \centering
  \caption{MATLAB BackBone Application Simulation}
    \begin{tabular}{|l|r|r|r|r|r|r|}
    \hline
    \multicolumn{7}{|c|}{\textbf{MATLAB BackBone Application Simulation}} \bigstrut\\
    \hline
       & \multicolumn{1}{l|}{\textbf{Actual }} & \multicolumn{1}{l|}{\textbf{FT}} & \multicolumn{1}{l|}{\textbf{ST}} & \multicolumn{1}{l|}{\textbf{E-W}} & \multicolumn{1}{l|}{\textbf{N-S }} & \multicolumn{1}{l|}{\textbf{D-A}} \bigstrut\\
    \hline
    \textbf{Jan} & 624 & 908 & 968 & 674 & 1095 & 1134 \bigstrut\\
    \hline
    \textbf{Feb} & 678.7 & 876 & 909 & 712 & 1014 & 1064 \bigstrut\\
    \hline
    \textbf{Mar} & 917.5 & 1087 & 1066 & 992 & 1215 & 1317 \bigstrut\\
    \hline
    \textbf{Apr} & 997.4 & 817 & 832 & 800 & 859 & 1029 \bigstrut\\
    \hline
    \textbf{May} & 1030 & 787 & 832 & 858 & 845 & 1045 \bigstrut\\
    \hline
    \textbf{Jun} & 921 & 708 & 741 & 822 & 744 & 944 \bigstrut\\
    \hline
    \textbf{Jul} & 693 & 575 & 575 & 640 & 617 & 764 \bigstrut\\
    \hline
    \textbf{Aug} & 646.9 & 613 & 613 & 633 & 670 & 782 \bigstrut\\
    \hline
    \textbf{Sep} & 682.8 & 640 & 640 & 602 & 714 & 769 \bigstrut\\
    \hline
    \textbf{Oct} & 697.7 & 763 & 763 & 641 & 871 & 902 \bigstrut\\
    \hline
    \textbf{Nov} & 562.7 & 829 & 878 & 628 & 987 & 1011 \bigstrut\\
    \hline
    \textbf{Dec} & 619.9 & 949 & 1020 & 685 & 1160 & 1191 \bigstrut\\
    \hline
    \end{tabular}%
  \label{PVresultTab1}%
\end{table}%

From Figure (\ref{PVG1}) and Table (\ref{PVresultTab1}) we see that the MATLAB application estimates the month-wise energy estimation of Backbone 5MW SPV for different orientation types. It can be seen that that there is a consistent increase in estimated energy from FT to D-A; the only outlier being the E-W estimation, this is because the tracking system has maximum and minimum azimuths of 45 and -45 respectively, and it would have been beneficial to have larger azimuths for tracking.\\

Figure (\ref{PVG2}) shows the graph of energy output of Backbone 5MW SPV for different orientations computed using PVsyst.

\begin{figure}[H]
\centering
\includegraphics[scale=1]{PVresultG2}
\caption{PVsyst Backbone Plant month-wise energy estimation for different orientations}
\label{PVG2} %% to refer use, \ref{}
\end{figure}

\begin{table}[H]
  \centering
  \caption{Pvsyst BackBone Simulation Results}
    \begin{tabular}{|l|r|r|r|r|r|r|}
    \hline
    \multicolumn{7}{|c|}{\textbf{Pvsyst BackBone Simulation Results}} \bigstrut\\
    \hline
       & \multicolumn{1}{l|}{\textbf{Actual }} & \multicolumn{1}{l|}{\textbf{FT}} & \multicolumn{1}{l|}{\textbf{ST}} & \multicolumn{1}{l|}{\textbf{E-W}} & \multicolumn{1}{l|}{\textbf{N-S }} & \multicolumn{1}{l|}{\textbf{D-A}} \bigstrut\\
    \hline
    \textbf{Jan} & 624 & 866 & 967 & 908 & 985 & 1176 \bigstrut\\
    \hline
    \textbf{Feb} & 678.7 & 789 & 820 & 874 & 841 & 1023 \bigstrut\\
    \hline
    \textbf{Mar} & 917.5 & 943 & 903 & 1155 & 975 & 1250 \bigstrut\\
    \hline
    \textbf{Apr} & 997.4 & 858 & 872 & 1146 & 895 & 1189 \bigstrut\\
    \hline
    \textbf{May} & 1030 & 850 & 874 & 1183 & 923 & 1210 \bigstrut\\
    \hline
    \textbf{Jun} & 921 & 746 & 771 & 1028 & 825 & 1049 \bigstrut\\
    \hline
    \textbf{Jul} & 693 & 585 & 601 & 742 & 637 & 751 \bigstrut\\
    \hline
    \textbf{Aug} & 646.9 & 574 & 586 & 710 & 610 & 721 \bigstrut\\
    \hline
    \textbf{Sep} & 682.8 & 741 & 745 & 903 & 761 & 952 \bigstrut\\
    \hline
    \textbf{Oct} & 697.7 & 859 & 870 & 1003 & 903 & 1144 \bigstrut\\
    \hline
    \textbf{Nov} & 562.7 & 799 & 872 & 845 & 887 & 1058 \bigstrut\\
    \hline
    \textbf{Dec} & 619.9 & 804 & 911 & 808 & 929 & 1081 \bigstrut\\
    \hline
    \end{tabular}%
  \label{PVresultTab2}%
\end{table}%

From Figure (\ref{PVG2}) and Table (\ref{PVresultTab2}) we see the month-wise energy estimation of Backbone 5MW SPV for different orientation types computed using PVsyst. It can be seen that that there is a consistent increase in estimated energy from FT to D-A; here too the only outlier being the E-W estimation.\\

Table (\ref{PVresultTab3}) shows the month-wise percentage errors in the energy estimation of Backbone plant for different orientations computed using the developed MATLAB application with PVsyst being the reference.

\begin{table}[H]
  \centering
  \caption{Comparison of PVsyst and MATLAB Application}
    \begin{tabular}{|l|r|r|r|r|r|}
    \hline
    \multicolumn{6}{|c|}{\textbf{Comparison of PVsyst and MATLAB Application}} \bigstrut\\
    \hline
       & \multicolumn{1}{l|}{\textbf{FT}} & \multicolumn{1}{l|}{\textbf{ST}} & \multicolumn{1}{l|}{\textbf{E-W}} & \multicolumn{1}{l|}{\textbf{N-S }} & \multicolumn{1}{l|}{\textbf{D-A}} \bigstrut\\
    \hline
    \textbf{Jan} & -4.8 & -0.1 & 25.7 & -11.1 & 3.6 \bigstrut\\
    \hline
    \textbf{Feb} & -11.1 & -10.8 & 18.6 & -20.6 & -4 \bigstrut\\
    \hline
    \textbf{Mar} & -15.3 & -18 & 14.1 & -24.6 & -5.4 \bigstrut\\
    \hline
    \textbf{Apr} & 4.8 & 4.5 & 30.2 & 4  & 13.4 \bigstrut\\
    \hline
    \textbf{May} & 7.4 & 4.8 & 27.4 & 8.5 & 13.6 \bigstrut\\
    \hline
    \textbf{Jun} & 5.2 & 3.8 & 20 & 9.8 & 10 \bigstrut\\
    \hline
    \textbf{Jul} & 1.7 & 4.4 & 13.7 & 3.1 & -1.8 \bigstrut\\
    \hline
    \textbf{Aug} & -6.7 & -4.7 & 10.8 & -9.9 & -8.5 \bigstrut\\
    \hline
    \textbf{Sep} & 13.7 & 14.2 & 33.3 & 6.1 & 19.3 \bigstrut\\
    \hline
    \textbf{Oct} & 11.2 & 12.3 & 36.1 & 3.6 & 21.1 \bigstrut\\
    \hline
    \textbf{Nov} & -3.8 & -0.7 & 25.6 & -11.3 & 4.5 \bigstrut\\
    \hline
    \textbf{Dec} & -18.1 & -11.9 & 15.3 & -24.9 & -10.2 \bigstrut\\
    \hline
    \end{tabular}%
  \label{PVresultTab3}%
\end{table}%

From (\ref{PVresultTab3}) it is observed that the MATLAB application has underestimated the energy production on majority of the times as compared to PVsyst, also it can be seen that the errors for E-W is abnormally higher than other orientation types. If we consider absolute values of the errors the average error percentage between PVsyst and MATLAB application is 12.3\%, moreover if we do not take into account E-W values the error reduces to 9.3\%. This shows that the energy estimation model developed is computing reliable estimation of energy as it is using the site data and not the generalized data used by PVsyst.\\

Figure (\ref{PVG5}) shows a comparison between the energy estimation of the Backbone 5MW SPV, with its actual orientation of single axis east-west tracker computed using PVsyst (E-W1, associate error is Error1, in Table (\ref{PVresultTab4})) and MATLAB application (E-W2, associated error is Error2, in Table (\ref{PVresultTab4})).

\begin{figure}[H]
\centering
\includegraphics[scale=1]{PVresultG5}
\caption{Comparison of PVsyst, MATLAB and Actual Energy}
\label{PVG5} %% to refer use, \ref{}
\end{figure}

\begin{table}[H]
  \centering
  \caption{Comparison of PVsyst and MATLAB Application with Actual Energy}
    \begin{tabular}{|l|r|r|r|r|r|}
    \hline
    \multicolumn{6}{|c|}{\textbf{Comparison of PVsyst and MATLAB Application with Actual Energy}} \bigstrut\\
    \hline
       & \multicolumn{1}{l|}{\textbf{Actual }} & \multicolumn{1}{l|}{\textbf{E-W1}} & \multicolumn{1}{l|}{\textbf{E-W2}} & \multicolumn{1}{l|}{\textbf{Error1 \%}} & \multicolumn{1}{l|}{\textbf{Error2 \%}} \bigstrut\\
    \hline
    \textbf{Jan} & 624 & 908 & 674.3 & -45.5 & -8.1 \bigstrut\\
    \hline
    \textbf{Feb} & 678.7 & 874 & 711.5 & -28.8 & -4.8 \bigstrut\\
    \hline
    \textbf{Mar} & 917.5 & 1155 & 992.2 & -25.9 & -8.1 \bigstrut\\
    \hline
    \textbf{Apr} & 997.4 & 1146 & 799.5 & -14.9 & 19.8 \bigstrut\\
    \hline
    \textbf{May} & 1029.6 & 1183 & 858.5 & -14.9 & 16.6 \bigstrut\\
    \hline
    \textbf{Jun} & 921 & 1028 & 822.2 & -11.6 & 10.7 \bigstrut\\
    \hline
    \textbf{Jul} & 693 & 742 & 640.2 & -7.1 & 7.6 \bigstrut\\
    \hline
    \textbf{Aug} & 646.9 & 710 & 633 & -9.8 & 2.1 \bigstrut\\
    \hline
    \textbf{Sep} & 682.8 & 903 & 602.4 & -32.2 & 11.8 \bigstrut\\
    \hline
    \textbf{Oct} & 697.7 & 1003 & 640.7 & -43.7 & 8.2 \bigstrut\\
    \hline
    \textbf{Nov} & 562.7 & 845 & 628.3 & -50.2 & -11.7 \bigstrut\\
    \hline
    \textbf{Dec} & 619.9 & 808 & 684.6 & -30.3 & -10.4 \bigstrut\\
    \hline
    \end{tabular}%
  \label{PVresultTab4}%
\end{table}%

From Figure (\ref{PVG5}) and Table (\ref{PVresultTab4}) we can compare the performance of the PVsyst and MATLAB Application with actual plant data. It is observed that PVsyst overestimates the energy production with an average error percentage of 26\% (absolute values of errors), whereas the MATLAB application estimates the energy with an average error of 10\% (absolute values of errors). Hence, the developed MATLAB application performs better than PVsyst for energy estimation of the given plant.\\

Figure (\ref{PVG3}) shows a comparison between the energy estimation of the Backbone 5MW SPV, with its actual orientation of single axis east-west tracker computed the daily insolation time series file obtained from the plant (E-W3, associate error is Error3, in Table (\ref{PVresultTab5}) and using modified clear sky model (E-W4, associated error is Error4, in Table (\ref{PVresultTab5}).

\begin{table}[H]
  \centering
  \caption{ MATLAB Application Simulation Comparison of Actual, Insolation Mode and Clear Sky Model Mode}
    \begin{tabular}{|l|r|r|r|r|c|}
    \hline
    \multicolumn{6}{|c|}{\textbf{Comparison of Actual, Insolation Mode and Clear Sky Model Mode}} \bigstrut\\
    \hline
       & \multicolumn{1}{l|}{\textbf{Actual }} & \multicolumn{1}{l|}{\textbf{E-W3}} & \multicolumn{1}{l|}{\textbf{E-W4}} & \multicolumn{1}{l|}{\textbf{Error3 \%}} & \multicolumn{1}{l|}{\textbf{Error4 \%}} \bigstrut\\
    \hline
    \textbf{Jan} & 624 & 674.3 & 676.1 & -8.1 & -8.4 \bigstrut\\
    \hline
    \textbf{Feb} & 678.7 & 711.5 & 685.1 & -4.8 & -1 \bigstrut\\
    \hline
    \textbf{Mar} & 917.5 & 992.2 & 881.6 & -8.1 & 3.9 \bigstrut\\
    \hline
    \textbf{Apr} & 997.4 & 799.5 & 947.7 & 19.8 & 5 \bigstrut\\
    \hline
    \textbf{May} & 1029.6 & 858.5 & 1026.1 & 16.6 & 0.3 \bigstrut\\
    \hline
    \textbf{Jun} & 921 & 822.2 & 826.9 & 10.7 & 10.2 \bigstrut\\
    \hline
    \textbf{Jul} & 693 & 640.2 & 783.1 & 7.6 & -13 \bigstrut\\
    \hline
    \textbf{Aug} & 646.9 & 633 & 797.4 & 2.1 & -23.3 \bigstrut\\
    \hline
    \textbf{Sep} & 682.8 & 602.4 & 650.6 & 11.8 & 4.7 \bigstrut\\
    \hline
    \textbf{Oct} & 697.7 & 640.7 & 779.2 & 8.2 & -11.7 \bigstrut\\
    \hline
    \textbf{Nov} & 562.7 & 628.3 & 643.1 & -11.7 & -14.3 \bigstrut\\
    \hline
    \textbf{Dec} & 619.9 & 684.6 & 616.8 & -10.4 & 0.5 \bigstrut\\
    \hline
    \end{tabular}%
  \label{PVresultTab5}%
\end{table}%

\begin{figure}[H]
\centering
\includegraphics[scale=1]{PVresultG3}
\caption{Comparison of Insolation mode and modified Clear Sky Model mode}
\label{PVG3} %% to refer use, \ref{}
\end{figure}

From Figure (\ref{PVG3}) and Table (\ref{PVresultTab5}) we can compare two out of the three simulation modes of the MATLAB application i.e. Insolation File mode and modified clear sky model mode (SPV generation data has been correlated to the rainfall to drive clearness index). It is clearly observed that the modified clear sky mode performs better than the insolation file mode.\\

Figure (\ref{PVG4}) shows the graph of intra-day energy generation with a resolution of 15 minutes for three distinct seasonal days.

\begin{figure}[H]
\centering
\includegraphics[scale=1]{PVresultG4}
\caption{Intra-Day Energy Estimation of Backbone Plant }
\label{PVG4} %% to refer use, \ref{}
\end{figure}

Figure (\ref{PVG4}) shows the intra-day values of energy produced by Backbone 5MW SPV at a resolution of 15 minutes. The Gaussian bell shape of the curves are due to the Gaussian disintegration of the daily insolation value received from the plant. Hence, the application has the capability to compute energy for sub-hourly resolutions.\\

\newpage

Table (\ref{PVresultTab6}) gives the plant performance of the Backbone 5MW SPV (with E-W orientation) computed by the developed MATLAB application according to the IEC Standard 61724

\begin{table}[H]
  \centering
  \caption{Plant Performance Analysis}
    \begin{tabular}{|l|r|}
    \hline
    \multicolumn{2}{|c|}{\textbf{Plant Performance Analysis}} \bigstrut\\
    \hline
    \textbf{Final Yeild (Yf)} & 1.790451 \bigstrut\\
    \hline
    \textbf{Reference Yield (Yr)} & 2.0145 \bigstrut\\
    \hline
    \textbf{Array Yeild (Ya)} & 1.902521 \bigstrut\\
    \hline
    \textbf{Temperature Corrected Reference Yeild (Yt)} & 1.990852 \bigstrut\\
    \hline
    \textbf{Thermal Capture Loss (Lct)} & 0.023647 \bigstrut\\
    \hline
    \textbf{Array Capture Loss (Lc)} & 0.111979 \bigstrut\\
    \hline
    \textbf{Miscelleneous Capture Losses (Lcm)} & 0.088332 \bigstrut\\
    \hline
    \textbf{PR} & 0.888782 \bigstrut\\
    \textbf{System Losses (Ls)} & 0.11207 \bigstrut\\
    \hline
    \hline
    \textbf{CUF} & 0.204389 \bigstrut\\
    \hline
    \textbf{Temperature Corrected PR} & 0.87847 \bigstrut\\
    \hline
    \end{tabular}%
  \label{PVresultTab6}%
\end{table}%


\documentclass[11pt, oneside]{article}

\usepackage{../shared/preamble}
%\addbibresource{../shared/references.bib}

%\usepackage{sorting}

\title{Sorting}
\author{Arthur Ryman, {\tt arthur.ryman@gmail.com}}
\date{\today}

\begin{document}

\maketitle

\begin{abstract}
This article formalizes the notion of sorting to illustrate the relation between
a specification and an implementation, and how to prove that an implementation satisfies
a specification.
\end{abstract}

\section{Sorting}

Suppose we are given a finite list of numbers and are asked to sort it.
By sorting we mean that the result should contain exactly the same set of numbers, with the same multiplicities,
arranged in ascending order.

\subsection{Lists, $LIST$}

Let $LIST$ denote a finite sequence of integers.

\begin{zed}
LIST == \seq \num
\end{zed}

\subsection{Ascending Order $ascending$}

A list is in ascending order when any element that appears earlier in the list is less than
or equal to any element that appears later in the list.
Let $ascending$ denote the set of all lists that are in ascending order.

\begin{zed}
ascending == \{~ s: LIST | (\forall i, j: \dom s | i < j @ s(i) \leq s(j)) ~\}
\end{zed}

Although mathematically correct, the definition of $ascending$ is not in a
form that can be implemented efficiently on a computer.
The definition implies that if the list contains $n$ elements then
there are $n(n+1)/2$ pairs $(i, j)$ such that $i \leq j$ for which $s$ has to be checked.
The complexity of the direct, naive implementation of this check is therefore $O(n^2)$.
Clearly, we can reduce this complexity to $O(n)$.

\subsection{Refinement of Ascending Order $ascending1$}

A more efficient definition of ascending order is given by $ascending1$.

\begin{zed}
ascending1 == \{~ s: LIST | (\forall i: \dom s | i < \# s @ s(i) \leq s(i + 1)) ~\}
\end{zed}

Clearly, a direct implementation of this definition has complexity $O(n)$.

\subsection{Theorem $ascending = ascending1$}

Although the equivalence of $ascending$ and $ascending1$ is obvious to a human,
other similar optimizations may not be.
It is therefore instructive to prove the equivalence.

The plan of the proof is to prove $ascending \subseteq ascending1$ and $ascending1 \subseteq ascending$
which corresponds to unfolding the definition of equality for sets.

First prove $ascending \subseteq ascending1$.

\begin{argue}
s: ascending 							& assumption \\
\forall i, j : \dom s | i < j @ s(i) \leq s(j)		& def of $ascending$ \\
i: \dom s | i < \# s						& intro $i$ \\
j == i + 1								& intro $j$ \\
j \in \dom s							& def of $\dom$ \\
i < j									& def of $<$ \\
s(i) \leq s(j)							& $\forall$-elim \\
s(i) \leq s(i + 1)							& def of $j$ \\
\forall i: \dom s | i < \# s @ s(i) \leq s(i + 1)		& $\forall$-intro \\
s \in ascending1						& def of $ascending1$ \\
s: ascending \implies s \in ascending1		& $\implies$-intro \\
ascending \subseteq ascending1			& $s$-elim QED
\end{argue}

Next prove  $ascending1 \subseteq ascending$.
This proof requires induction on the length of the list.

\begin{argue}
s: ascending1							& assumption \\
\forall i: \dom s | i < \# s @ s(i) \leq s(i + 1)		& def of $ascending1$ \\
\# s = 0								& assumption \\
s \in ascending							& def of $ascending$ \\
\# s = 0 \implies s \in ascending				& $\implies$-intro \\
\# s = 1								& assumption \\
s in ascending							& def of $ascending$ \\
\# s = 1 \implies s \in ascending				& $\implies$-intro \\
n: \nat_1 | n > 1							& intro $n$ \\
\#s = n \implies s \in ascending				& inductive assumption

\end{argue}

%\printbibliography

\end{document}


\documentclass[12pt]{amsart}
\title{Optimal Ricci matrix computations.}
\begin{document}
\maketitle

\section{Notation}
\label{sec:Notation}

Throughout the paper we use $\times$ to denote pointwise multiplication. Consequently, we also use $f^{\times2}$ to denote pointwise powers. We also assume that operators are represented by matrices and functions are column vectors.

We will use $M_i$ to denote a row of a matrix, and $M_{:i}$ to denote its column.

Suppose $r$ is a row vector, $c$ is a column vector and $M$ is a matrix. We can perform pointwise operations between objects not having the same size. For example $c+M$ should be understood as the column is first promoted to a square matrix with the same column repeated. The same way we can promote a row to a matrix, or a number to any shape. Note that such extension of $+$ and $-$ preserves distributive property of matrix multiplication. We can also apply the promotion to pointwise multiplication $\times$.

We will exploit the following property:
\begin{align}\label{property}
    r(M\times c)&=(r\times c^T)M, \qquad r\text{ can be a matrix}\\
    (r\times M)c &= M(c\times r^T), \qquad c\text{ can be a matrix}.
\end{align}
Note that $r\times c^T$ gives a row vector, while $r\times c$ would give a matrix (both $r$ and $c$ would be promoted first).

\section{Initial computations}
We start with
\begin{align*}
    \Gamma(L, f, h) = \frac{1}{2} \left(L(f\times h) - f\times Lh-h\times Lf\right),
\end{align*}
which is a column vector.
We are interested in
\begin{align}
    \Gamma_2(L, f, f) &= \frac{1}{2}L\Gamma(L, f, f) - \Gamma(L, Lf, f)=\cdots=\nonumber
    \\&=\frac{1}{4} L^2f^{\times2}-L(f\times Lf)+\frac{1}{2}f\times L^2f+\frac{1}{2}(Lf)^{\times2}.\label{Gamma2}
\end{align}

We have a squared distance matrix $D$ (size $n\times x$), Laplace matrix $L$, and a set of functions $f$, one for each entry of $D$ (pair of points).

We have $f^{(i,j)}=D_{:i}-D_{:,j}$. We take a difference of two columns. Hence we have $n^2$ functions with $n$ entries each. This makes it hard to store all of them.

Each function $f^{(i,j)}$ is used to find one entry $R_{i,j}$ in the Ricci matrix:
\begin{align*}
    R_{i,j} = \Gamma_2(L, f^{(i,j)}, f^{(i,j)})_{i}.
\end{align*}
We take $i$-th row from a column vector $\Gamma_2(\ldots)$.
\section{One row in Ricci matrix}
\label{sec:RowInRicci}
We will try to compute the whole row of the Ricci matrix at once.

We define the functions matrix $F(i)=D_{:i}-D$.  Then $F(i)$ encodes all functions $f^{(i,\cdot)}$ as its columns.

We can use $F(i)$ in place of $f$ in the formula \eqref{Gamma2} for $\Gamma_2$. We get
\begin{align*}
    R_{i} = \left(\frac{1}{4} L^2F(i)^{\times2}-L(F(i)\times LF(i))+\frac{1}{2}F(i)\times L^2F(i)+\frac{1}{2}(LF(i))^{\times2}\right)_{i}.
\end{align*}
Since we are computing just one row, we can do that in each term. To further simplify the notation we drop $(i)$ from $F$.

Note that for matrices $(A \times B)_i = A_i\times B_i$ and $(AB)_i = A_i B$. Hence
\begin{align}\label{RicciRow}
    R_{i} = \frac{1}{4} (L^2)_i F^{\times2}-L_i(F\times LF)+\frac{1}{2}F_i\times (L^2)_iF+\frac{1}{2}(LF)^{\times2}_i.
\end{align}

First note that
\begin{align*}
LF = L(D_{:i}-D)=(LD)_{:i}-LD.
\end{align*}

We can precompute a few matrix products independent of $i$:
\begin{align*}
    A&=L^2,\\
    B&=LD,\\
    C&=AD.
\end{align*}
\textbf{Note that $B$ and $C$ are not symmetric, since products of symmetric matrices generally are not symmetric.}

Now we will look at each term in \eqref{RicciRow}. The last term is the simplest:
\begin{align*}
    (LF)^{\times2}_i&=(B_{:i}-B)^{\times2}_i=(B_{ii}-B_i)^{\times2}
    \\&=
    (B^{\times2})_i+(B^{\times2})_{ii}-2B_i\times B_{ii}.
\end{align*}
Hence it can be computed as row$\times$row operation.

    The first term can also be simplified:
    \begin{align*}
	(L^2)_i F^{\times2} &= A_i(D^{\times2}+D^{\times2}_{:i}-2D\times D_{:i})
			\\&=
			A_iD^{\times2}+A_i(D^{\times2})_{:i} -2 A_i (D\times D_{:i})
			\\&=
			(A D^{\times2})_i+(A D^{\times2})_{ii} -2 (A\times D)_iD,
    \end{align*}
    In the last step we used symmetry of $D$ and \eqref{property}. For nonsymmetric matrix column $D_{:,i}$ would simply need to be transposed instead of changing to $D_i$.
    Next we have
    \begin{align*}
	F_i\times (L^2)_iF &= (D_{ii}-D_i)\times [A_i (D_{:i}-D)]
	\\&=
	(D_{ii}-D_i)\times ((AD)_{ii}-(AD)_i)
	\\&=
	(D_{ii}-D_i)\times (C_{ii}-C_i)
	\\&=
	(D\times C)_i + (D\times C)_{ii} - D_i\times C_{ii},
    \end{align*}
    since $D_{ii}=0$.
    Finally
    \begin{align*}
	L_i(F\times LF)&=L_i((D_{:i}-D) \times (B_{:i}-B)) =
	\\&=
	L_i\left(D\times B+(D\times B)_{:i}-D\times B_{:i}-B\times D_{:i}\right)=
	\\&=
	G_i+G_{ii}-(L_i\times (B^T)_i)D-(L_i\times D_i)B
	\\&=
	G_i+G_{ii}-(L\times B^T)_{i}D-(L\times D)_iB,
    \end{align*}
    where $G=L(D\times B)$.

    To make our notation even more compact we let $diag(M)$ be a column vector composed of the diagonal elements of $M$. Then
    \begin{align*}
	\overline M = M + diag(M)
    \end{align*}
satisfies
\begin{align*}
\overline M_i = M_i+M_{ii}.
\end{align*}
With this notation we can represent a row of Ricci matrix (given by \eqref{RicciRow}) as
\begin{align*}
    R_i &= \frac{1}{4}\left( \overline{AD^{\times2}} \right)_i -  \frac{1}{2}(A\times D)_i D
    \\&
    -\left( \overline{L(D\times B)} \right)_i +(L\times B^T)_iD +(L\times D)_i B
    \\&
    + \frac{1}{2}\left(  \overline{D\times C}\right)_i - \frac{1}{2}D_i\times C_{ii}
    \\&
    + \frac{1}{2}\left(\overline{ B^{\times2}}\right)_i - B_i\times B_{ii}.
\end{align*}

\section{Full Ricci matrix}
Note that the formula for the row of the Ricci matrix contains only take a row operations for various matrices. We can also combine all overline operations. Hence

\begin{align}
    R &= \overline{\frac{1}{4}AD^{\times2} -L(D\times B) +\frac{1}{2}D\times C + \frac{1}{2}B^{\times2}}\nonumber
    \\&
    +\left((L\times B^T)-  \frac{1}{2}(A\times D)\right)D +(L\times D) B\label{2ndline}
     \\&
     - \frac{1}{2}D\times diag(C) - B\times diag(B),\nonumber
\end{align}
with $A=L^2$, $B=LD$ and $C=AD$, a $diag$ denoting a column vector composed of the diagonal of a matrix.

\section{Order of computations}
\label{sec:OrderOfComputations}

We want to minimize the number of multiplications while keeping memory requirement small. Clearly, 7 matrix multiplications and 3 extra matrices are enough. Although it should be possible to bring this down to 2 extra matrices. Furthermore, Laplacian could be just computed as needed, since it is really fast.

\subsection{Only 2 temporary matrices}
\label{sec:Only2Temporary}

We will avoid computing $A$ by taking $L$ out of the first two terms.
\begin{itemize}
    \item Compute $B\leftarrow D^{\times2}/4$ first, then $A\leftarrow LB$ (1st M-M multiply).
    \item Compute $B\leftarrow LD$ (2nd M-M multiply), then $A\leftarrow A-D\times B$.
    \item Now $R\leftarrow LA$ (3rd M-M multiply).
    \item Compute $A\leftarrow LB$ (4th M-M multiply, called C in the section above), then finish adding $diag$ terms and overline.
\end{itemize}
Now we need the second line terms from \eqref{2ndline}. We already have $B$ computed.
\begin{itemize}
    \item Find $A\leftarrow LL$ (5th M-M multiply), then $A\leftarrow \Big((L\times B^T)-  \frac{1}{2}(A\times D)\Big)$.
            \item Now $R \leftarrow R+AD$ (6th M-M multiply, full dgemm).
                \item Finally, $A\leftarrow D\times L$, then $R\leftarrow R+AB$ (7th M-M multiply, full dgemm).
\end{itemize}

\end{document}


\mainsection{Math Library}
Eventually we expect every floating point type to
have a full set of math functions.
Remember the \verb|ClearIEEE| type is must faster than
the \verb|ClearFixed<K,F>| type, but the \verb|SecretFixed<K,F>|
is generally much faster than the \verb|SecretIEEE| type.
To include the functions in the math library use
\begin{lstlisting}
    use scale_std::math::*;
\end{lstlisting}
This captures traits \verb|Floor|, \verb|FAbs|
and \verb|Sqrt| (implementing member functions
\verb|floor()|, \verb|fabs()| and \verb|sqrt()| respectively),
plus also a supertrait called
\verb|Float| which captures much of the properties
common to all floating point types.

Currently the following are available (we comment the
function out if it is not yet fully tested/implemented):

\subsection{Generic Routines}

\func{poly_eval(poly, x)}
This generic function with signature
\begin{lstlisting}
  pub fn poly_eval<S, C, const N: u64>(poly: Array<C, N>, x: S) -> S
  where
    S: Float,
    S: Add<C, Output = S>,
    S: Mul<S, Output = S>,
    C: Float,
\end{lstlisting}
allows one to evaluate a polynomial in type \verb|C|
(a clear floating point type) at a value of type
\verb|S| (which is either the {\em same} clear floating
point type, or its {\em associated} secret variant).

\func{Pade(P, Q, x)}
This generic function with signature
\begin{lstlisting}
  pub fn Pade<S, C, const N: u64>
   (poly_p: Array<C, N>, poly_q: Array<C, N>, x: S) -> S
  where
    S: Float,
    S: Add<S, Output = S>,
    S: Mul<S, Output = S>,
    S: Div<S, Output = S>,
    S: Mul<C, Output = S>,
    S: From<C>,
    C: Float,
\end{lstlisting}
allows one to evaluate the Pade approximation given
by polynomials $P$ and $Q$ in type \verb|C|
(a clear floating point type) at a value of type
\verb|S| (which is either the {\em same} clear floating
point type, or its {\em associated} secret variant).
The polynomials $P$ and $Q$ must have the same degree.

All floating point types \verb|T| have predefined constants
\begin{lstlisting}
   T::two_pi();
   T::pi();
   T::half_pi();
   T::ln2();
   T::e();
\end{lstlisting}

\subsection{ClearIEEE}
The following are all implemented via a local function call
to the SCALE C++ runtime; thus are relatively fast.
\begin{lstlisting}
    let c = ClearIEEE::from(0.5431);
    let t = c.acos();
    let t = c.asin();
    let t = c.atan();
    let t = c.cos();
    let t = c.cosh();
    let t = c.sin();
    let t = c.sinh();
    let t = c.tan();
    let t = c.tanh();
    let t = c.exp();
    let t = c.exp2()
    let t = c.log();
    let t = c.log2();
    let t = c.log10();
    let t = c.ceil();
    let t = c.fabs();
    let t = c.floor();
    let t = c.sqrt();
\end{lstlisting}
Note, \verb|exp2()| computes the function $2^x$.

\subsection{SecretIEEE}
The following functions are implemented
\begin{lstlisting}
    let s = SecretIEEE::from(0.5431);
    let t = s.acos();
    let t = s.asin();
    let t = s.atan();
    let t = s.cos();
    let t = s.cosh();
    let t = s.sin();
    let t = s.sinh();
    let t = s.tan();
    let t = s.tanh();
    let t = s.exp();
    let t = s.exp2()
    let t = s.log();
    let t = s.log2();
    let t = s.log10();
    let t = s.ceil();
    let t = s.fabs();
    let t = s.floor();
    let t = s.sqrt();
\end{lstlisting}
Note logarithms of negative values result in undefined
behaviour.

\subsection{ClearFixed}
The following functions are implemented
\begin{lstlisting}
    let c: ClearFixed<40,20>= ClearFixed::from(0.5431);
    let t = c.acos();
    let t = c.asin();
    let t = c.atan();
    let t = c.cos();
    let t = c.cosh();
    let t = c.sin();
    let t = c.sinh();
    let t = c.tan();
    let t = c.tanh();
    let t = c.exp();
    let t = c.exp2()
    let t = c.log();
    let t = c.log2();
    let t = c.log10();
    let t = c.ceil();
    let t = c.fabs();
    let t = c.floor();
    let t = c.sqrt();
\end{lstlisting}

\subsection{SecretFixed}
The following functions are implemented
\begin{lstlisting}
    let s: SecretFixed<40,20>= SecretFixed::from(0.5431);
    let s = c.acos();
    let s = c.asin();
    let s = c.atan();
    let s = c.cos();
    let s = c.cosh();
    let s = c.sin();
    let s = c.sinh();
    let s = c.tan();
    let s = c.tanh();
    let s = c.exp();
    let s = c.exp2()
    let s = c.log();
    let s = c.log2();
    let s = c.log10();
    let s = c.ceil();
    let s = c.fabs();
    let s = c.floor();
    let s = c.sqrt();
\end{lstlisting}