forked from pvlib/pvlib-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_singlediode.py
More file actions
414 lines (353 loc) · 16.1 KB
/
Copy pathtest_singlediode.py
File metadata and controls
414 lines (353 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
"""
testing single-diode methods using JW Bishop 1988
"""
import numpy as np
import pandas as pd
import scipy
from pvlib import pvsystem
from pvlib.singlediode import (bishop88_mpp, estimate_voc, VOLTAGE_BUILTIN,
bishop88, bishop88_i_from_v, bishop88_v_from_i)
from pvlib._deprecation import pvlibDeprecationWarning
import pytest
from .conftest import DATA_DIR
POA = 888
TCELL = 55
@pytest.mark.parametrize('method', ['brentq', 'newton'])
def test_method_spr_e20_327(method, cec_module_spr_e20_327):
"""test pvsystem.singlediode with different methods on SPR-E20-327"""
spr_e20_327 = cec_module_spr_e20_327
x = pvsystem.calcparams_desoto(
effective_irradiance=POA, temp_cell=TCELL,
alpha_sc=spr_e20_327['alpha_sc'], a_ref=spr_e20_327['a_ref'],
I_L_ref=spr_e20_327['I_L_ref'], I_o_ref=spr_e20_327['I_o_ref'],
R_sh_ref=spr_e20_327['R_sh_ref'], R_s=spr_e20_327['R_s'],
EgRef=1.121, dEgdT=-0.0002677)
pvs = pvsystem.singlediode(*x, method='lambertw')
out = pvsystem.singlediode(*x, method=method)
assert np.isclose(pvs['i_sc'], out['i_sc'])
assert np.isclose(pvs['v_oc'], out['v_oc'])
assert np.isclose(pvs['i_mp'], out['i_mp'])
assert np.isclose(pvs['v_mp'], out['v_mp'])
assert np.isclose(pvs['p_mp'], out['p_mp'])
assert np.isclose(pvs['i_x'], out['i_x'])
assert np.isclose(pvs['i_xx'], out['i_xx'])
@pytest.mark.parametrize('method', ['brentq', 'newton'])
def test_newton_fs_495(method, cec_module_fs_495):
"""test pvsystem.singlediode with different methods on FS495"""
fs_495 = cec_module_fs_495
x = pvsystem.calcparams_desoto(
effective_irradiance=POA, temp_cell=TCELL,
alpha_sc=fs_495['alpha_sc'], a_ref=fs_495['a_ref'],
I_L_ref=fs_495['I_L_ref'], I_o_ref=fs_495['I_o_ref'],
R_sh_ref=fs_495['R_sh_ref'], R_s=fs_495['R_s'],
EgRef=1.475, dEgdT=-0.0003)
pvs = pvsystem.singlediode(*x, method='lambertw')
out = pvsystem.singlediode(*x, method=method)
assert np.isclose(pvs['i_sc'], out['i_sc'])
assert np.isclose(pvs['v_oc'], out['v_oc'])
assert np.isclose(pvs['i_mp'], out['i_mp'])
assert np.isclose(pvs['v_mp'], out['v_mp'])
assert np.isclose(pvs['p_mp'], out['p_mp'])
assert np.isclose(pvs['i_x'], out['i_x'])
assert np.isclose(pvs['i_xx'], out['i_xx'])
def build_precise_iv_curve_dataframe(file_csv, file_json):
"""
Reads a precise IV curve parameter set CSV and JSON to create a DataFrame.
The CSV contains the parameters of the single diode equation which are used
to generate the JSON data. The data are calculated using [1]_ with 40
decimal digits of precision in order have at least 16 decimal digits of
precision when they are stored in JSON. The precision is sufficient for the
difference between the left and right side of the single diode equation to
be less than :math:`1 \times 10^{-16}` when the numbers from the JSON are
read as mpmath floats. The code to generate these IV curve data is from
[2]_. The data and tests that use this function were added in :pull:`1573`.
Parameters
----------
file_csv: str
Path to a CSV file of IV curve parameter sets.
file_json: str
Path to a JSON file of precise IV curves.
Returns
-------
A DataFrame with these columns: ``Index``, ``photocurrent``,
``saturation_current``, ``resistance_series``, ``resistance_shunt``,
``n``, ``cells_in_series``, ``Voltages``, ``Currents``,
``diode_voltage``, ``v_oc``, ``i_sc``, ``v_mp``, ``i_mp``, ``p_mp``,
``i_x``, ``i_xx`, ``Temperature``, ``Irradiance``, ``Sweep Direction``,
``Datetime``, ``Boltzmann``, ``Elementary Charge``, and ``Vth``. The
columns ``Irradiance``, ``Sweep Direction`` are None or empty strings.
References
----------
.. [1] The mpmath development team. (2023). mpmath: a Python library for
arbitrary-precision floating-point arithmetic (version 1.2.1).
`mpmath <mpmath.org>`_
.. [2] The ivcurves development team. (2022). Code to generate precise
solutions to the single diode equation.
`ivcurves <github.com/cwhanse/ivcurves>`_
"""
params = pd.read_csv(file_csv)
curves_metadata = pd.read_json(file_json)
curves = pd.DataFrame(curves_metadata['IV Curves'].values.tolist())
curves['cells_in_series'] = curves_metadata['cells_in_series']
joined = params.merge(curves, on='Index', how='inner',
suffixes=(None, '_drop'), validate='one_to_one')
joined = joined[(c for c in joined.columns if not c.endswith('_drop'))]
# parse strings to np.float64
is_array = ['Currents', 'Voltages', 'diode_voltage']
joined[is_array] = joined[is_array].applymap(
lambda a: np.asarray(a, dtype=np.float64)
)
is_number = ['v_oc', 'i_sc', 'v_mp', 'i_mp', 'p_mp', 'i_x', 'i_xx',
'Temperature']
joined[is_number] = joined[is_number].applymap(np.float64)
joined['Boltzmann'] = scipy.constants.Boltzmann
joined['Elementary Charge'] = scipy.constants.elementary_charge
joined['Vth'] = (
joined['Boltzmann'] * joined['Temperature']
/ joined['Elementary Charge']
)
return joined
@pytest.fixture(scope='function', params=[
{
'csv': f'{DATA_DIR}/precise_iv_curves_parameter_sets1.csv',
'json': f'{DATA_DIR}/precise_iv_curves1.json'
},
{
'csv': f'{DATA_DIR}/precise_iv_curves_parameter_sets2.csv',
'json': f'{DATA_DIR}/precise_iv_curves2.json'
}
], ids=[1, 2])
def precise_iv_curves(request):
file_csv, file_json = request.param['csv'], request.param['json']
pc = build_precise_iv_curve_dataframe(file_csv, file_json)
params = ['photocurrent', 'saturation_current', 'resistance_series',
'resistance_shunt']
singlediode_params = pc.loc[:, params]
singlediode_params['nNsVth'] = pc['n'] * pc['cells_in_series'] * pc['Vth']
return singlediode_params, pc
@pytest.mark.parametrize('method', ['lambertw', 'brentq', 'newton'])
def test_singlediode_precision(method, precise_iv_curves):
"""
Tests the accuracy of singlediode. ivcurve_pnts is not tested.
"""
x, pc = precise_iv_curves
outs = pvsystem.singlediode(method=method, **x)
assert np.allclose(pc['i_sc'], outs['i_sc'], atol=1e-10, rtol=0)
assert np.allclose(pc['v_oc'], outs['v_oc'], atol=1e-10, rtol=0)
assert np.allclose(pc['i_mp'], outs['i_mp'], atol=7e-8, rtol=0)
assert np.allclose(pc['v_mp'], outs['v_mp'], atol=1e-6, rtol=0)
assert np.allclose(pc['p_mp'], outs['p_mp'], atol=1e-10, rtol=0)
assert np.allclose(pc['i_x'], outs['i_x'], atol=1e-10, rtol=0)
# This test should pass with atol=9e-8 on MacOS and Windows.
# The atol was lowered to pass on Linux when the vectorized umath module
# introduced in NumPy 1.22.0 is used.
assert np.allclose(pc['i_xx'], outs['i_xx'], atol=1e-6, rtol=0)
@pytest.mark.parametrize('method', ['lambertw'])
def test_ivcurve_pnts_precision(method, precise_iv_curves):
"""
Tests the accuracy of the IV curve points calcuated by singlediode. Only
methods of singlediode that linearly spaced points are tested.
"""
x, pc = precise_iv_curves
pc_i, pc_v = np.stack(pc['Currents']), np.stack(pc['Voltages'])
ivcurve_pnts = len(pc['Currents'][0])
with pytest.warns(pvlibDeprecationWarning, match='ivcurve_pnts'):
outs = pvsystem.singlediode(method=method, ivcurve_pnts=ivcurve_pnts,
**x)
assert np.allclose(pc_i, outs['i'], atol=1e-10, rtol=0)
assert np.allclose(pc_v, outs['v'], atol=1e-10, rtol=0)
@pytest.mark.parametrize('method', ['lambertw', 'brentq', 'newton'])
def test_v_from_i_i_from_v_precision(method, precise_iv_curves):
"""
Tests the accuracy of pvsystem.v_from_i and pvsystem.i_from_v.
"""
x, pc = precise_iv_curves
pc_i, pc_v = pc['Currents'], pc['Voltages']
for i, v, (_, x_one_curve) in zip(pc_i, pc_v, x.iterrows()):
out_i = pvsystem.i_from_v(voltage=v, method=method, **x_one_curve)
out_v = pvsystem.v_from_i(current=i, method=method, **x_one_curve)
assert np.allclose(i, out_i, atol=1e-10, rtol=0)
assert np.allclose(v, out_v, atol=1e-10, rtol=0)
def get_pvsyst_fs_495():
"""
PVsyst parameters for First Solar FS-495 module from PVSyst-6.7.2 database.
I_L_ref derived from Isc_ref conditions::
I_L_ref = (I_sc_ref + Id + Ish) / (1 - d2mutau/(Vbi*N_s - Vd))
where::
Vd = I_sc_ref * R_s
Id = I_o_ref * (exp(Vd / nNsVt) - 1)
Ish = Vd / R_sh_ref
"""
return {
'd2mutau': 1.31, 'alpha_sc': 0.00039, 'gamma_ref': 1.48,
'mu_gamma': 0.001, 'I_o_ref': 9.62e-10, 'R_sh_ref': 5000,
'R_sh_0': 12500, 'R_sh_exp': 3.1, 'R_s': 4.6, 'beta_oc': -0.2116,
'EgRef': 1.5, 'cells_in_series': 108, 'cells_in_parallel': 2,
'I_sc_ref': 1.55, 'V_oc_ref': 86.5, 'I_mp_ref': 1.4, 'V_mp_ref': 67.85,
'temp_ref': 25, 'irrad_ref': 1000, 'I_L_ref': 1.5743233463848496
}
# DeSoto @(888[W/m**2], 55[degC]) = {Pmp: 72.71, Isc: 1.402, Voc: 75.42)
@pytest.mark.parametrize(
'poa, temp_cell, expected, tol', [
# reference conditions
(
get_pvsyst_fs_495()['irrad_ref'],
get_pvsyst_fs_495()['temp_ref'],
{
'pmp': (get_pvsyst_fs_495()['I_mp_ref'] *
get_pvsyst_fs_495()['V_mp_ref']),
'isc': get_pvsyst_fs_495()['I_sc_ref'],
'voc': get_pvsyst_fs_495()['V_oc_ref']
},
(5e-4, 0.04)
),
# other conditions
(
POA,
TCELL,
{
'pmp': 76.262,
'isc': 1.3868,
'voc': 79.292
},
(1e-4, 1e-4)
)
]
)
@pytest.mark.parametrize('method', ['newton', 'brentq'])
def test_pvsyst_recombination_loss(method, poa, temp_cell, expected, tol):
"""test PVSst recombination loss"""
pvsyst_fs_495 = get_pvsyst_fs_495()
# first evaluate PVSyst model with thin-film recombination loss current
# at reference conditions
x = pvsystem.calcparams_pvsyst(
effective_irradiance=poa, temp_cell=temp_cell,
alpha_sc=pvsyst_fs_495['alpha_sc'],
gamma_ref=pvsyst_fs_495['gamma_ref'],
mu_gamma=pvsyst_fs_495['mu_gamma'], I_L_ref=pvsyst_fs_495['I_L_ref'],
I_o_ref=pvsyst_fs_495['I_o_ref'], R_sh_ref=pvsyst_fs_495['R_sh_ref'],
R_sh_0=pvsyst_fs_495['R_sh_0'], R_sh_exp=pvsyst_fs_495['R_sh_exp'],
R_s=pvsyst_fs_495['R_s'],
cells_in_series=pvsyst_fs_495['cells_in_series'],
EgRef=pvsyst_fs_495['EgRef']
)
il_pvsyst, io_pvsyst, rs_pvsyst, rsh_pvsyst, nnsvt_pvsyst = x
voc_est_pvsyst = estimate_voc(photocurrent=il_pvsyst,
saturation_current=io_pvsyst,
nNsVth=nnsvt_pvsyst)
vd_pvsyst = np.linspace(0, voc_est_pvsyst, 1000)
pvsyst = bishop88(
diode_voltage=vd_pvsyst, photocurrent=il_pvsyst,
saturation_current=io_pvsyst, resistance_series=rs_pvsyst,
resistance_shunt=rsh_pvsyst, nNsVth=nnsvt_pvsyst,
d2mutau=pvsyst_fs_495['d2mutau'],
NsVbi=VOLTAGE_BUILTIN*pvsyst_fs_495['cells_in_series']
)
# test max power
assert np.isclose(max(pvsyst[2]), expected['pmp'], *tol)
# test short circuit current
isc_pvsyst = np.interp(0, pvsyst[1], pvsyst[0])
assert np.isclose(isc_pvsyst, expected['isc'], *tol)
# test open circuit voltage
voc_pvsyst = np.interp(0, pvsyst[0][::-1], pvsyst[1][::-1])
assert np.isclose(voc_pvsyst, expected['voc'], *tol)
# repeat tests as above with specialized bishop88 functions
y = dict(d2mutau=pvsyst_fs_495['d2mutau'],
NsVbi=VOLTAGE_BUILTIN*pvsyst_fs_495['cells_in_series'])
mpp_88 = bishop88_mpp(*x, **y, method=method)
assert np.isclose(mpp_88[2], expected['pmp'], *tol)
isc_88 = bishop88_i_from_v(0, *x, **y, method=method)
assert np.isclose(isc_88, expected['isc'], *tol)
voc_88 = bishop88_v_from_i(0, *x, **y, method=method)
assert np.isclose(voc_88, expected['voc'], *tol)
ioc_88 = bishop88_i_from_v(voc_88, *x, **y, method=method)
assert np.isclose(ioc_88, 0.0, *tol)
vsc_88 = bishop88_v_from_i(isc_88, *x, **y, method=method)
assert np.isclose(vsc_88, 0.0, *tol)
@pytest.mark.parametrize(
'brk_params, recomb_params, poa, temp_cell, expected, tol', [
# reference conditions without breakdown model
(
(0., -5.5, 3.28),
(get_pvsyst_fs_495()['d2mutau'],
VOLTAGE_BUILTIN * get_pvsyst_fs_495()['cells_in_series']),
get_pvsyst_fs_495()['irrad_ref'],
get_pvsyst_fs_495()['temp_ref'],
{
'pmp': (get_pvsyst_fs_495()['I_mp_ref'] * # noqa: W504
get_pvsyst_fs_495()['V_mp_ref']),
'isc': get_pvsyst_fs_495()['I_sc_ref'],
'voc': get_pvsyst_fs_495()['V_oc_ref']
},
(5e-4, 0.04)
),
# other conditions with breakdown model on and recombination model off
(
(1.e-4, -5.5, 3.28),
(0., np.Inf),
POA,
TCELL,
{
'pmp': 79.723,
'isc': 1.4071,
'voc': 79.646
},
(1e-4, 1e-4)
)
]
)
@pytest.mark.parametrize('method', ['newton', 'brentq'])
def test_pvsyst_breakdown(method, brk_params, recomb_params, poa, temp_cell,
expected, tol):
"""test PVSyst recombination loss"""
pvsyst_fs_495 = get_pvsyst_fs_495()
# first evaluate PVSyst model with thin-film recombination loss current
# at reference conditions
x = pvsystem.calcparams_pvsyst(
effective_irradiance=poa, temp_cell=temp_cell,
alpha_sc=pvsyst_fs_495['alpha_sc'],
gamma_ref=pvsyst_fs_495['gamma_ref'],
mu_gamma=pvsyst_fs_495['mu_gamma'], I_L_ref=pvsyst_fs_495['I_L_ref'],
I_o_ref=pvsyst_fs_495['I_o_ref'], R_sh_ref=pvsyst_fs_495['R_sh_ref'],
R_sh_0=pvsyst_fs_495['R_sh_0'], R_sh_exp=pvsyst_fs_495['R_sh_exp'],
R_s=pvsyst_fs_495['R_s'],
cells_in_series=pvsyst_fs_495['cells_in_series'],
EgRef=pvsyst_fs_495['EgRef']
)
il_pvsyst, io_pvsyst, rs_pvsyst, rsh_pvsyst, nnsvt_pvsyst = x
d2mutau, NsVbi = recomb_params
breakdown_factor, breakdown_voltage, breakdown_exp = brk_params
voc_est_pvsyst = estimate_voc(photocurrent=il_pvsyst,
saturation_current=io_pvsyst,
nNsVth=nnsvt_pvsyst)
vd_pvsyst = np.linspace(0, voc_est_pvsyst, 1000)
pvsyst = bishop88(
diode_voltage=vd_pvsyst, photocurrent=il_pvsyst,
saturation_current=io_pvsyst, resistance_series=rs_pvsyst,
resistance_shunt=rsh_pvsyst, nNsVth=nnsvt_pvsyst,
d2mutau=d2mutau, NsVbi=NsVbi,
breakdown_factor=breakdown_factor, breakdown_voltage=breakdown_voltage,
breakdown_exp=breakdown_exp
)
# test max power
assert np.isclose(max(pvsyst[2]), expected['pmp'], *tol)
# test short circuit current
isc_pvsyst = np.interp(0, pvsyst[1], pvsyst[0])
assert np.isclose(isc_pvsyst, expected['isc'], *tol)
# test open circuit voltage
voc_pvsyst = np.interp(0, pvsyst[0][::-1], pvsyst[1][::-1])
assert np.isclose(voc_pvsyst, expected['voc'], *tol)
# repeat tests as above with specialized bishop88 functions
y = {'d2mutau': recomb_params[0], 'NsVbi': recomb_params[1],
'breakdown_factor': brk_params[0], 'breakdown_voltage': brk_params[1],
'breakdown_exp': brk_params[2]}
mpp_88 = bishop88_mpp(*x, **y, method=method)
assert np.isclose(mpp_88[2], expected['pmp'], *tol)
isc_88 = bishop88_i_from_v(0, *x, **y, method=method)
assert np.isclose(isc_88, expected['isc'], *tol)
voc_88 = bishop88_v_from_i(0, *x, **y, method=method)
assert np.isclose(voc_88, expected['voc'], *tol)
ioc_88 = bishop88_i_from_v(voc_88, *x, **y, method=method)
assert np.isclose(ioc_88, 0.0, *tol)
vsc_88 = bishop88_v_from_i(isc_88, *x, **y, method=method)
assert np.isclose(vsc_88, 0.0, *tol)