PySAM风力涡轮机模拟报错:嵌套元组转矩阵行失败求助
PySAM风力涡轮机模拟报错:Error (-4) converting nested tuple 0 into row in matrix 解决方法
问题描述
运行PySAM风力涡轮机模拟代码时触发报错:
Exception: Error (-4) converting nested tuple 0 into row in matrix
报错位置在wind_turbine.Resource.wind_resource_data赋值语句处。
问题原因
PySAM对wind_resource_data的结构有严格要求:每个字段(如wind_speed、perc)的格式需要是外层列表对应高度维度,内层列表包含该高度下的所有数据点。当前代码中生成的[[float(key)] for key in data_dict]会把每个数据点单独放在一个子列表里,形成[[v1], [v2], ...]的结构,不符合PySAM预期的[[v1, v2, ...]]格式,导致矩阵转换失败。
解决方法
修改wind_resource_data中各字段的生成方式,将同一高度的所有数据合并到一个内层列表中,外层保留一个列表对应高度[0]:
import PySAM.Windpower as wp import csv # Create a Windpower object wind_turbine = wp.default("WindPowerSingleOwner") # Set the turbine parameters wind_turbine.system_capacity = 200 # Turbine capacity in kW wind_turbine.max_cp = 0.45 # Maximum power coefficient wind_turbine.max_tip_speed = 80 # Maximum tip speed in m/s wind_turbine.cut_in_speed = 3 # Cut-in wind speed in m/s wind_turbine.cut_out_speed = 25 # Cut-out wind speed in m/s def csv_to_dict(file_path): result_dict = {} with open(file_path, 'r') as csvfile: reader = csv.DictReader(csvfile) for row in reader: val = float(row['val']) perc = float(row['perc']) sel_perc = float(row['sel_perc']) result_dict[val] = (perc, sel_perc) return result_dict # Provide the file path to your CSV file csv_file_path = 'C:/Users/arthu/OneDrive/Documents/INTERNSHIP/SAM/05-07-2023/windSpeed.csv' # Convert the CSV file to a dictionary data_dict = csv_to_dict(csv_file_path) print(data_dict) # Set the wind resource data - 修改后的结构 wind_turbine.Resource.wind_resource_data = { 'fields': ['wind_speed', 'perc', 'sel_perc'], 'heights': [0], # 将所有数据合并到一个内层列表中,外层用列表包裹对应高度 'wind_speed': [[float(key) for key in data_dict]], 'perc': [[data_dict[key][0] for key in data_dict]], 'sel_perc': [[data_dict[key][1] for key in data_dict]] } # Run the simulation wind_turbine.execute() # Access the wind turbine outputs annual_energy = wind_turbine.Outputs.annual_energy # Print the annual energy production print("Annual Energy Production:", annual_energy, "kWh")
额外说明
- 确保CSV中的数据是按合理顺序排列的(比如风速从小到大),避免模拟结果出现异常
- 如果有多个高度的风资源数据,只需在
heights中添加对应高度值,并为每个字段添加对应高度的数据列表,例如'wind_speed': [[height0_data], [height1_data]]
内容的提问来源于stack exchange,提问作者Arthur Martineau
相关产品推荐
相关产品推荐

