• No results found

So far the problem has been solved by explicit steps in volume and using the fact that the volume is a linear function ofα. The rest of the variables were found by solving a set of algebraic equations using Newton iteration. Even though the phase stability is a problem with special properties, it is tempting to try a more general solution approach. The tracing procedure of the tangent plane distance can easily be rewritten as a system of ordinary differential equations (ODE). This means that it is possible to integrate along the constant µ-manifold. Since the model implementation used here provides more information than a general function does, it was decided to implement the ODE solvers by hand. It is for instance possible to calculate the exact global error in each step. This is defined as the distance

α

˜x

0 0.25 0.5 0.75 1

-0.2 0.1 0.4 0.7 1

α

˜x

0.97 0.9754 0.9808 0.9861 0.9915 0

1 2 3 4 ×10−4

Figure 3.12: Projection of the tangent plane distance ˜x versusα for the system CH4–nC6–H2S. The left figure shows the whole curve, while the right figure only shows the area where the minima occur.

p[105Pa]

V [10-5m3]

0.8 1.2 1.6 2 2.4

-26 9.5 45 80.5 116

Figure 3.13: Projection of p–V at constant S andµ for the system CH4–nC6–H2S.

T[K]

S[J/K mol]

23 34.25 45.5 56.75 68

199 200.25 201.5 202.75 204

Figure 3.14: Projection of T –S at constant V andµ for the system CH4–nC6–H2S

from the constantµ-manifold and µ is calculated in each step anyway. Methods with built-in error estimates are not be able to take advantage of this information, and in order to estimate the error the solver must do more function evaluations in addition to solving the problem. Since only the minima of the curve are of interest, it should be possible to solve the problem with less accuracy when the tangent plane distance is far from zero. The gradient and the Hessian are also calculated at each step. This information can, as for the method in the previous sections, be used to predict the minima.

The following two ODE solvers were implemented and tested out: Fourth order Runge-Kutta (RK4) and Runge-Kutta-Fehlberg (RKF). As will be explained, a set of algebraic equations must be solved for each integrator step, and since the integrators are not meant to be general purpose tools the algebraic equation are solved within the integrator. The two-component system described in Section 3.1 proved to be a lot more complicated than the three component system described in the previous section. It was therefore decided to use the three-component system as a test case for developing the method. The ODE solvers also need initial values.

It was therefore chosen to start at the same converged four phase equilibrium as described in the previous section. The starting point is used to establishα and the chemical potentials, whereα is the integration variable of the problem. The constant chemical potential manifold constrains the problem has now the residual:

∆x(α, a) =

−S(a) + S(α)

−V (a) +V (α) µ(a) − µ

Here, a means the Helmholtz variables T ,V and n. The solution should of course stay as close to the∆µ = 0 manifold as possible. The volume is explicit in a, but there will be deviations in all the other variables, that is T , N1, N2 and N3. This is compensated for by taking small steps to keep close to the manifold, and by defining a set of equations such that the solver makes a move close to the tangent of the manifold. The integration is based on a Newton step using the current state a as a starting point. By integrating galong theα–axis we get:

G(a)˙g = −c(a) − λ ∆x(α, a) (3.28) Here c= ∂ ∆x/∂ α = C(|T|-1, |p|-1, 0, 0, . . . )T, where c maps ˙gonto the tangent plane of the manifold, whileλ ∆x corrects for nonlinearities in the form of an accu-mulated drift. Note that C= |T|S + |p|V , which is identical to denominator of α.

The differential and algebraic equations are not solved simultaneously

Gibbs energy canonical variables,(T, p, n)

Time derivative of g

Since the thermodynamic model used is explicit in T , V , n the differential equa-tions need to be written as a vector function of the Helmholtz variables. Typically, we let ˙a= J(a)˙g, where J(a) is the Jacobian as:

Combining Equation 3.28 and 3.29, yields:

˙

a= J(a)X(a)[c(a) + λ ∆x(α, a)] (3.30) Equation 3.30 shows the ODE that is integrated on the constantµ-manifold. The next two sections deal with the actual integration routines.

3.4.1 Fourth Order Runge-Kutta (RK4)

The differential equations were first tested by Euler’s method before the actual implementation of the integrators started. Indeed, Euler’s method is able to give a similar curve to the approach shown in Section 3.3. The goal is (as before) to follow the manifold and to locate all the minima of the function in Equation 3.8. In order to do this, several different ways of controlling the step size were implemented. The global criteria of constant chemical potentials can be used to make sure that the solution is close to the manifold at each integration step. As with the explicit step method a Taylor expansion is used at each point to predict the minimum. In Section 3.3 the minimum was solved as soon as it was predicted, but as explained later, a different approach of reducing the step size was tried for RK4. First, RK4 is implemented with no control of the step size. As expected this is rather slow and inaccurate compared with the explicit step method. As a first attempt to control the step size, the same Taylor expansion as explained in Section 3.2 is used to predict a minimum. Instead of solving for the exact minimum, the step size is reduced to a fixed value. When no minima is predicted, the step size is increased. This gives stability problems because when the step size grows, the deviation from the manifold becomes so large that the variables get outside the bounds of the thermodynamic model. The RK4 method has therefore an internal step size control which reduces the step size until the deviation from theµ-manifold is within a prescribed tolerance.

The accuracy is not so important far away from the minima, and it was therefore attempted to implement an adaptive tolerance control. This was not successful because the change of the tolerance during integration sometimes caused stability

problems in the integrator. It appeared that the integrator was very sensitive to changes in the tolerance while integrating. It is possible to change the tolerance at the start of the integration, but not during the integration. After several attempts to improve the method, it was concluded that the reduction of the tolerance to keep the method close to the minimum had little or no effect on the solution. Since the effect was minimal, and the method became unstable, the adaptive change of the tolerance was removed. No further investigations regarding the tolerance were made.

The way the RK4 method is implemented here makes it a hybrid solution between the explicit step method in Section 3.2 and an ODE solver. As can be seen from Figure 3.15 the results are comparable, but the explicit step method requires fewer function evaluations to trace out the curve. It should, however, be noted that con-siderably more effort were put into improving the explicit step method than tuning the RK4 solver. It should also be noted that for the RK4 method there is no check to see that the S–V relationship is constant, as this is explicitly assumed by the integrator. The predicted value ofα was larger than one at the end of integration, indicating that this criterion is not fulfilled. No further investigations were done, but from Figure 3.15 no significant deviations can be seen.

α

˜x

0.97 0.9754 0.9808 0.9861 0.9915 0

1 2 3 4×10−4

(a) Integration (RK4).

α

˜x

0.97 0.9754 0.9808 0.9861 0.9915 0

1 2 3 4 ×10−4

(b) Explicit step method.

Figure 3.15: Comparison of integration and iteration for the projection of the tan-gent plane distance ˜xversusα for the system CH4–nC6–H2S. The crosses show each point calculated. The squares show the converged minima. The dashed lines show the trend based on the points.

This is not a DAE solver, as the differential and algebraic equations are solved separately

3.4.2 Runge-Kutta-Fehlberg (RKF)

A step size control was implemented for the RK4 method based upon the knowl-edge of the problem and the fact that only parts of the curve are of interest. The integration is comparable to the explicit step method, but the RK4 solver is no longer a pure ODE solver. Without any step size control the RK4 method is, as expected, either very slow or inaccurate. The natural alternative would of course be to implement an integration method with built-in control of the step size. The RKF method is a simple ODE solver with built-in step control. The RKF method will naturally not be able to take advantage of the information used to construct the step size control for the RK4 method and for the explicit step method. This means that the implementation will not be able to increase the resolution close to the minima. The RKF method needs more function evaluations per step to find an error estimate, which is used to control the step size. However, this is based on local information only, and no global error is predicted. The execution time of the RKF method depends therefore on the tolerance of the local error. When the tolerance is set to 10-2 the runtime is about the same as for the RK4 method, and, as can be seen from Figure 3.16, the accuracy is good.

α

˜x

0.97 0.9754 0.9808 0.9861 0.9915 0

1 2 3 4×10−4

(a) Integration (RKF).

α

˜x

0.97 0.9754 0.9808 0.9861 0.9915 0

1 2 3 4 ×10−4

(b) Explicit step method

Figure 3.16: Comparison of integration and iteration for the projection of the tan-gent plane distance ˜x versusα for the system CH4–nC6–H2S. The crosses show each point calculated. The squares show the converged minima. The dashed lines show the trend based on the points.

It will still be possible to detect a minimum outside the method, and externally change the step size, or reduce the step size. This was not done in this case