## Using autograd to plot implicit functions

| categories: | tags: | View Comments

Consider the solution to these equations (adapted from https://www.mathworks.com/help/optim/ug/fsolve.html):

$$e^{-e^{-(x_1 + x_2)}} = x_2 (1 + x_1^2)$$

and

$$x_1 \cos(x_2) + x_2 \sin(x_1) = 1/2$$

It is not clear how many solutions there are to this set of equations, or what you should guess for the initial guess. Usually, the best way to see where a solution might be is to plot the equations and see where they intersect. These equations are implicit though, and it is not easy to plot them because we cannot solve for $$x_2$$ in terms of $$x_1$$ in either case. Here we explore a strategy to get plots so we can see where solutions could be.

The idea is that we find one solution to each equation independently. Then, we derive a differential equation for each equation so we can integrate it to find the curve that is defined by the implicit function. First, we find a solution for each equation. We guess a value for $$x_2$$ and then find the value of $$x_1$$ that solves each equation independently.

import autograd.numpy as np
from scipy.optimize import fsolve

def f1(x1, x2):
return np.exp(-np.exp(-(x1 + x2))) - x2 * (1 + x1**2)

def f2(x1, x2):
return x1 * np.cos(x2) + x2 * np.sin(x1) - 0.5

x2_1 = 0.6
x1_1, = fsolve(f1, 0, args=(x2_1,))
print('f1: ', x1_1, x2_1)

x2_2 = 1.0
x1_2, = fsolve(f2, 0 ,args=(x2_2,))
print('f2: ', x1_2, x2_2)


f1: 0.08638978040861575 0.6 f2: 0.32842406163614396 1.0

Next, we need a differential equation that is $$dx_2/dx_1$$. If we had that, we could just integrate it from one of the starting points above, and get the curve we want. The functions are implicit, so we have to use the implicit derivative, which for the first equation is $$dx_2/dx_1 = -df1/dx_1 / df1/dx_2$$. We will get these gradients from autograd. Then, we just integrate the solution. Here we do this for the first equation.

from scipy.integrate import solve_ivp

def dx2dx1_1(x1, x2):
return -df1dx1(x1, x2) / df1dx2(x1, x2)

x1_span = (x1_1, 1)
x2_0 = (x2_1, )
sol1 = solve_ivp(dx2dx1_1, x1_span, x2_0, max_step=0.1)


And then, we do it for the second equation.

df2dx1 = grad(f2, 0)

def dx2dx1_2(x1, x2):
return -df2dx1(x1, x2) / df2dx2(x1, x2)

x1_span = (x1_2, 1)
x2_0 = (x2_2, )
sol2 = solve_ivp(dx2dx1_2, x1_span, x2_0, max_step=0.1)


Finally, we plot the two solutions.

%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(sol1.t, sol1.y.T)
plt.plot(sol2.t, sol2.y.T)
plt.xlabel('$x_1$')
plt.ylabel('$x_2$')
plt.legend(['f1', 'f2'])

<Figure size 432x288 with 1 Axes>


You can see now that in this range, there is only one intersection, i.e. one solution, and it is near $$x_1=0.4, x_2=0.6$$. We can finally use that as an initial guess to find the only solution in this region, with confidence we are not missing any solutions.

def objective(X):
x1, x2 = X
return [f1(x1, x2), f2(x1, x2)]

fsolve(objective, [0.4, 0.6])

array([0.35324662, 0.60608174])


That is the same solution as reported at the Matlab site. Another use of autograd for the win here.

org-mode source

Org-mode version = 9.2.3

## Solving differential algebraic equations with help from autograd

| categories: | tags: | View Comments

This problem is adapted from one in "Problem Solving in Chemical Engineering with Numerical Methods, Michael B. Cutlip, Mordechai Shacham".

In the binary, batch distillation of benzene (1) and toluene (2), the moles of liquid $$L$$ remaining as a function of the mole fraction of toluene ($$x_2$$) is expressed by:

$$\frac{dL}{dx_2} = \frac{L}{x_2 (k_2 - 1)}$$

where $$k_2$$ is the vapor liquid equilibrium ratio for toluene. This can be computed as:

$$k_i = P_i / P$$ where $$P_i = 10^{A_i + \frac{B_i}{T +C_i}}$$ and that pressure is in mmHg, and the temperature is in degrees Celsius.

One difficulty in solving this problem is that the temperature is not constant; it changes with the composition. We know that the temperature changes to satisfy this constraint $$k_1(T) x_1 + k_2(T) x_2 = 1$$.

Sometimes, one can solve for T directly, and substitute it into the first ODE, but this is not a possibility here. One way you might solve this is to use the constraint to find $$T$$ inside an ODE function, but that is tricky; nonlinear algebra solvers need a guess and don't always converge, or may converge to non-physical solutions. They also require iterative solutions, so they will be slower than an approach where we just have to integrate the solution. A better way is to derive a second ODE $$dT/dx_2$$ from the constraint. The constraint is implicit in $$T$$, so We compute it as $$dT/dx_2 = -df/dx_2 / df/dT$$ where $$f(x_2, T) = k_1(T) x_1 + k_2(T) x_2 - 1 = 0$$. This equation is used to compute the bubble point temperature. Note, it is possible to derive these analytically, but who wants to? We can use autograd to get those derivatives for us instead.

The following information is given:

The total pressure is fixed at 1.2 atm, and the distillation starts at $$x_2=0.4$$. There are initially 100 moles in the distillation.

species A B C
benzene 6.90565 -1211.033 220.79
toluene 6.95464 -1344.8 219.482

We have to start by finding the initial temperature from the constraint.

import autograd.numpy as np
from scipy.integrate import solve_ivp
from scipy.optimize import fsolve
%matplotlib inline
import matplotlib.pyplot as plt

P = 760 * 1.2 # mmHg
A1, B1, C1 = 6.90565, -1211.033,  220.79
A2, B2, C2 = 6.95464, -1344.8, 219.482

def k1(T):
return 10**(A1 + B1 / (C1 + T)) / P

def k2(T):
return 10**(A2 + B2 / (C2 + T)) / P

def f(x2, T):
x1 = 1 - x2
return k1(T) * x1 + k2(T) * x2 - 1

T0, = fsolve(lambda T: f(0.4, T), 96)
print(f'The initial temperature is {T0:1.2f} degC.')


The initial temperature is 95.59 degC.

Next, we compute the derivative we need. This derivative is derived from the constraint, which should ensure that the temperature changes as required to maintain the constraint.

dfdx2 = grad(f, 0)

def dTdx2(x2, T):
return -dfdx2(x2, T) / dfdT(x2, T)

def ode(x2, X):
L, T = X
dLdx2 = L / (x2 * (k2(T) - 1))
return [dLdx2, dTdx2(x2, T)]


Next we solve and plot the ODE.

x2span = (0.4, 0.8)
X0 = (100, T0)
sol = solve_ivp(ode, x2span, X0, max_step=0.01)

plt.plot(sol.t, sol.y.T)
plt.legend(['L', 'T']);
plt.xlabel('$x_2$')
plt.ylabel('L, T')
x2 = sol.t
L, T = sol.y
print(f'At x2={x2[-1]:1.2f} there are {L[-1]:1.2f} moles of liquid left at {T[-1]:1.2f} degC')


At x2=0.80 there are 14.04 moles of liquid left at 108.57 degC

<Figure size 432x288 with 1 Axes>


You can see that the liquid level drops, and the temperature rises.

Let's double check that the constraint is actually met. We do that qualitatively here by plotting it, and quantitatively by showing all values are close to 0.

constraint = k1(T) * (1 - x2) + k2(T) * x2 - 1
plt.plot(x2, constraint)
plt.ylim([-1, 1])
plt.xlabel('$x_2$')
plt.ylabel('constraint value')
print(np.allclose(constraint, np.zeros_like(constraint)))
constraint


True

array([ 2.22044605e-16,  4.44089210e-16,  2.22044605e-16,  0.00000000e+00,
1.11022302e-15,  0.00000000e+00,  6.66133815e-16,  0.00000000e+00,
-2.22044605e-16,  1.33226763e-15,  8.88178420e-16, -4.44089210e-16,
4.44089210e-16,  1.11022302e-15, -2.22044605e-16,  0.00000000e+00,
-2.22044605e-16, -1.11022302e-15,  4.44089210e-16,  0.00000000e+00,
-4.44089210e-16,  4.44089210e-16, -6.66133815e-16, -4.44089210e-16,
4.44089210e-16, -1.11022302e-16, -8.88178420e-16, -8.88178420e-16,
-9.99200722e-16, -3.33066907e-16, -7.77156117e-16, -2.22044605e-16,
-9.99200722e-16, -1.11022302e-15, -3.33066907e-16, -1.99840144e-15,
-1.33226763e-15, -2.44249065e-15, -1.55431223e-15, -6.66133815e-16,
-2.22044605e-16])

<Figure size 432x288 with 1 Axes>


So indeed, the constraint is met! Once again, autograd comes to the rescue in making a computable derivative from an algebraic constraint so that we can solve a DAE as a set of ODEs using our regular machinery. Nice work autograd!

org-mode source

Org-mode version = 9.2.3

## Sensitivity analysis with odeint and autograd

| categories: | tags: | View Comments

In this previous post I showed a way to do sensitivity analysis of the solution of a differential equation to parameters in the equation using autograd. The basic approach was to write a differentiable integrator, and then use it in a function so that autograd could take the derivative.

Since that time, autograd has added derivative support for scipy.integrate.odeint. In this post we examine that. As usual with autograd, we have to import the autograd version of numpy, and the autograd version of odeint. We will find the derivative of the solution to an ODE (which is an array) so we need to also import the jacobian function. Finally, there is a subtle, and non-obvious requirement that we need to import the autograd tuple. That ensures that the variables are differentiable through the tuple we will use for the arguments.

The differential equation we solve returns the concentration of a species as a function of time, and the solution depends on two parameters, i.e. $$C = f(t; k_1, k_{-1})$$, and we are interested in the time-dependent sensitivity of $$C$$ with respect to those parameters. The approach we use is to define a function that has those parameters as arguments. The function will solve the ODE and return the time-dependent solution. First we make that solution, mostly to see that the autograd version of odeint works.

import autograd.numpy as np

import matplotlib.pyplot as plt

Ca0 = 1.0
k1 = k_1 = 3.0

tspan = np.linspace(0, 0.5)

def C(K):
k1, k_1 = K
def dCdt(Ca, t, k1, k_1):
return -k1 * Ca + k_1 * (Ca0 - Ca)
sol = odeint(dCdt, Ca0, tspan, tuple((k1, k_1)))
return sol

plt.plot(tspan, C([k1, k_1]))
plt.xlim([tspan.min(), tspan.max()])
plt.xlabel('t')
plt.ylabel('C');

<Figure size 432x288 with 1 Axes>


Now, the solution is an array, and we want the derivative of C with respect to the parameters at each time point. That means we want the jacobian derivative of the output with respect to the input. Here is the autograd approach to doing that. The jacobian function returns a function that we can evaluate to get the derivatives.

import time
t0 = time.time()
dCdk = jacobian(C, 0)

k_sensitivity = dCdk(np.array([k1, k_1]))

k1_sensitivity = k_sensitivity[:, 0, 0]
k_1_sensitivity = k_sensitivity[:, 0, 1]

plt.plot(tspan, np.abs(k1_sensitivity), label='dC/dk1')
plt.plot(tspan, np.abs(k_1_sensitivity), label='dC/dk_1')
plt.legend(loc='best')
plt.xlabel('t')
plt.ylabel('sensitivity')
print(f'Elapsed time = {time.time() - t0:1.1f} seconds')


Elapsed time = 38.2 seconds

<Figure size 432x288 with 1 Axes>


That looks similar to the results from before. It is pretty slow I think, that took more than half a minute to work out. That is still faster and probably more correct than if I had to do it by hand. In contrast, however, the finite difference code below is comparatively very fast! I don't know what is slow in the autograd implementation. I guess it is an implementation detail.

import numdifftools as nd
t0 = time.time()

fdk1, fdk_1 = nd.Jacobian(C)([k1, k_1]).T
print(f'Elapsed time = {time.time() - t0:1.1f} seconds')

plt.plot(tspan, np.abs(fdk1), label='fd dC/dk1')
plt.plot(tspan, np.abs(fdk_1), label='fd dC/dk_1')
plt.plot(tspan, np.abs(k1_sensitivity), 'y--', label='dC/dk1')
plt.plot(tspan, np.abs(k_1_sensitivity),'m--', label='dC/dk_1')
plt.legend(loc='best');
plt.xlabel('t');
plt.ylabel('sensitivity');


Elapsed time = 0.1 seconds

<Figure size 432x288 with 1 Axes>


You can see the two results are visually indistinguishable. Even the code is pretty similar. I would tend to prefer the autograd way since it should be less sensitive to finite difference artifacts, but it is nice to have an independent way to test if it is working.

org-mode source

Org-mode version = 9.2.3

## New publication in SoftwareX

| categories: | tags: | View Comments

Bibliometrics are increasingly used to quantify scholarly productivity. In this paper, we introduce a Python package called pybliometrics that provides a scriptable interface to Scopus to aggregate publication data for analysis. The package provides pretty comprehensive coverage of the APIs for author, abstract, affiliation and citation queries. The manuscript shows examples for downloading abstracts in bulk, building collaboration network graphs, and analyzing citation trends. You have to get a key from Scopus to access their databases, and the package provides some guidance on how to get it and configure the package. If you are interested in bibliometrics, this package may be useful to you!

@article{rose-2019-pybliom,
author =       {Michael E. Rose and John R. Kitchin},
title =        {Pybliometrics: Scriptable Bibliometrics Using a Python
Interface To Scopus},
journal =      {SoftwareX},
volume =       10,
number =       {nil},
pages =        100263,
year =         2019,
doi =          {10.1016/j.softx.2019.100263},
url =          {https://doi.org/10.1016/j.softx.2019.100263},
DATE_ADDED =   {Mon Jul 8 07:06:58 2019},
}


org-mode source

Org-mode version = 9.2.3

## An improvement for figures in ipython + scimax

| categories: ipython | tags: | View Comments

One of the best features of ipython in scimax is automatic inline images that you do not have to name. This has had a downside though, and that is it is not easy to use this and put attributes like names (so you can reference them later) or captions, or if you want a specific filename to get that. No more. Now you can use the :ipyfile header argument to control these. For example, if you use this in the header of the next block, it will save the images into the filenames you specified (in the order they are defined), and add attributes to the output. The syntax is just a list of plists (in elispese).

:ipyfile '((:name "clockwise" :filename "obipy-resources/clockwise.png" :caption "A clockwise spiral.") (:name "counterclockwise" :filename "obipy-resources/counterclockwise.png" :caption "A counterclockwise spiral."))


That allows you to refer to the clockwise one in Figure clockwise and the counterclockwise in Fig. counterclockwise. That may be helpful when using Ipython to write papers or for presentations where you might prefer named figures that are easy to find. Enjoy!

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

t = np.linspace(0, 20 * np.pi, 350)
x = np.exp(-0.1 * t) * np.sin(t)
y = np.exp(-0.1 * t) * np.cos(t)

plt.plot(x, y)
plt.axis('equal')

plt.figure()
plt.plot(y, x)

plt.axis('equal')

print('Length of t = {}'.format(len(t)))
print('x .dot. y = {}'.format(x @ y))


Length of t = 350 x .dot. y = 1.3598389888491538