def DAA03(massAcid, emf, tempK, massSample, concAcid, concTotals, eqConstants): """Solve for alkalinity and f using the Dickson CRM method [DAA03].""" alkGuess, emf0Guess, hGuess, pHGuess = guessGran(massAcid, emf, tempK, massSample, concAcid) L = logical_and(pHGuess > 3, pHGuess < 3.5) concTotalsL, eqConstantsL = _eqConcL(concTotals, eqConstants, L) optResult = olsq(lambda alk_f: _lsqfun_DAA03(massAcid[L], hGuess[L], massSample, concAcid, alk_f[1], alk_f[0], concTotalsL, eqConstantsL), [alkGuess, 1], x_scale=[1e-3, 1], method='lm') optResult['L'] = L return optResult
def pH(massAcid, massSample, concAcid, alk0, concTotals, eqConstants): """Simulate pH from known total alkalinity and total concentrations.""" pH = full_like(massAcid, nan) mu = solve.mu(massAcid, massSample) for i, massAcid_i in enumerate(massAcid): iConcTotals = {k: v if size(v)==1 else v[i] for k, v in concTotals.items()} pH[i] = olsq(lambda pH: alk(10.0**-pH, mu[i], iConcTotals, eqConstants)[0] - mu[i]*alk0 + massAcid_i*concAcid/ (massAcid_i + massSample), 8.0, method='lm')['x'][0] return pH
def complete(massAcid, emf, tempK, massSample, concAcid, concTotals, eqConstants): """Solve for alkalinity and EMF0 using the complete calculation method.""" alkGuess, emf0Guess, _, pHGuess = guessGran(massAcid, emf, tempK, massSample, concAcid) L = logical_and(pHGuess > 3, pHGuess < 4) concTotalsL, eqConstantsL = _eqConcL(concTotals, eqConstants, L) optResult = olsq(lambda alk_emf0: _lsqfunComplete(massAcid[L], emf[L], tempK[L], massSample, concAcid, alk_emf0[1], alk_emf0[0], concTotalsL, eqConstantsL), [alkGuess, emf0Guess], x_scale=[1e-6, 1], method='lm') optResult['L'] = L return optResult
def Dickson1981(massAcid, emf, tempK, massSample, concAcid, concTotals, eqConstants): """Solve for alkalinity, DIC and f using the closed-cell method [D81].""" alkGuess, EMF0g, hGuess, pHGuess = guessGran(massAcid, emf, tempK, massSample, concAcid) L = pHGuess > 5 concTotalsL, eqConstantsL = _eqConcL(concTotals, eqConstants, L) optResult = olsq(lambda alk_totalCarbonate_f: _lsqfun_Dickson1981(massAcid[L], hGuess[L], massSample, concAcid, alk_totalCarbonate_f[2], alk_totalCarbonate_f[0], alk_totalCarbonate_f[1], concTotalsL, eqConstantsL), [alkGuess, alkGuess*0.95, 1], x_scale=[1e-3, 1e-3, 1], method='lm') optResult['L'] = L return optResult
def concAcid(massAcid, emf, tempK, massSample, alkCert, concTotals, eqConstants, solver='complete', **kwargs): """Calibrate the acid concentration using known sample alkalinity.""" if solver.lower() in solve.allSolvers.keys(): solveFunc = solve.allSolvers[solver.lower()] concAcidOptResult = olsq(lambda concAcid: solveFunc(massAcid, emf, tempK, massSample, concAcid, concTotals, eqConstants, **kwargs)['x'][0] - alkCert, 0.1, method='lm') else: print('calkulate.calibrate.concAcid: solver not recognised.') print('Options (case-insensitive):' + (len(solve.allSolvers.keys())*' \'{}\'').format( *solve.allSolvers.keys())) concAcidOptResult = {'x': [None,]} return concAcidOptResult
def dotest(): return olsq(lambda x: x - 5.,0)['x']