Last active
December 16, 2022 16:08
-
-
Save kandersolar/280c9331d6b469945f68d4f56eb7a399 to your computer and use it in GitHub Desktop.
Hacky way of using pre-calculated solar position/airmass values with ModelChain.run_model()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "4e405d36-78dd-4ecf-92e5-4d7f7043e3fb", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import pvlib\n", | |
"import pandas as pd\n", | |
"\n", | |
"location = pvlib.location.Location(40, -80)\n", | |
"kwargs = dict(\n", | |
" surface_tilt=20,\n", | |
" surface_azimuth=180,\n", | |
" module_parameters={'pdc0': 1000, 'gamma_pdc': -0.004},\n", | |
" inverter_parameters={'pdc0': 1000},\n", | |
" temperature_model_parameters=pvlib.temperature.TEMPERATURE_MODEL_PARAMETERS['sapm']['open_rack_glass_polymer'],\n", | |
")\n", | |
"system = pvlib.pvsystem.PVSystem(**kwargs)\n", | |
"\n", | |
"times = pd.date_range('2019-01-01', '2020-01-01', freq='15T', tz='Etc/GMT+5')\n", | |
"weather = location.get_clearsky(times)\n", | |
"weather['temp_air'] = 25\n", | |
"weather['wind_speed'] = 1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "de800335-331d-49ea-9b37-0758caab63fd", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# standard approach\n", | |
"mc1 = pvlib.modelchain.ModelChain.with_pvwatts(system, location)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "bb3cd803-8db6-451b-b734-f7d381c76e04", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# hacky workaround to use pre-calculated airmass and solar position\n", | |
"class HackedLocation:\n", | |
" def __init__(self, sp, am):\n", | |
" self.sp = sp\n", | |
" self.am = am\n", | |
" def get_solarposition(self, *args, **kwargs):\n", | |
" return self.sp\n", | |
" def get_airmass(self, *args, **kwargs):\n", | |
" return self.am\n", | |
"\n", | |
"sp = location.get_solarposition(times, temperature=25)\n", | |
"am = location.get_airmass(times, sp)\n", | |
"hacked_location = HackedLocation(sp, am)\n", | |
"mc2 = pvlib.modelchain.ModelChain.with_pvwatts(system, hacked_location)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "e4b9645f-4907-4cfe-8cc2-90f20ebe84cf", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: total: 266 ms\n", | |
"Wall time: 257 ms\n" | |
] | |
} | |
], | |
"source": [ | |
"%time _ = mc1.run_model(weather)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "2535658f-56c7-4a0a-96eb-d585ff3a9e84", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: total: 31.2 ms\n", | |
"Wall time: 37.9 ms\n" | |
] | |
} | |
], | |
"source": [ | |
"%time _ = mc2.run_model(weather)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "213192e4-8bfd-43cd-94e5-1b684106230f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"mc1.results.ac.sum() == mc2.results.ac.sum()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "b364c654-dcd6-4814-b474-5060e9fa0dc2", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.9.12" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment