forked from pvlib/pvlib-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_modelchain.py
More file actions
2036 lines (1750 loc) · 85.5 KB
/
test_modelchain.py
File metadata and controls
2036 lines (1750 loc) · 85.5 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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import numpy as np
import pandas as pd
from pvlib import iam, modelchain, pvsystem, temperature, inverter
from pvlib.modelchain import ModelChain
from pvlib.pvsystem import PVSystem
from pvlib.location import Location
from pvlib._deprecation import pvlibDeprecationWarning
from .conftest import assert_series_equal, assert_frame_equal
import pytest
from .conftest import fail_on_pvlib_version
@pytest.fixture(scope='function')
def sapm_dc_snl_ac_system(sapm_module_params, cec_inverter_parameters,
sapm_temperature_cs5p_220m):
module = 'Canadian_Solar_CS5P_220M___2009_'
module_parameters = sapm_module_params.copy()
temp_model_params = sapm_temperature_cs5p_220m.copy()
system = PVSystem(surface_tilt=32.2, surface_azimuth=180,
module=module,
module_parameters=module_parameters,
temperature_model_parameters=temp_model_params,
inverter_parameters=cec_inverter_parameters)
return system
@pytest.fixture
def cec_dc_snl_ac_system(cec_module_cs5p_220m, cec_inverter_parameters,
sapm_temperature_cs5p_220m):
module_parameters = cec_module_cs5p_220m.copy()
module_parameters['b'] = 0.05
module_parameters['EgRef'] = 1.121
module_parameters['dEgdT'] = -0.0002677
temp_model_params = sapm_temperature_cs5p_220m.copy()
system = PVSystem(surface_tilt=32.2, surface_azimuth=180,
module=module_parameters['Name'],
module_parameters=module_parameters,
temperature_model_parameters=temp_model_params,
inverter_parameters=cec_inverter_parameters)
return system
@pytest.fixture
def cec_dc_snl_ac_arrays(cec_module_cs5p_220m, cec_inverter_parameters,
sapm_temperature_cs5p_220m):
module_parameters = cec_module_cs5p_220m.copy()
module_parameters['b'] = 0.05
module_parameters['EgRef'] = 1.121
module_parameters['dEgdT'] = -0.0002677
temp_model_params = sapm_temperature_cs5p_220m.copy()
array_one = pvsystem.Array(
mount=pvsystem.FixedMount(surface_tilt=32.2, surface_azimuth=180),
module=module_parameters['Name'],
module_parameters=module_parameters.copy(),
temperature_model_parameters=temp_model_params.copy()
)
array_two = pvsystem.Array(
mount=pvsystem.FixedMount(surface_tilt=42.2, surface_azimuth=220),
module=module_parameters['Name'],
module_parameters=module_parameters.copy(),
temperature_model_parameters=temp_model_params.copy()
)
system = PVSystem(
arrays=[array_one, array_two],
inverter_parameters=cec_inverter_parameters
)
return system
@pytest.fixture
def cec_dc_native_snl_ac_system(cec_module_cs5p_220m, cec_inverter_parameters,
sapm_temperature_cs5p_220m):
module_parameters = cec_module_cs5p_220m.copy()
temp_model_params = sapm_temperature_cs5p_220m.copy()
system = PVSystem(surface_tilt=32.2, surface_azimuth=180,
module=module_parameters['Name'],
module_parameters=module_parameters,
temperature_model_parameters=temp_model_params,
inverter_parameters=cec_inverter_parameters)
return system
@pytest.fixture
def pvsyst_dc_snl_ac_system(pvsyst_module_params, cec_inverter_parameters,
sapm_temperature_cs5p_220m):
module = 'PVsyst test module'
module_parameters = pvsyst_module_params
module_parameters['b'] = 0.05
temp_model_params = sapm_temperature_cs5p_220m.copy()
system = PVSystem(surface_tilt=32.2, surface_azimuth=180,
module=module,
module_parameters=module_parameters,
temperature_model_parameters=temp_model_params,
inverter_parameters=cec_inverter_parameters)
return system
@pytest.fixture
def pvsyst_dc_snl_ac_arrays(pvsyst_module_params, cec_inverter_parameters,
sapm_temperature_cs5p_220m):
module = 'PVsyst test module'
module_parameters = pvsyst_module_params
module_parameters['b'] = 0.05
temp_model_params = sapm_temperature_cs5p_220m.copy()
array_one = pvsystem.Array(
mount=pvsystem.FixedMount(surface_tilt=32.2, surface_azimuth=180),
module=module,
module_parameters=module_parameters.copy(),
temperature_model_parameters=temp_model_params.copy()
)
array_two = pvsystem.Array(
mount=pvsystem.FixedMount(surface_tilt=42.2, surface_azimuth=220),
module=module,
module_parameters=module_parameters.copy(),
temperature_model_parameters=temp_model_params.copy()
)
system = PVSystem(
arrays=[array_one, array_two],
inverter_parameters=cec_inverter_parameters
)
return system
@pytest.fixture
def cec_dc_adr_ac_system(sam_data, cec_module_cs5p_220m,
sapm_temperature_cs5p_220m):
module_parameters = cec_module_cs5p_220m.copy()
module_parameters['b'] = 0.05
module_parameters['EgRef'] = 1.121
module_parameters['dEgdT'] = -0.0002677
temp_model_params = sapm_temperature_cs5p_220m.copy()
inverters = sam_data['adrinverter']
inverter = inverters['Zigor__Sunzet_3_TL_US_240V__CEC_2011_'].copy()
system = PVSystem(surface_tilt=32.2, surface_azimuth=180,
module=module_parameters['Name'],
module_parameters=module_parameters,
temperature_model_parameters=temp_model_params,
inverter_parameters=inverter)
return system
@pytest.fixture
def pvwatts_dc_snl_ac_system(cec_inverter_parameters):
module_parameters = {'pdc0': 220, 'gamma_pdc': -0.003}
system = PVSystem(surface_tilt=32.2, surface_azimuth=180,
module_parameters=module_parameters,
inverter_parameters=cec_inverter_parameters)
return system
@pytest.fixture(scope="function")
def pvwatts_dc_pvwatts_ac_system(sapm_temperature_cs5p_220m):
module_parameters = {'pdc0': 220, 'gamma_pdc': -0.003}
temp_model_params = sapm_temperature_cs5p_220m.copy()
inverter_parameters = {'pdc0': 220, 'eta_inv_nom': 0.95}
system = PVSystem(surface_tilt=32.2, surface_azimuth=180,
module_parameters=module_parameters,
temperature_model_parameters=temp_model_params,
inverter_parameters=inverter_parameters)
return system
@pytest.fixture(scope="function")
def pvwatts_dc_pvwatts_ac_system_arrays(sapm_temperature_cs5p_220m):
module_parameters = {'pdc0': 220, 'gamma_pdc': -0.003}
temp_model_params = sapm_temperature_cs5p_220m.copy()
inverter_parameters = {'pdc0': 220, 'eta_inv_nom': 0.95}
array_one = pvsystem.Array(
mount=pvsystem.FixedMount(surface_tilt=32.2, surface_azimuth=180),
module_parameters=module_parameters.copy(),
temperature_model_parameters=temp_model_params.copy()
)
array_two = pvsystem.Array(
mount=pvsystem.FixedMount(surface_tilt=42.2, surface_azimuth=220),
module_parameters=module_parameters.copy(),
temperature_model_parameters=temp_model_params.copy()
)
system = PVSystem(
arrays=[array_one, array_two], inverter_parameters=inverter_parameters)
return system
@pytest.fixture(scope="function")
def pvwatts_dc_pvwatts_ac_faiman_temp_system():
module_parameters = {'pdc0': 220, 'gamma_pdc': -0.003}
temp_model_params = {'u0': 25.0, 'u1': 6.84}
inverter_parameters = {'pdc0': 220, 'eta_inv_nom': 0.95}
system = PVSystem(surface_tilt=32.2, surface_azimuth=180,
module_parameters=module_parameters,
temperature_model_parameters=temp_model_params,
inverter_parameters=inverter_parameters)
return system
@pytest.fixture(scope="function")
def pvwatts_dc_pvwatts_ac_pvsyst_temp_system():
module_parameters = {'pdc0': 220, 'gamma_pdc': -0.003}
temp_model_params = {'u_c': 29.0, 'u_v': 0.0, 'module_efficiency': 0.1,
'alpha_absorption': 0.9}
inverter_parameters = {'pdc0': 220, 'eta_inv_nom': 0.95}
system = PVSystem(surface_tilt=32.2, surface_azimuth=180,
module_parameters=module_parameters,
temperature_model_parameters=temp_model_params,
inverter_parameters=inverter_parameters)
return system
@pytest.fixture(scope="function")
def pvwatts_dc_pvwatts_ac_fuentes_temp_system():
module_parameters = {'pdc0': 220, 'gamma_pdc': -0.003}
temp_model_params = {'noct_installed': 45}
inverter_parameters = {'pdc0': 220, 'eta_inv_nom': 0.95}
system = PVSystem(surface_tilt=32.2, surface_azimuth=180,
module_parameters=module_parameters,
temperature_model_parameters=temp_model_params,
inverter_parameters=inverter_parameters)
return system
@pytest.fixture(scope="function")
def pvwatts_dc_pvwatts_ac_noct_sam_temp_system():
module_parameters = {'pdc0': 220, 'gamma_pdc': -0.003}
temp_model_params = {'noct': 45, 'module_efficiency': 0.2}
inverter_parameters = {'pdc0': 220, 'eta_inv_nom': 0.95}
system = PVSystem(surface_tilt=32.2, surface_azimuth=180,
module_parameters=module_parameters,
temperature_model_parameters=temp_model_params,
inverter_parameters=inverter_parameters)
return system
@pytest.fixture(scope="function")
def system_no_aoi(cec_module_cs5p_220m, sapm_temperature_cs5p_220m,
cec_inverter_parameters):
module_parameters = cec_module_cs5p_220m.copy()
module_parameters['EgRef'] = 1.121
module_parameters['dEgdT'] = -0.0002677
temp_model_params = sapm_temperature_cs5p_220m.copy()
inverter_parameters = cec_inverter_parameters.copy()
system = PVSystem(surface_tilt=32.2, surface_azimuth=180,
module_parameters=module_parameters,
temperature_model_parameters=temp_model_params,
inverter_parameters=inverter_parameters)
return system
@pytest.fixture
def system_no_temp(cec_module_cs5p_220m, cec_inverter_parameters):
module_parameters = cec_module_cs5p_220m.copy()
module_parameters['EgRef'] = 1.121
module_parameters['dEgdT'] = -0.0002677
inverter_parameters = cec_inverter_parameters.copy()
system = PVSystem(surface_tilt=32.2, surface_azimuth=180,
module_parameters=module_parameters,
inverter_parameters=inverter_parameters)
return system
@pytest.fixture
def location():
return Location(32.2, -111, altitude=700)
@pytest.fixture
def weather():
times = pd.date_range('20160101 1200-0700', periods=2, freq='6h')
weather = pd.DataFrame({'ghi': [500, 0], 'dni': [800, 0], 'dhi': [100, 0]},
index=times)
return weather
@pytest.fixture
def total_irrad(weather):
return pd.DataFrame({'poa_global': [800., 500.],
'poa_direct': [500., 300.],
'poa_diffuse': [300., 200.]}, index=weather.index)
@pytest.fixture(scope='function')
def sapm_dc_snl_ac_system_Array(sapm_module_params, cec_inverter_parameters,
sapm_temperature_cs5p_220m):
module = 'Canadian_Solar_CS5P_220M___2009_'
module_parameters = sapm_module_params.copy()
temp_model_params = sapm_temperature_cs5p_220m.copy()
array_one = pvsystem.Array(mount=pvsystem.FixedMount(surface_tilt=32,
surface_azimuth=180),
albedo=0.2, module=module,
module_parameters=module_parameters,
temperature_model_parameters=temp_model_params,
modules_per_string=1,
strings=1)
array_two = pvsystem.Array(mount=pvsystem.FixedMount(surface_tilt=15,
surface_azimuth=180),
albedo=0.2, module=module,
module_parameters=module_parameters,
temperature_model_parameters=temp_model_params,
modules_per_string=1,
strings=1)
return PVSystem(arrays=[array_one, array_two],
inverter_parameters=cec_inverter_parameters)
@pytest.fixture(scope='function')
def sapm_dc_snl_ac_system_same_arrays(sapm_module_params,
cec_inverter_parameters,
sapm_temperature_cs5p_220m):
"""A system with two identical arrays."""
module = 'Canadian_Solar_CS5P_220M___2009_'
module_parameters = sapm_module_params.copy()
temp_model_params = sapm_temperature_cs5p_220m.copy()
array_one = pvsystem.Array(mount=pvsystem.FixedMount(surface_tilt=32.2,
surface_azimuth=180),
module=module,
module_parameters=module_parameters,
temperature_model_parameters=temp_model_params,
modules_per_string=1,
strings=1)
array_two = pvsystem.Array(mount=pvsystem.FixedMount(surface_tilt=32.2,
surface_azimuth=180),
module=module,
module_parameters=module_parameters,
temperature_model_parameters=temp_model_params,
modules_per_string=1,
strings=1)
return PVSystem(arrays=[array_one, array_two],
inverter_parameters=cec_inverter_parameters)
def test_ModelChain_creation(sapm_dc_snl_ac_system, location):
ModelChain(sapm_dc_snl_ac_system, location)
def test_with_sapm(sapm_dc_snl_ac_system, location, weather):
mc = ModelChain.with_sapm(sapm_dc_snl_ac_system, location)
assert mc.dc_model == mc.sapm
mc.run_model(weather)
def test_with_pvwatts(pvwatts_dc_pvwatts_ac_system, location, weather):
mc = ModelChain.with_pvwatts(pvwatts_dc_pvwatts_ac_system, location)
assert mc.dc_model == mc.pvwatts_dc
assert mc.temperature_model == mc.sapm_temp
mc.run_model(weather)
def test_run_model_with_irradiance(sapm_dc_snl_ac_system, location):
mc = ModelChain(sapm_dc_snl_ac_system, location)
times = pd.date_range('20160101 1200-0700', periods=2, freq='6h')
irradiance = pd.DataFrame({'dni': 900, 'ghi': 600, 'dhi': 150},
index=times)
ac = mc.run_model(irradiance).results.ac
expected = pd.Series(np.array([187.80746494643176, -0.02]),
index=times)
assert_series_equal(ac, expected)
@pytest.fixture(scope='function')
def multi_array_sapm_dc_snl_ac_system(
sapm_temperature_cs5p_220m, sapm_module_params,
cec_inverter_parameters):
module_parameters = sapm_module_params
temp_model_parameters = sapm_temperature_cs5p_220m.copy()
inverter_parameters = cec_inverter_parameters
array_one = pvsystem.Array(
mount=pvsystem.FixedMount(surface_tilt=32.2, surface_azimuth=180),
module_parameters=module_parameters,
temperature_model_parameters=temp_model_parameters
)
array_two = pvsystem.Array(
mount=pvsystem.FixedMount(surface_tilt=32.2, surface_azimuth=220),
module_parameters=module_parameters,
temperature_model_parameters=temp_model_parameters
)
two_array_system = PVSystem(
arrays=[array_one, array_two],
inverter_parameters=inverter_parameters
)
array_one_system = PVSystem(
arrays=[array_one],
inverter_parameters=inverter_parameters
)
array_two_system = PVSystem(
arrays=[array_two],
inverter_parameters=inverter_parameters
)
return {'two_array_system': two_array_system,
'array_one_system': array_one_system,
'array_two_system': array_two_system}
def test_run_model_from_irradiance_arrays_no_loss(
multi_array_sapm_dc_snl_ac_system, location):
mc_both = ModelChain(
multi_array_sapm_dc_snl_ac_system['two_array_system'],
location,
aoi_model='no_loss',
spectral_model='no_loss',
losses_model='no_loss'
)
mc_one = ModelChain(
multi_array_sapm_dc_snl_ac_system['array_one_system'],
location,
aoi_model='no_loss',
spectral_model='no_loss',
losses_model='no_loss'
)
mc_two = ModelChain(
multi_array_sapm_dc_snl_ac_system['array_two_system'],
location,
aoi_model='no_loss',
spectral_model='no_loss',
losses_model='no_loss'
)
times = pd.date_range('20160101 1200-0700', periods=2, freq='6h')
irradiance = pd.DataFrame({'dni': 900, 'ghi': 600, 'dhi': 150},
index=times)
mc_one.run_model(irradiance)
mc_two.run_model(irradiance)
mc_both.run_model(irradiance)
assert_frame_equal(
mc_both.results.dc[0],
mc_one.results.dc
)
assert_frame_equal(
mc_both.results.dc[1],
mc_two.results.dc
)
@pytest.mark.parametrize("input_type", [tuple, list])
def test_run_model_from_irradiance_arrays_no_loss_input_type(
multi_array_sapm_dc_snl_ac_system, location, input_type):
mc_both = ModelChain(
multi_array_sapm_dc_snl_ac_system['two_array_system'],
location,
aoi_model='no_loss',
spectral_model='no_loss',
losses_model='no_loss'
)
mc_one = ModelChain(
multi_array_sapm_dc_snl_ac_system['array_one_system'],
location,
aoi_model='no_loss',
spectral_model='no_loss',
losses_model='no_loss'
)
mc_two = ModelChain(
multi_array_sapm_dc_snl_ac_system['array_two_system'],
location,
aoi_model='no_loss',
spectral_model='no_loss',
losses_model='no_loss'
)
times = pd.date_range('20160101 1200-0700', periods=2, freq='6h')
irradiance = pd.DataFrame({'dni': 900, 'ghi': 600, 'dhi': 150},
index=times)
mc_one.run_model(irradiance)
mc_two.run_model(irradiance)
mc_both.run_model(input_type((irradiance, irradiance)))
assert_frame_equal(
mc_both.results.dc[0], mc_one.results.dc
)
assert_frame_equal(
mc_both.results.dc[1], mc_two.results.dc
)
@pytest.mark.parametrize('inverter', ['adr'])
def test_ModelChain_invalid_inverter_params_arrays(
inverter, sapm_dc_snl_ac_system_same_arrays,
location, adr_inverter_parameters):
inverter_params = {'adr': adr_inverter_parameters}
sapm_dc_snl_ac_system_same_arrays.inverter_parameters = \
inverter_params[inverter]
with pytest.raises(ValueError,
match=r'adr inverter function cannot'):
ModelChain(sapm_dc_snl_ac_system_same_arrays, location)
@pytest.mark.parametrize("input_type", [tuple, list])
def test_prepare_inputs_multi_weather(
sapm_dc_snl_ac_system_Array, location, input_type):
times = pd.date_range(start='20160101 1200-0700',
end='20160101 1800-0700', freq='6h')
mc = ModelChain(sapm_dc_snl_ac_system_Array, location)
weather = pd.DataFrame({'ghi': 1, 'dhi': 1, 'dni': 1},
index=times)
mc.prepare_inputs(input_type((weather, weather)))
num_arrays = sapm_dc_snl_ac_system_Array.num_arrays
assert len(mc.results.total_irrad) == num_arrays
# check that albedo is transfered to mc.results from mc.system.arrays
assert mc.results.albedo == (0.2, 0.2)
@pytest.mark.parametrize("input_type", [tuple, list])
def test_prepare_inputs_albedo_in_weather(
sapm_dc_snl_ac_system_Array, location, input_type):
times = pd.date_range(start='20160101 1200-0700',
end='20160101 1800-0700', freq='6h')
mc = ModelChain(sapm_dc_snl_ac_system_Array, location)
weather = pd.DataFrame({'ghi': 1, 'dhi': 1, 'dni': 1, 'albedo': 0.5},
index=times)
# weather as a single DataFrame
mc.prepare_inputs(weather)
num_arrays = sapm_dc_snl_ac_system_Array.num_arrays
assert len(mc.results.albedo) == num_arrays
# repeat with tuple of weather
mc.prepare_inputs(input_type((weather, weather)))
num_arrays = sapm_dc_snl_ac_system_Array.num_arrays
assert len(mc.results.albedo) == num_arrays
def test_prepare_inputs_no_irradiance(sapm_dc_snl_ac_system, location):
mc = ModelChain(sapm_dc_snl_ac_system, location)
weather = pd.DataFrame()
with pytest.raises(ValueError):
mc.prepare_inputs(weather)
def test_prepare_inputs_arrays_one_missing_irradiance(
sapm_dc_snl_ac_system_Array, location):
"""If any of the input DataFrames is missing a column then a
ValueError is raised."""
mc = ModelChain(sapm_dc_snl_ac_system_Array, location)
weather = pd.DataFrame(
{'ghi': [1], 'dhi': [1], 'dni': [1]}
)
weather_incomplete = pd.DataFrame(
{'ghi': [1], 'dhi': [1]}
)
with pytest.raises(ValueError,
match=r"Incomplete input data\. .*"):
mc.prepare_inputs((weather, weather_incomplete))
with pytest.raises(ValueError,
match=r"Incomplete input data\. .*"):
mc.prepare_inputs((weather_incomplete, weather))
@pytest.mark.parametrize("input_type", [tuple, list])
def test_prepare_inputs_weather_wrong_length(
sapm_dc_snl_ac_system_Array, location, input_type):
mc = ModelChain(sapm_dc_snl_ac_system_Array, location)
weather = pd.DataFrame({'ghi': [1], 'dhi': [1], 'dni': [1]})
with pytest.raises(ValueError,
match="Input must be same length as number of Arrays "
r"in system\. Expected 2, got 1\."):
mc.prepare_inputs(input_type((weather,)))
with pytest.raises(ValueError,
match="Input must be same length as number of Arrays "
r"in system\. Expected 2, got 3\."):
mc.prepare_inputs(input_type((weather, weather, weather)))
def test_ModelChain_times_error_arrays(sapm_dc_snl_ac_system_Array, location):
"""ModelChain.times is assigned a single index given multiple weather
DataFrames.
"""
error_str = r"Input DataFrames must have same index\."
mc = ModelChain(sapm_dc_snl_ac_system_Array, location)
irradiance = {'ghi': [1, 2], 'dhi': [1, 2], 'dni': [1, 2]}
times_one = pd.date_range(start='1/1/2020', freq='6h', periods=2)
times_two = pd.date_range(start='1/1/2020 00:15', freq='6h', periods=2)
weather_one = pd.DataFrame(irradiance, index=times_one)
weather_two = pd.DataFrame(irradiance, index=times_two)
with pytest.raises(ValueError, match=error_str):
mc.prepare_inputs((weather_one, weather_two))
# test with overlapping, but differently sized indices.
times_three = pd.date_range(start='1/1/2020', freq='6h', periods=3)
irradiance_three = irradiance
irradiance_three['ghi'].append(3)
irradiance_three['dhi'].append(3)
irradiance_three['dni'].append(3)
weather_three = pd.DataFrame(irradiance_three, index=times_three)
with pytest.raises(ValueError, match=error_str):
mc.prepare_inputs((weather_one, weather_three))
def test_ModelChain_times_arrays(sapm_dc_snl_ac_system_Array, location):
"""ModelChain.times is assigned a single index given multiple weather
DataFrames.
"""
mc = ModelChain(sapm_dc_snl_ac_system_Array, location)
irradiance_one = {'ghi': [1, 2], 'dhi': [1, 2], 'dni': [1, 2]}
irradiance_two = {'ghi': [2, 1], 'dhi': [2, 1], 'dni': [2, 1]}
times = pd.date_range(start='1/1/2020', freq='6h', periods=2)
weather_one = pd.DataFrame(irradiance_one, index=times)
weather_two = pd.DataFrame(irradiance_two, index=times)
mc.prepare_inputs((weather_one, weather_two))
assert mc.results.times.equals(times)
mc = ModelChain(sapm_dc_snl_ac_system_Array, location)
mc.prepare_inputs(weather_one)
assert mc.results.times.equals(times)
@pytest.mark.parametrize("missing", ['dhi', 'ghi', 'dni'])
def test_prepare_inputs_missing_irrad_component(
sapm_dc_snl_ac_system, location, missing):
mc = ModelChain(sapm_dc_snl_ac_system, location)
weather = pd.DataFrame({'dhi': [1, 2], 'dni': [1, 2], 'ghi': [1, 2]})
weather.drop(columns=missing, inplace=True)
with pytest.raises(ValueError):
mc.prepare_inputs(weather)
@pytest.mark.parametrize('ac_model', ['sandia', 'pvwatts'])
@pytest.mark.parametrize("input_type", [tuple, list])
def test_run_model_arrays_weather(sapm_dc_snl_ac_system_same_arrays,
pvwatts_dc_pvwatts_ac_system_arrays,
location, ac_model, input_type):
system = {'sandia': sapm_dc_snl_ac_system_same_arrays,
'pvwatts': pvwatts_dc_pvwatts_ac_system_arrays}
mc = ModelChain(system[ac_model], location, aoi_model='no_loss',
spectral_model='no_loss')
times = pd.date_range('20200101 1200-0700', periods=2, freq='2h')
weather_one = pd.DataFrame({'dni': [900, 800],
'ghi': [600, 500],
'dhi': [150, 100]},
index=times)
weather_two = pd.DataFrame({'dni': [500, 400],
'ghi': [300, 200],
'dhi': [75, 65]},
index=times)
mc.run_model(input_type((weather_one, weather_two)))
assert (mc.results.dc[0] != mc.results.dc[1]).all().all()
assert not mc.results.ac.empty
def test_run_model_perez(sapm_dc_snl_ac_system, location):
mc = ModelChain(sapm_dc_snl_ac_system, location,
transposition_model='perez')
times = pd.date_range('20160101 1200-0700', periods=2, freq='6h')
irradiance = pd.DataFrame({'dni': 900, 'ghi': 600, 'dhi': 150},
index=times)
ac = mc.run_model(irradiance).results.ac
expected = pd.Series(np.array([187.94295642, -2.00000000e-02]),
index=times)
assert_series_equal(ac, expected)
def test_run_model_gueymard_perez(sapm_dc_snl_ac_system, location):
mc = ModelChain(sapm_dc_snl_ac_system, location,
airmass_model='gueymard1993',
transposition_model='perez')
times = pd.date_range('20160101 1200-0700', periods=2, freq='6h')
irradiance = pd.DataFrame({'dni': 900, 'ghi': 600, 'dhi': 150},
index=times)
ac = mc.run_model(irradiance).results.ac
expected = pd.Series(np.array([187.94317405, -2.00000000e-02]),
index=times)
assert_series_equal(ac, expected)
def test_run_model_with_weather_sapm_temp(sapm_dc_snl_ac_system, location,
weather, mocker):
# test with sapm cell temperature model
weather['wind_speed'] = 5
weather['temp_air'] = 10
mc = ModelChain(sapm_dc_snl_ac_system, location)
mc.temperature_model = 'sapm'
m_sapm = mocker.spy(sapm_dc_snl_ac_system, 'get_cell_temperature')
mc.run_model(weather)
assert m_sapm.call_count == 1
# assert_called_once_with cannot be used with series, so need to use
# assert_series_equal on call_args
assert_series_equal(m_sapm.call_args[0][1], weather['temp_air']) # temp
assert_series_equal(m_sapm.call_args[0][2], weather['wind_speed']) # wind
assert m_sapm.call_args[1]['model'] == 'sapm'
assert not mc.results.ac.empty
def test_run_model_with_weather_pvsyst_temp(sapm_dc_snl_ac_system, location,
weather, mocker):
# test with pvsyst cell temperature model
weather['wind_speed'] = 5
weather['temp_air'] = 10
sapm_dc_snl_ac_system.arrays[0].racking_model = 'freestanding'
sapm_dc_snl_ac_system.arrays[0].temperature_model_parameters = \
temperature._temperature_model_params('pvsyst', 'freestanding')
mc = ModelChain(sapm_dc_snl_ac_system, location)
mc.temperature_model = 'pvsyst'
m_pvsyst = mocker.spy(sapm_dc_snl_ac_system, 'get_cell_temperature')
mc.run_model(weather)
assert m_pvsyst.call_count == 1
assert_series_equal(m_pvsyst.call_args[0][1], weather['temp_air'])
assert_series_equal(m_pvsyst.call_args[0][2], weather['wind_speed'])
assert m_pvsyst.call_args[1]['model'] == 'pvsyst'
assert not mc.results.ac.empty
def test_run_model_with_weather_faiman_temp(sapm_dc_snl_ac_system, location,
weather, mocker):
# test with faiman cell temperature model
weather['wind_speed'] = 5
weather['temp_air'] = 10
sapm_dc_snl_ac_system.arrays[0].temperature_model_parameters = {
'u0': 25.0, 'u1': 6.84
}
mc = ModelChain(sapm_dc_snl_ac_system, location)
mc.temperature_model = 'faiman'
m_faiman = mocker.spy(sapm_dc_snl_ac_system, 'get_cell_temperature')
mc.run_model(weather)
assert m_faiman.call_count == 1
assert_series_equal(m_faiman.call_args[0][1], weather['temp_air'])
assert_series_equal(m_faiman.call_args[0][2], weather['wind_speed'])
assert m_faiman.call_args[1]['model'] == 'faiman'
assert not mc.results.ac.empty
def test_run_model_with_weather_fuentes_temp(sapm_dc_snl_ac_system, location,
weather, mocker):
weather['wind_speed'] = 5
weather['temp_air'] = 10
sapm_dc_snl_ac_system.arrays[0].temperature_model_parameters = {
'noct_installed': 45, 'surface_tilt': 30,
}
mc = ModelChain(sapm_dc_snl_ac_system, location)
mc.temperature_model = 'fuentes'
m_fuentes = mocker.spy(sapm_dc_snl_ac_system, 'get_cell_temperature')
mc.run_model(weather)
assert m_fuentes.call_count == 1
assert_series_equal(m_fuentes.call_args[0][1], weather['temp_air'])
assert_series_equal(m_fuentes.call_args[0][2], weather['wind_speed'])
assert m_fuentes.call_args[1]['model'] == 'fuentes'
assert not mc.results.ac.empty
def test_run_model_with_weather_noct_sam_temp(sapm_dc_snl_ac_system, location,
weather, mocker):
weather['wind_speed'] = 5
weather['temp_air'] = 10
sapm_dc_snl_ac_system.arrays[0].temperature_model_parameters = {
'noct': 45, 'module_efficiency': 0.2
}
mc = ModelChain(sapm_dc_snl_ac_system, location)
mc.temperature_model = 'noct_sam'
m_noct_sam = mocker.spy(sapm_dc_snl_ac_system, 'get_cell_temperature')
mc.run_model(weather)
assert m_noct_sam.call_count == 1
assert_series_equal(m_noct_sam.call_args[0][1], weather['temp_air'])
assert_series_equal(m_noct_sam.call_args[0][2], weather['wind_speed'])
# check that effective_irradiance was used
assert m_noct_sam.call_args[1] == {
'effective_irradiance': mc.results.effective_irradiance,
'model': 'noct_sam'}
def test__assign_total_irrad(sapm_dc_snl_ac_system, location, weather,
total_irrad):
data = pd.concat([weather, total_irrad], axis=1)
mc = ModelChain(sapm_dc_snl_ac_system, location)
mc._assign_total_irrad(data)
assert_frame_equal(mc.results.total_irrad, total_irrad)
def test_prepare_inputs_from_poa(sapm_dc_snl_ac_system, location,
weather, total_irrad):
data = pd.concat([weather, total_irrad], axis=1)
mc = ModelChain(sapm_dc_snl_ac_system, location)
mc.prepare_inputs_from_poa(data)
weather_expected = weather.copy()
weather_expected['temp_air'] = 20
weather_expected['wind_speed'] = 0
# order as expected
weather_expected = weather_expected[
['ghi', 'dhi', 'dni', 'wind_speed', 'temp_air']]
# weather attribute
assert_frame_equal(mc.results.weather, weather_expected)
# total_irrad attribute
assert_frame_equal(mc.results.total_irrad, total_irrad)
assert not pd.isnull(mc.results.solar_position.index[0])
@pytest.mark.parametrize("input_type", [tuple, list])
def test_prepare_inputs_from_poa_multi_data(
sapm_dc_snl_ac_system_Array, location, total_irrad, weather,
input_type):
mc = ModelChain(sapm_dc_snl_ac_system_Array, location)
poa = pd.concat([weather, total_irrad], axis=1)
mc.prepare_inputs_from_poa(input_type((poa, poa)))
num_arrays = sapm_dc_snl_ac_system_Array.num_arrays
assert len(mc.results.total_irrad) == num_arrays
@pytest.mark.parametrize("input_type", [tuple, list])
def test_prepare_inputs_from_poa_wrong_number_arrays(
sapm_dc_snl_ac_system_Array, location, total_irrad, weather,
input_type):
len_error = r"Input must be same length as number of Arrays in system\. " \
r"Expected 2, got [0-9]+\."
type_error = r"Input must be a tuple of length 2, got .*\."
mc = ModelChain(sapm_dc_snl_ac_system_Array, location)
poa = pd.concat([weather, total_irrad], axis=1)
with pytest.raises(TypeError, match=type_error):
mc.prepare_inputs_from_poa(poa)
with pytest.raises(ValueError, match=len_error):
mc.prepare_inputs_from_poa(input_type((poa,)))
with pytest.raises(ValueError, match=len_error):
mc.prepare_inputs_from_poa(input_type((poa, poa, poa)))
def test_prepare_inputs_from_poa_arrays_different_indices(
sapm_dc_snl_ac_system_Array, location, total_irrad, weather):
error_str = r"Input DataFrames must have same index\."
mc = ModelChain(sapm_dc_snl_ac_system_Array, location)
poa = pd.concat([weather, total_irrad], axis=1)
with pytest.raises(ValueError, match=error_str):
mc.prepare_inputs_from_poa((poa, poa.shift(periods=1, freq='6h')))
def test_prepare_inputs_from_poa_arrays_missing_column(
sapm_dc_snl_ac_system_Array, location, weather, total_irrad):
mc = ModelChain(sapm_dc_snl_ac_system_Array, location)
poa = pd.concat([weather, total_irrad], axis=1)
with pytest.raises(ValueError, match=r"Incomplete input data\. "
r"Data needs to contain .*\. "
r"Detected data in element 1 "
r"contains: .*"):
mc.prepare_inputs_from_poa((poa, poa.drop(columns='poa_global')))
def test__prepare_temperature(sapm_dc_snl_ac_system, location, weather,
total_irrad):
data = weather.copy()
data[['poa_global', 'poa_diffuse', 'poa_direct']] = total_irrad
mc = ModelChain(sapm_dc_snl_ac_system, location, aoi_model='no_loss',
spectral_model='no_loss')
# prepare_temperature expects mc.total_irrad and mc.results.weather
# to be set
mc._assign_weather(data)
mc._assign_total_irrad(data)
mc._prepare_temperature(data)
expected = pd.Series([48.928025, 38.080016], index=data.index)
assert_series_equal(mc.results.cell_temperature, expected)
data['module_temperature'] = [40., 30.]
mc._prepare_temperature(data)
expected = pd.Series([42.4, 31.5], index=data.index)
assert_series_equal(mc.results.cell_temperature, expected)
data['cell_temperature'] = [50., 35.]
mc._prepare_temperature(data)
assert_series_equal(mc.results.cell_temperature, data['cell_temperature'])
def test__prepare_temperature_len1_weather_tuple(
sapm_dc_snl_ac_system, location, weather, total_irrad):
# GH 1192
weather['module_temperature'] = [40., 30.]
data = weather.copy()
mc = ModelChain(sapm_dc_snl_ac_system, location, aoi_model='no_loss',
spectral_model='no_loss')
mc.run_model([data])
expected = pd.Series([42.617244212941394, 30.0], index=data.index)
assert_series_equal(mc.results.cell_temperature[0], expected)
data = weather.copy().rename(
columns={
"ghi": "poa_global", "dhi": "poa_diffuse", "dni": "poa_direct"}
)
mc = ModelChain(sapm_dc_snl_ac_system, location, aoi_model='no_loss',
spectral_model='no_loss')
mc.run_model_from_poa([data])
expected = pd.Series([41.5, 30.0], index=data.index)
assert_series_equal(mc.results.cell_temperature[0], expected)
data = weather.copy()[["module_temperature", "ghi"]].rename(
columns={"ghi": "effective_irradiance"}
)
mc = ModelChain(sapm_dc_snl_ac_system, location, aoi_model='no_loss',
spectral_model='no_loss')
mc.run_model_from_effective_irradiance([data])
expected = pd.Series([41.5, 30.0], index=data.index)
assert_series_equal(mc.results.cell_temperature[0], expected)
def test__prepare_temperature_arrays_weather(sapm_dc_snl_ac_system_same_arrays,
location, weather,
total_irrad):
data = weather.copy()
data[['poa_global', 'poa_direct', 'poa_diffuse']] = total_irrad
data_two = data.copy()
mc = ModelChain(sapm_dc_snl_ac_system_same_arrays, location,
aoi_model='no_loss', spectral_model='no_loss')
# prepare_temperature expects mc.results.total_irrad and mc.results.weather
# to be set
mc._assign_weather((data, data_two))
mc._assign_total_irrad((data, data_two))
mc._prepare_temperature((data, data_two))
expected = pd.Series([48.928025, 38.080016], index=data.index)
assert_series_equal(mc.results.cell_temperature[0], expected)
assert_series_equal(mc.results.cell_temperature[1], expected)
data['module_temperature'] = [40., 30.]
mc._prepare_temperature((data, data_two))
expected = pd.Series([42.4, 31.5], index=data.index)
assert (mc.results.cell_temperature[1] != expected).all()
assert_series_equal(mc.results.cell_temperature[0], expected)
data['cell_temperature'] = [50., 35.]
mc._prepare_temperature((data, data_two))
assert_series_equal(
mc.results.cell_temperature[0], data['cell_temperature'])
data_two['module_temperature'] = [40., 30.]
mc._prepare_temperature((data, data_two))
assert_series_equal(mc.results.cell_temperature[1], expected)
assert_series_equal(
mc.results.cell_temperature[0], data['cell_temperature'])
data_two['cell_temperature'] = [10.0, 20.0]
mc._prepare_temperature((data, data_two))
assert_series_equal(
mc.results.cell_temperature[1], data_two['cell_temperature'])
assert_series_equal(
mc.results.cell_temperature[0], data['cell_temperature'])
@pytest.mark.parametrize('temp_params,temp_model',
[({'a': -3.47, 'b': -.0594, 'deltaT': 3},
ModelChain.sapm_temp),
({'u_c': 29.0, 'u_v': 0},
ModelChain.pvsyst_temp),
({'u0': 25.0, 'u1': 6.84},
ModelChain.faiman_temp),
({'noct_installed': 45},
ModelChain.fuentes_temp),
({'noct': 45, 'module_efficiency': 0.2},
ModelChain.noct_sam_temp)])
def test_temperature_models_arrays_multi_weather(
temp_params, temp_model,
sapm_dc_snl_ac_system_same_arrays,
location, weather, total_irrad):
for array in sapm_dc_snl_ac_system_same_arrays.arrays:
array.temperature_model_parameters = temp_params
# set air temp so it does not default to the same value for both arrays
weather['temp_air'] = 25
weather_one = weather
weather_two = weather.copy() * 0.5
mc = ModelChain(sapm_dc_snl_ac_system_same_arrays, location,
aoi_model='no_loss', spectral_model='no_loss')
mc.prepare_inputs((weather_one, weather_two))
temp_model(mc)
assert (mc.results.cell_temperature[0]
!= mc.results.cell_temperature[1]).all()
def test_run_model_solar_position_weather(
pvwatts_dc_pvwatts_ac_system, location, weather, mocker):
mc = ModelChain(pvwatts_dc_pvwatts_ac_system, location,
aoi_model='no_loss', spectral_model='no_loss')
weather['pressure'] = 90000
weather['temp_air'] = 25
m = mocker.spy(location, 'get_solarposition')
mc.run_model(weather)
# assert_called_once_with cannot be used with series, so need to use
# assert_series_equal on call_args
assert_series_equal(m.call_args[1]['temperature'], weather['temp_air'])
assert_series_equal(m.call_args[1]['pressure'], weather['pressure'])
def test_run_model_from_poa(sapm_dc_snl_ac_system, location, total_irrad):
mc = ModelChain(sapm_dc_snl_ac_system, location, aoi_model='no_loss',
spectral_model='no_loss')
ac = mc.run_model_from_poa(total_irrad).results.ac
expected = pd.Series(np.array([149.280238, 96.678385]),
index=total_irrad.index)
assert_series_equal(ac, expected)
@pytest.mark.parametrize("input_type", [tuple, list])
def test_run_model_from_poa_arrays(sapm_dc_snl_ac_system_Array, location,
weather, total_irrad, input_type):
data = weather.copy()
data[['poa_global', 'poa_diffuse', 'poa_direct']] = total_irrad
mc = ModelChain(sapm_dc_snl_ac_system_Array, location, aoi_model='no_loss',
spectral_model='no_loss')
mc.run_model_from_poa(input_type((data, data)))
# arrays have different orientation, but should give same dc power
# because we are the same passing POA irradiance and air
# temperature.
assert_frame_equal(mc.results.dc[0], mc.results.dc[1])
def test_run_model_from_poa_arrays_solar_position_weather(
sapm_dc_snl_ac_system_Array, location, weather, total_irrad, mocker):
data = weather.copy()
data[['poa_global', 'poa_diffuse', 'poa_direct']] = total_irrad
data['pressure'] = 90000
data['temp_air'] = 25
data2 = data.copy()
data2['pressure'] = 95000
data2['temp_air'] = 30
mc = ModelChain(sapm_dc_snl_ac_system_Array, location, aoi_model='no_loss',
spectral_model='no_loss')
m = mocker.spy(location, 'get_solarposition')
mc.run_model_from_poa((data, data2))
# mc uses only the first weather data for solar position corrections