<commit_before><commit_msg>Create fitTest.py<commit_after>#!/usr/bin/env python3

# written by <NAME> <first name [at] last name [not] net>
# this tests different fitting algorithms

#import mpmath.libmp
#assert mpmath.libmp.BACKEND == 'gmpy'
import numpy as np
from numpy import nan
from numpy import inf

from scipy.special import lambertw

from scipy import optimize

import matplotlib.pyplot as plt
plt.switch_backend("Qt5Agg")

v = [-0.19994928, -0.14574878, -0.09155122, -0.03743893,  0.01663798,
       0.07081506,  0.12496336,  0.17912959,  0.23336065,  0.28753638,
       0.34162751,  0.39578977,  0.44999278,  0.50416487,  0.55837834,
       0.61247802,  0.6666159 ,  0.72084767,  0.77501357,  0.82919377,
       0.88335657,  0.93745446,  0.99165738,  1.04589999,  1.1000551 ]

i = [ 0.00631131,  0.00633053,  0.0062757 ,  0.00631208,  0.00625346,
        0.00628661,  0.00627005,  0.00628372,  0.00624534,  0.00622571,
        0.00622486,  0.00623647,  0.00620945,  0.00620305,  0.00621628,
        0.00608415,  0.00577381,  0.0052973 ,  0.00453748,  0.00347691,
        0.00208499,  0.00020583, -0.00227922, -0.00524322, -0.00856056]

#v = [np.complex128(x) for x in v]
#i = [np.complex128(x) for x in i]

cellTemp = 29; #degC all analysis is done assuming the cell is at 29 degC
T = 273.15 + cellTemp; #cell temp in K
K = 1.3806488e-23; #boltzman constant
q = 1.60217657e-19; #electron charge
thermalVoltage = K*T/q; #thermal voltage ~26mv	

lambertWTol = 1e-16
def optimizeThis (x, I0, Iph, Rs, Rsh, n):
    #return (Rs*(I0*Rsh + Iph*Rsh - x) - thermalVoltage*n*(Rs + Rsh)*lambertw(I0*Rs*Rsh*np.exp((Rs*(I0*Rsh + Iph*Rsh - x) + x*(Rs + Rsh))/(thermalVoltage*n*(Rs + Rsh)))/(thermalVoltage*n*(Rs + Rsh)),tol=lambertWTol))/(Rs*(Rs + Rsh))
    return np.real_if_close((Rs*(I0*Rsh + Iph*Rsh - x) - thermalVoltage*n*(Rs + Rsh)*lambertw(I0*Rs*Rsh*np.exp((Rs*(I0*Rsh + Iph*Rsh - x) + x*(Rs + Rsh))/(thermalVoltage*n*(Rs + Rsh)))/(thermalVoltage*n*(Rs + Rsh)),tol=lambertWTol))/(Rs*(Rs + Rsh)))

# [I0, I_L, R_s, R_sh, n]
guess = [7.974383037191594e-11, 0.00627619846736794, 12.743239329693433, 5694.842341863068, 2.0]
#guess = [np.complex128(x) for x in guess]

myXtol = np.float(1e-30)
myFtol = np.float(1e-30)
myGtol = np.float(1e-30)
myMax_nfev = 12000

#I0_bounds = [0, inf]
#I_L_bounds = [0, inf]
#R_s_bounds = [0, inf]
#R_sh_bounds = [0, inf]
#n_bounds = [0, 4]

I0_bounds = [-inf, inf]
I_L_bounds = [-inf, inf]
R_s_bounds = [-inf, inf]
R_sh_bounds = [-inf, inf]
n_bounds = [-inf, inf]

myBounds=([I0_bounds[0], I_L_bounds[0], R_s_bounds[0], R_sh_bounds[0], n_bounds[0]], [I0_bounds[1], I_L_bounds[1], R_s_bounds[1], R_sh_bounds[1], n_bounds[1]])
#myMethod = 'trf'
myMethod = 'dogbox'
#myMethod = 'lm'

#fitReturn = optimize.curve_fit(optimizeThis, v, i, p0=guess, bounds=myBounds, method=myMethod, jac ='cs', x_scale="jac", max_nfev=myMax_nfev, loss="soft_l1", tr_options={'tr_solver': "lsmr"}, verbose=2)
fitReturn = optimize.curve_fit(optimizeThis, v, i, p0=guess, jac ='cs', xtol=0, ftol=1e-20, x_scale="jac", method=myMethod, verbose=2,max_nfev=1200000)

fitParams = fitReturn[0]
sumSqErr = sum([(optimizeThis(x, *fitParams)-y)**2 for x,y in zip(v,i)])

print(fitReturn)
print("The sum of the square of the errors is:")
print(sumSqErr)
{'fraction_non_alphanumeric': 0.1727636028281586, 'fraction_numerical': 0.19151552413157086, 'mean_word_length': 4.432387312186978, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3766470', 'n_tokens_mistral': 1860, 'n_tokens_neox': 1505, 'n_words': 300}