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
625 lines (539 loc) · 24.6 KB
/
test_singlediode.py
File metadata and controls
625 lines (539 loc) · 24.6 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
"""
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)
import pytest
from numpy.testing import assert_array_equal
from .conftest import TESTS_DATA_DIR
from .conftest import chandrupatla, chandrupatla_available
POA = 888
TCELL = 55
@pytest.mark.parametrize('method', ['brentq', 'newton', chandrupatla])
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', chandrupatla])
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']
for col in is_array:
joined[col] = [np.asarray(a, dtype=np.float64) for a in joined[col]]
is_number = ['v_oc', 'i_sc', 'v_mp', 'i_mp', 'p_mp', 'i_x', 'i_xx',
'Temperature']
joined[is_number] = joined[is_number].astype(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'{TESTS_DATA_DIR}/precise_iv_curves_parameter_sets1.csv',
'json': f'{TESTS_DATA_DIR}/precise_iv_curves1.json'
},
{
'csv': f'{TESTS_DATA_DIR}/precise_iv_curves_parameter_sets2.csv',
'json': f'{TESTS_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',
chandrupatla])
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=1e-7, 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)
def test_singlediode_lambert_negative_voc(mocker):
"""Tests approximation to zero of v_oc when it is negative and small.
See singlediode.py:_lambertw > comment 'Set small elements <0 in v_oc to 0'
"""
# Next values should result in a negative v_oc out of `_lambertw_v_from_i`
# however, we can't ensure that the output belongs to (-1e-12, 0), so we
# mock it. It depends on the platform and Python distro. See issue #2000.
patcher = mocker.patch("pvlib.singlediode._lambertw_v_from_i")
x = np.array([0.0, 1.480501e-11, 0.178, 8000.0, 1.797559])
patcher.return_value = -9.999e-13
outs = pvsystem.singlediode(*x, method="lambertw")
assert outs["v_oc"] == 0
# Testing for an array
patcher.return_value = np.array([-9.999e-13, -1.001e-13])
x = np.array([x, x]).T
outs = pvsystem.singlediode(*x, method="lambertw")
assert_array_equal(outs["v_oc"], [0, 0])
@pytest.mark.parametrize('method', ['lambertw', 'brentq', 'newton',
chandrupatla])
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', chandrupatla])
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', chandrupatla])
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)
@pytest.fixture
def bishop88_arguments():
pvsyst_fs_495 = get_pvsyst_fs_495()
# evaluate PVSyst model with thin-film recombination loss current
# at reference conditions
x = pvsystem.calcparams_pvsyst(
effective_irradiance=pvsyst_fs_495['irrad_ref'],
temp_cell=pvsyst_fs_495['temp_ref'],
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']
)
y = dict(d2mutau=pvsyst_fs_495['d2mutau'],
NsVbi=VOLTAGE_BUILTIN*pvsyst_fs_495['cells_in_series'])
# Convert (*x, **y) in a bishop88_.* call to dict of arguments
args_dict = {
'photocurrent': x[0],
'saturation_current': x[1],
'resistance_series': x[2],
'resistance_shunt': x[3],
'nNsVth': x[4],
}
args_dict.update(y)
return args_dict
@pytest.mark.parametrize('method, method_kwargs', [
('newton', {
'tol': 1e-8,
'rtol': 1e-8,
'maxiter': 30,
}),
('brentq', {
'xtol': 1e-8,
'rtol': 1e-8,
'maxiter': 30,
}),
# can't include chandrupatla since the function is not available to patch
# TODO: add this once chandrupatla becomes non-optional functionality
# ('chandrupatla', {
# 'tolerances ': {'xtol': 1e-8, 'rtol': 1e-8},
# 'maxiter': 30,
# }),
])
def test_bishop88_kwargs_transfer(method, method_kwargs, mocker,
bishop88_arguments):
"""test method_kwargs modifying optimizer does not break anything"""
# patch method namespace at singlediode module namespace
optimizer_mock = mocker.patch('pvlib.singlediode.' + method)
# check kwargs passed to bishop_.* are a subset of the call args
# since they are called with more keyword arguments
bishop88_i_from_v(0, **bishop88_arguments, method=method,
method_kwargs=method_kwargs)
_, kwargs = optimizer_mock.call_args
assert method_kwargs.items() <= kwargs.items()
bishop88_v_from_i(0, **bishop88_arguments, method=method,
method_kwargs=method_kwargs)
_, kwargs = optimizer_mock.call_args
assert method_kwargs.items() <= kwargs.items()
bishop88_mpp(**bishop88_arguments, method=method,
method_kwargs=method_kwargs)
_, kwargs = optimizer_mock.call_args
assert method_kwargs.items() <= kwargs.items()
@pytest.mark.parametrize('method, method_kwargs', [
('newton', {
'tol': 1e-4,
'rtol': 1e-4,
'maxiter': 20,
'_inexistent_param': "0.01"
}),
('brentq', {
'xtol': 1e-4,
'rtol': 1e-4,
'maxiter': 20,
'_inexistent_param': "0.01"
}),
pytest.param('chandrupatla', {
'xtol': 1e-4,
'rtol': 1e-4,
'maxiter': 20,
'_inexistent_param': "0.01"
}, marks=pytest.mark.skipif(not chandrupatla_available,
reason="needs scipy 1.15")),
])
def test_bishop88_kwargs_fails(method, method_kwargs, bishop88_arguments):
"""test invalid method_kwargs passed onto the optimizer fail"""
pytest.raises(TypeError, bishop88_i_from_v,
0, **bishop88_arguments, method=method,
method_kwargs=method_kwargs)
pytest.raises(TypeError, bishop88_v_from_i,
0, **bishop88_arguments, method=method,
method_kwargs=method_kwargs)
pytest.raises(TypeError, bishop88_mpp,
**bishop88_arguments, method=method,
method_kwargs=method_kwargs)
@pytest.mark.parametrize('method', ['newton', 'brentq', chandrupatla])
def test_bishop88_full_output_kwarg(method, bishop88_arguments):
"""test call to bishop88_.* with full_output=True return values are ok"""
method_kwargs = {'full_output': True}
ret_val = bishop88_i_from_v(0, **bishop88_arguments, method=method,
method_kwargs=method_kwargs)
assert isinstance(ret_val, tuple) # ret_val must be a tuple
assert len(ret_val) == 2 # of two elements
assert isinstance(ret_val[0], float) # first one has bishop88 result
assert isinstance(ret_val[1], tuple) # second is output from optimizer
# any root finder returns at least 2 elements with full_output=True
assert len(ret_val[1]) >= 2
ret_val = bishop88_v_from_i(0, **bishop88_arguments, method=method,
method_kwargs=method_kwargs)
assert isinstance(ret_val, tuple) # ret_val must be a tuple
assert len(ret_val) == 2 # of two elements
assert isinstance(ret_val[0], float) # first one has bishop88 result
assert isinstance(ret_val[1], tuple) # second is output from optimizer
# any root finder returns at least 2 elements with full_output=True
assert len(ret_val[1]) >= 2
ret_val = bishop88_mpp(**bishop88_arguments, method=method,
method_kwargs=method_kwargs)
assert isinstance(ret_val, tuple) # ret_val must be a tuple
assert len(ret_val) == 2 # of two elements
assert isinstance(ret_val[0], tuple) # first one has bishop88 result
assert len(ret_val[0]) == 3 # of three elements (I,V,P)
assert isinstance(ret_val[1], tuple) # second is output from optimizer
# any root finder returns at least 2 elements with full_output=True
assert len(ret_val[1]) >= 2
@pytest.mark.parametrize('method', ['newton', 'brentq', chandrupatla])
def test_bishop88_pdSeries_len_one(method, bishop88_arguments):
for k, v in bishop88_arguments.items():
bishop88_arguments[k] = pd.Series([v])
# should not raise error
bishop88_i_from_v(pd.Series([0]), **bishop88_arguments, method=method)
bishop88_v_from_i(pd.Series([0]), **bishop88_arguments, method=method)
bishop88_mpp(**bishop88_arguments, method=method)
def _sde_check_solution(i, v, il, io, rs, rsh, a, d2mutau=0., NsVbi=np.inf):
vd = v + rs * i
return il - io*np.expm1(vd/a) - vd/rsh - il*d2mutau/(NsVbi - vd) - i
@pytest.mark.parametrize('method', ['newton', 'brentq', chandrupatla])
def test_bishop88_init_cond(method):
# GH 2013
p = {'alpha_sc': 0.0012256,
'gamma_ref': 1.2916241612804187,
'mu_gamma': 0.00047308959960937403,
'I_L_ref': 3.068717040806731,
'I_o_ref': 2.2691248021217617e-11,
'R_sh_ref': 7000,
'R_sh_0': 7000,
'R_s': 4.602,
'cells_in_series': 268,
'R_sh_exp': 5.5,
'EgRef': 1.5}
NsVbi = 268 * 0.9
d2mutau = 1.4
irrad = np.arange(20, 1100, 20)
tc = np.arange(-25, 74, 1)
weather = np.array(np.meshgrid(irrad, tc)).T.reshape(-1, 2)
# with the above parameters and weather conditions, a few combinations
# result in voc_est > NsVbi, which causes failure of brentq and newton
# when the recombination parameters NsVbi and d2mutau are used.
sde_params = pvsystem.calcparams_pvsyst(weather[:, 0], weather[:, 1], **p)
# test _mpp
result = bishop88_mpp(*sde_params, d2mutau=d2mutau, NsVbi=NsVbi)
imp, vmp, pmp = result
err = np.abs(_sde_check_solution(
imp, vmp, sde_params[0], sde_params[1], sde_params[2], sde_params[3],
sde_params[4], d2mutau=d2mutau, NsVbi=NsVbi))
bad_results = np.isnan(pmp) | (pmp < 0) | (err > 0.00001) # 0.01mA error
assert not bad_results.any()
# test v_from_i
vmp2 = bishop88_v_from_i(imp, *sde_params, d2mutau=d2mutau, NsVbi=NsVbi)
err = np.abs(_sde_check_solution(imp, vmp2, *sde_params, d2mutau=d2mutau,
NsVbi=NsVbi))
bad_results = np.isnan(vmp2) | (vmp2 < 0) | (err > 0.00001)
assert not bad_results.any()
# test v_from_i
imp2 = bishop88_i_from_v(vmp, *sde_params, d2mutau=d2mutau, NsVbi=NsVbi)
err = np.abs(_sde_check_solution(imp2, vmp, *sde_params, d2mutau=d2mutau,
NsVbi=NsVbi))
bad_results = np.isnan(imp2) | (imp2 < 0) | (err > 0.00001)
assert not bad_results.any()