In [2]:
from pycalphad.tests.datasets import *
from pycalphad.core.solver import InteriorPointSolver
from pycalphad import equilibrium, Database, variables as v

ROSE_DBF = Database(ROSE_TDB)

class ProblemSaver(InteriorPointSolver):
    saved_problem = [None]
    def solve(self, prob):
        self.saved_problem[0] = prob
        self.verbose = True
        return super(ProblemSaver, self).solve(prob)

my_phases_rose = ['TEST']
comps = ['H', 'HE', 'LI', 'BE', 'B', 'C', 'N', 'O', 'F']
conds = dict({v.T: 1000, v.P: 101325})
for comp in comps[:-1]:
    conds[v.X(comp)] = 1.0/float(len(comps))
eqx = equilibrium(ROSE_DBF, comps, my_phases_rose, conds, verbose=False, solver=ProblemSaver())


Trying to improve poor solution
Chemical Potentials [-5888.80421816 -5888.88999055 -5888.86226516   922.04914093
 -5888.84858817   922.04914235   922.04914012   922.04914135
 -5780.80269595]
[5.00000000e-13 4.93461633e-18 5.00000000e-16 1.01572217e-12
 4.02593656e-10 1.01572216e-12 1.70412330e-10 4.02603357e-10
 1.70412330e-10 1.70412330e-10 1.70412330e-10 3.97234755e-10
 4.02588691e-10 1.01572217e-12 4.02575925e-10 1.70413568e-10
 1.01572218e-12 1.70413568e-10 1.70413568e-10 1.70413568e-10
 3.97237959e-10 3.95169500e-10 3.95180478e-10 3.95178123e-10
 2.01276775e-12 3.95174158e-10 2.01276775e-12 2.01276775e-12
 2.01276775e-12 3.89912783e-10 5.93726717e-11 5.93725966e-11
 5.93726165e-11 8.72995153e-12 5.93726233e-11 8.72995153e-12
 8.72995153e-12 8.72995154e-12 6.78225348e-13 2.25677632e-12
 2.25677600e-12 1.22658480e-12 3.35008067e-12]
[1.00000000e+00 1.01325000e+05 1.00000000e+03 4.92260594e-01
 1.24194703e-03 4.92260600e-01 2.93405999e-03 1.24191710e-03
 2.93405999e-03 2.93405999e-03 2.93405999e-03 1.25870154e-03
 1.24196235e-03 4.92260597e-01 1.24200164e-03 2.93403868e-03
 4.92260593e-01 2.93403868e-03 2.93403868e-03 2.93403868e-03
 1.25869139e-03 1.26527984e-03 1.26524463e-03 1.26525216e-03
 2.48414155e-01 1.26526492e-03 2.48414155e-01 2.48414155e-01
 2.48414155e-01 1.28233804e-03 8.42138286e-03 8.42139350e-03
 8.42139069e-03 5.72740866e-02 8.42138972e-03 5.72740866e-02
 5.72740866e-02 5.72740866e-02 7.37218097e-01 2.21555966e-01
 2.21555997e-01 4.07636900e-01 1.49251137e-01]
Status: 0 b'Algorithm terminated successfully at a locally optimal point, satisfying the convergence tolerances (can be specified by options).'
Trying to improve poor solution
Chemical Potentials [-5837.1469373  -5837.14692816 -5837.14694375 -5837.14694244
 -5837.14692961 -5837.14692393 -5837.14694007 -5837.14692796
 -4345.25461207]
[5.00000000e-13 4.93461633e-18 5.00000000e-16 1.00908054e-12
 4.00267334e-10 1.00908054e-12 4.00267287e-10 4.00267334e-10
 4.00267385e-10 4.00267287e-10 4.00267337e-10 3.32483942e-10
 4.00267271e-10 1.00908054e-12 4.00267271e-10 4.00267272e-10
 1.00908054e-12 4.00267386e-10 4.00267272e-10 4.00267338e-10
 3.32483916e-10 4.00267286e-10 4.00267334e-10 4.00267286e-10
 1.00908054e-12 4.00267334e-10 4.00267385e-10 1.00908054e-12
 4.00267337e-10 3.32483943e-10 1.98662983e-11 1.98662982e-11
 1.98662983e-11 1.98662983e-11 1.98662982e-11 1.98662986e-11
 1.98662983e-11 1.98662978e-11 6.26053347e-13 4.00267261e-10
 4.00267328e-10 4.00267261e-10 4.00267262e-10 4.00267328e-10
 1.00908054e-12 4.00267262e-10 1.00908054e-12 3.32483952e-10
 2.31884841e-12 2.31884841e-12 2.31884841e-12 3.63641884e-12
 2.31884841e-12]
[1.00000000e+00 1.01325000e+05 1.00000000e+03 4.95500590e-01
 1.24916477e-03 4.95500590e-01 1.24916476e-03 1.24916477e-03
 1.24916477e-03 1.24916476e-03 1.24916476e-03 1.50383233e-03
 1.24916474e-03 4.95500590e-01 1.24916474e-03 1.24916474e-03
 4.95500590e-01 1.24916475e-03 1.24916474e-03 1.24916474e-03
 1.50383230e-03 1.24916476e-03 1.24916477e-03 1.24916476e-03
 4.95500590e-01 1.24916477e-03 1.24916477e-03 4.95500590e-01
 1.24916476e-03 1.50383233e-03 2.51682525e-02 2.51682526e-02
 2.51682525e-02 2.51682525e-02 2.51682526e-02 2.51682517e-02
 2.51682525e-02 2.51682533e-02 7.98653980e-01 1.24916473e-03
 1.24916474e-03 1.24916473e-03 1.24916473e-03 1.24916474e-03
 4.95500590e-01 1.24916473e-03 4.95500589e-01 1.50383230e-03
 2.15625272e-01 2.15625272e-01 2.15625272e-01 1.37498913e-01
 2.15625272e-01]
Status: 0 b'Algorithm terminated successfully at a locally optimal point, satisfying the convergence tolerances (can be specified by options).'
Trying to improve poor solution
Chemical Potentials [-5835.09925112 -5835.09915364 -5835.09937106 -5835.09904147
 -5835.09903046 -5835.09927146 -5835.09915856 -5835.09791929
 -5835.10036419]
[5.00000000e-13 4.93461633e-18 5.00000000e-16 1.00882338e-12
 4.00171676e-10 1.00882338e-12 4.00171859e-10 4.00171629e-10
 4.00175283e-10 4.00171904e-10 4.00175203e-10 4.00175434e-10
 4.00173085e-10 1.00882338e-12 4.00173131e-10 4.00174390e-10
 1.00882338e-12 4.00172601e-10 4.00174435e-10 4.00172521e-10
 4.00172752e-10 4.00173529e-10 4.00174651e-10 4.00173574e-10
 1.00882338e-12 4.00174604e-10 4.00172164e-10 1.00882338e-12
 4.00172084e-10 4.00172315e-10 4.00173701e-10 4.00173688e-10
 4.00173656e-10 4.00173733e-10 4.00173736e-10 4.00173784e-10
 4.00173689e-10 1.00881915e-12 1.00882760e-12 4.00173733e-10
 4.00173720e-10 4.00173687e-10 4.00173765e-10 4.00173767e-10
 1.00882243e-12 4.00173720e-10 1.00882431e-12 4.00173756e-10
 4.00173667e-10 4.00173655e-10 4.00173622e-10 4.00173699e-10
 4.00173702e-10 1.00882479e-12 4.00173655e-10 4.00173699e-10
 1.00882196e-12 2.25001012e-12 2.25001012e-12 2.25001012e-12
 4.50004265e-12 4.50005313e-12 4.50002581e-12]
[1.00000000e+00 1.01325000e+05 1.00000000e+03 4.95626896e-01
 1.24946022e-03 4.95626896e-01 1.24945998e-03 1.24946028e-03
 1.24945576e-03 1.24945992e-03 1.24945599e-03 1.24945550e-03
 1.24945837e-03 4.95626896e-01 1.24945831e-03 1.24945665e-03
 4.95626896e-01 1.24945929e-03 1.24945659e-03 1.24945951e-03
 1.24945903e-03 1.24945778e-03 1.24945631e-03 1.24945773e-03
 4.95626896e-01 1.24945636e-03 1.24945987e-03 4.95626896e-01
 1.24946009e-03 1.24945960e-03 1.24945850e-03 1.24945851e-03
 1.24945855e-03 1.24945846e-03 1.24945845e-03 1.24945797e-03
 1.24945851e-03 4.95628987e-01 4.95624804e-01 1.24945843e-03
 1.24945844e-03 1.24945848e-03 1.24945838e-03 1.24945838e-03
 4.95627361e-01 1.24945844e-03 4.95626431e-01 1.24945789e-03
 1.24945860e-03 1.24945861e-03 1.24945866e-03 1.24945856e-03
 1.24945856e-03 4.95626195e-01 1.24945861e-03 1.24945826e-03
 4.95627595e-01 2.22222222e-01 2.22222222e-01 2.22222222e-01
 1.11111058e-01 1.11110798e-01 1.11111477e-01]
Status: 0 b'Algorithm terminated successfully at a locally optimal point, satisfying the convergence tolerances (can be specified by options).'

In [13]:
import numpy as np
soln = np.array(
    [1.00000000e+00, 1.01325000e+05, 1.00000000e+03, 4.95626896e-01,
 1.24946022e-03, 4.95626896e-01, 1.24945998e-03, 1.24946028e-03,
 1.24945576e-03, 1.24945992e-03, 1.24945599e-03, 1.24945550e-03,
 1.24945837e-03, 4.95626896e-01, 1.24945831e-03, 1.24945665e-03,
 4.95626896e-01, 1.24945929e-03, 1.24945659e-03, 1.24945951e-03,
 1.24945903e-03, 1.24945778e-03, 1.24945631e-03, 1.24945773e-03,
 4.95626896e-01, 1.24945636e-03, 1.24945987e-03, 4.95626896e-01,
 1.24946009e-03, 1.24945960e-03, 1.24945850e-03, 1.24945851e-03,
 1.24945855e-03, 1.24945846e-03, 1.24945845e-03, 1.24945797e-03,
 1.24945851e-03, 4.95628987e-01, 4.95624804e-01, 1.24945843e-03,
 1.24945844e-03, 1.24945848e-03, 1.24945838e-03, 1.24945838e-03,
 4.95627361e-01, 1.24945844e-03, 4.95626431e-01, 1.24945789e-03,
 1.24945860e-03, 1.24945861e-03, 1.24945866e-03, 1.24945856e-03,
 1.24945856e-03, 4.95626195e-01, 1.24945861e-03, 1.24945826e-03,
 4.95627595e-01, 2.22222222e-01, 2.22222222e-01, 2.22222222e-01,
 1.11111058e-01, 1.11110798e-01, 1.11111477e-01]
)

temps = np.arange(300,1000,1e-1)
obj = np.zeros_like(temps)
grad = np.zeros_like(temps)
error = np.zeros_like(temps)
for idx, temp in enumerate(temps):
    copy_of_soln = np.array(soln)
    copy_of_soln[2] = temp
    obj[idx] = ProblemSaver.saved_problem[0].objective(copy_of_soln)
    grad[idx] = ProblemSaver.saved_problem[0].gradient(copy_of_soln)[2]
error = np.abs(np.gradient(obj, 1e-1) - grad)

In [57]:
cons_idx = -4
selected_index = 6
step = 1e-5
inputs = np.arange(1e-12,1-1e-12,step)
obj = np.zeros_like(inputs)
grad = np.zeros_like(inputs)
error = np.zeros_like(inputs)
for idx, val in enumerate(inputs):
    copy_of_soln = np.array(soln)
    copy_of_soln[selected_index] = val
    obj[idx] = ProblemSaver.saved_problem[0].constraints(copy_of_soln)[cons_idx]
    grad[idx] = ProblemSaver.saved_problem[0].jacobian(copy_of_soln)[cons_idx,selected_index]
error = np.abs(np.gradient(obj, step) - grad)

In [58]:
%matplotlib inline
import matplotlib.pyplot as plt

plt.semilogy(inputs, error)
#plt.ylim((1e-13,1e-6))


Out[58]:
[<matplotlib.lines.Line2D at 0x7f53adc92400>]

In [59]:
error


Out[59]:
array([2.78568275e-03, 9.06824198e-08, 2.67480118e-07, ...,
       2.36121409e-07, 8.52935287e-07, 3.48522489e-04])

In [ ]: