PhotovoltaicEconomicOperationTest.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import com.alibaba.excel.EasyExcel;
  2. import com.alibaba.excel.context.AnalysisContext;
  3. import com.alibaba.excel.event.AnalysisEventListener;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import com.gyee.power.fitting.PowerBootApplication;
  6. import com.gyee.power.fitting.common.alg.PolynomialCurveFitting;
  7. import com.gyee.power.fitting.common.feign.IAdapterService;
  8. import com.gyee.power.fitting.common.util.DateUtils;
  9. import com.gyee.power.fitting.model.Photovoltaiccapacity;
  10. import com.gyee.power.fitting.model.Photovoltaiciv;
  11. import com.gyee.power.fitting.model.Windpowerstationtestingpoint2;
  12. import com.gyee.power.fitting.model.custom.TsDoubleData;
  13. import com.gyee.power.fitting.service.IPhotovoltaiccapacityService;
  14. import com.gyee.power.fitting.service.IPhotovoltaicivService;
  15. import com.gyee.power.fitting.service.IWindpowerstationtestingpoint2Service;
  16. import com.gyee.power.fitting.service.Windturbinetestingpointai2Service;
  17. import org.apache.commons.math3.fitting.WeightedObservedPoint;
  18. import org.apache.commons.math3.fitting.WeightedObservedPoints;
  19. import org.junit.jupiter.api.Test;
  20. import org.springframework.boot.test.context.SpringBootTest;
  21. import javax.annotation.Resource;
  22. import java.io.File;
  23. import java.io.FileOutputStream;
  24. import java.io.PrintWriter;
  25. import java.util.*;
  26. import java.util.stream.Collectors;
  27. /**
  28. * 光伏经济运行
  29. */
  30. @SpringBootTest(classes = PowerBootApplication.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  31. public class PhotovoltaicEconomicOperationTest {
  32. @Resource
  33. private Windturbinetestingpointai2Service windturbinetestingpointai2Service;
  34. @Resource
  35. private IAdapterService adpClient;
  36. @Resource
  37. private IPhotovoltaicivService photovoltaicivService;
  38. @Resource
  39. private IWindpowerstationtestingpoint2Service windpowerstationtestingpoint2Service;
  40. @Resource
  41. private IPhotovoltaiccapacityService photovoltaiccapacityService;
  42. @Test()
  43. public void test() throws Exception {
  44. PolynomialCurveFitting pncf = new PolynomialCurveFitting();
  45. //HZJGGL.NX_GD_HZJG_YC_P1_L1_001_QXZ006
  46. List<String> list = new ArrayList<>(); //初始化列表
  47. EasyExcel.read("D:/工作簿2.xlsx") //文件路径
  48. .sheet("Sheet1") //表单名
  49. .registerReadListener(new AnalysisEventListener<Map<Integer,String>>() { //监听器,以行的形式读取Excel
  50. //一行一行读取Excel,在这里处理读取到的数据
  51. @Override
  52. public void invoke(Map<Integer, String> integerStringMap, AnalysisContext analysisContext) {
  53. // TODO Auto-generated method stub
  54. list.add(integerStringMap.get(0)); //将读取到的每一行为一组存入列表中
  55. }
  56. //数据读取完毕后运行下面
  57. @Override
  58. public void doAfterAllAnalysed(AnalysisContext context) {
  59. System.out.println("数据读取完毕");
  60. }
  61. }).headRowNumber(0).doRead();
  62. /*QueryWrapper<Photovoltaiccapacity> wrap = new QueryWrapper<>();
  63. wrap.eq("STATIONID", "HZJ_GDC");
  64. List<Photovoltaiccapacity> list = photovoltaiccapacityService.list(wrap);*/
  65. WeightedObservedPoints points = new WeightedObservedPoints();
  66. //获取场站光照测点
  67. Windpowerstationtestingpoint2 hzj_gdc = windpowerstationtestingpoint2Service.getStationIllumination("HZJ_GDC");
  68. long start = DateUtils.string2DateL("2022-12-10 00:00:00").getTime();
  69. long end = DateUtils.string2DateL("2022-12-10 23:59:59").getTime();
  70. //获取场站光照值
  71. List<TsDoubleData> rawValues = adpClient.getRawValuesByKey(hzj_gdc.getCode(), start, end);
  72. rawValues=rawValues.stream().filter(rw->rw.getDoubleValue()!=0.0).collect(Collectors.toList());
  73. List<TsDoubleData> rawValues2=rawValues.stream().sorted(Comparator.comparing(TsDoubleData::getDoubleValue)).collect(Collectors.toList());
  74. long tsstart = rawValues.get(0).getTs();
  75. long tsend = rawValues.get(rawValues.size() - 1).getTs() + 5*60*1000;
  76. double[] index = {0.751897050683247, 0.0131420628997927};
  77. pncf.printResultAll(rawValues2,index,2);
  78. String s1 = pncf.printResultY(rawValues2, index);
  79. List<TsDoubleData> WDvalues = adpClient.getValuesByKey("HZJGGL.NX_GD_HZJG_YC_P1_L1_001_QXZ006", tsstart, tsend, 5 * 60);
  80. //获取光伏逆变器支路电流电压测点
  81. QueryWrapper<Photovoltaiciv> wrapper = new QueryWrapper<>();
  82. //wrapper.eq("electric", "电压").lt("rownum",19).orderByAsc("windturbineid");
  83. wrapper.eq("electric", "电压");
  84. //电压测点
  85. List<Photovoltaiciv> DYlist = photovoltaicivService.list(wrapper);
  86. wrapper.clear();
  87. //wrapper.eq("electric", "电流").lt("rownum",19).orderByAsc("windturbineid");
  88. wrapper.eq("electric", "电流");
  89. //电流测点
  90. List<Photovoltaiciv> DLlist = photovoltaicivService.list(wrapper);
  91. DYlist=DYlist.stream().filter(dl->!list.contains(dl.getWindturbineid()+dl.getBranchnum())).collect(Collectors.toList());
  92. DLlist=DLlist.stream().filter(dl->!list.contains(dl.getWindturbineid()+dl.getBranchnum())).collect(Collectors.toList());
  93. //逆变器+支路号,时间,电压值
  94. Map<String, Map<Long, Double>> DYMapMap = new HashMap<>();
  95. //逆变器+支路号,时间,电流值
  96. Map<String, Map<Long, Double>> DLMapMap = new HashMap<>();
  97. //逆变器+支路号,光照强度,功率
  98. Map<String, Map<Double, Double>> GLMapMap = new HashMap<>();
  99. Map<String, WeightedObservedPoints> PointsMap = new HashMap<>();
  100. for (Photovoltaiciv ptvtc : DYlist) {
  101. //电压值
  102. List<TsDoubleData> DYrawValues = adpClient.getValuesByKey(ptvtc.getId(), tsstart, tsend, 5*60);
  103. Map<Long, Double> collect = DYrawValues.stream().collect(Collectors.toMap(TsDoubleData::getTs, TsDoubleData::getDoubleValue));
  104. DYMapMap.put(ptvtc.getWindturbineid()+ptvtc.getBranchnum(), collect);
  105. }
  106. for (Photovoltaiciv ptvtc : DLlist) {
  107. //电流值
  108. List<TsDoubleData> DLrawValues = adpClient.getValuesByKey(ptvtc.getId(), tsstart, tsend, 5*60);
  109. Map<Long, Double> collect = DLrawValues.stream().collect(Collectors.toMap(TsDoubleData::getTs, TsDoubleData::getDoubleValue));
  110. DLMapMap.put(ptvtc.getWindturbineid()+ptvtc.getBranchnum(), collect);
  111. }
  112. for (Photovoltaiciv ptvtc : DYlist) {
  113. //逆变器+支路号
  114. String nbqzl = ptvtc.getWindturbineid() + ptvtc.getBranchnum();
  115. Map<Double, Double> GLMap = new LinkedHashMap<>();
  116. WeightedObservedPoints pointsOne = new WeightedObservedPoints();
  117. for (TsDoubleData rv : rawValues2) {
  118. double v = DYMapMap.get(nbqzl).get(rv.getTs()) * 0.001 * DLMapMap.get(nbqzl).get(rv.getTs());
  119. /*if((rv.getDoubleValue()<318.0&&v>7.3)||(rv.getDoubleValue()<190.0&&v>4.3)){
  120. System.out.println(nbqzl+","+DYMapMap.get(nbqzl).get(rv.getTs())+","+DLMapMap.get(nbqzl).get(rv.getTs())+","+rv.getTs());
  121. }*/
  122. GLMap.put(rv.getDoubleValue(), v);
  123. points.add(rv.getDoubleValue(), v);
  124. pointsOne.add(rv.getDoubleValue(), v);
  125. }
  126. PointsMap.put(nbqzl, pointsOne);
  127. GLMapMap.put(nbqzl, GLMap);
  128. }
  129. /*for (WeightedObservedPoint point : points.toList()) {
  130. System.out.print("[");
  131. System.out.printf("%.2f", point.getX());
  132. System.out.print(",");
  133. System.out.printf("%.2f", point.getY());
  134. System.out.println("],\n");
  135. }*/
  136. /*File files = new File("D:/光伏光照强度-功率.txt");
  137. FileOutputStream fos = new FileOutputStream(files);
  138. // 逐行写入
  139. PrintWriter pw = new PrintWriter(fos);
  140. for (WeightedObservedPoint point : points.toList()) {
  141. String s = "[" + String.format("%.2f", point.getX()) + "," + String.format("%.2f", point.getY()) + "],";
  142. pw.println(s);
  143. }
  144. pw.close();*/
  145. File files = new File("D:/光伏光照强度-功率.txt");
  146. FileOutputStream fos = new FileOutputStream(files);
  147. // 逐行写入
  148. PrintWriter pw = new PrintWriter(fos);
  149. //文件写入光照强度
  150. for (TsDoubleData rawValue : rawValues2) {
  151. pw.print(String.format("%.2f", rawValue.getDoubleValue()) + ",");
  152. }
  153. pw.println();
  154. //文件写入各支路功率
  155. for (Map.Entry<String, Map<Double, Double>> sme : GLMapMap.entrySet()) {
  156. pw.println(sme.getKey());
  157. for (Double value : sme.getValue().values()) {
  158. String s = String.format("%.2f", value) + ",";
  159. pw.print(s);
  160. }
  161. pw.println();
  162. }
  163. pw.close();
  164. //double[] run = pncf.run(points);
  165. double[] hzj01_22416s = pncf.run(PointsMap.get("HZJ01_22416"));
  166. double[] hzj01_1893s = pncf.run(PointsMap.get("HZJ01_1893"));
  167. /*//计算最差的和最好的
  168. for (Map.Entry<String, Map<Double, Double>> sme : GLMapMap.entrySet()) {
  169. double v = pncf.calcLow(sme.getValue(), run);
  170. System.out.println(sme.getKey()+","+v);
  171. }*/
  172. pncf.printResultAll(rawValues2,index,2);
  173. pncf.printResultAll(rawValues2,hzj01_22416s,2);
  174. pncf.printResultAll(rawValues2,hzj01_1893s,2);
  175. }
  176. void test2(){
  177. String faultCode = windturbinetestingpointai2Service.getFaultCodes().get(0).getId();
  178. Calendar instance = Calendar.getInstance();
  179. instance.add(Calendar.DATE, -2);
  180. long start = DateUtils.getZero(instance).getTimeInMillis();
  181. long end = DateUtils.getNight(instance).getTimeInMillis();
  182. Map<Integer, Map<Long, Double>> 电流MapMap = new HashMap<>();
  183. Map<Integer, Map<Long, Double>> 电压MapMap = new HashMap<>();
  184. Map<Integer, Map<Long, Double>> 功率MapMap = new HashMap<>();
  185. List<List<TsDoubleData>> 电流ListList = new ArrayList<>();
  186. List<List<TsDoubleData>> 电压ListList = new ArrayList<>();
  187. List<List<Double>> 功率ListList = new ArrayList<>();
  188. for (int i = 1; i < 19; i++) {
  189. String s = String.valueOf(i);
  190. String code = faultCode.substring(0, faultCode.length() - s.length()) + s;
  191. //List<TsDoubleData> rawValuesByKey = adpClient.getRawValuesByKey(code, start, end);
  192. List<TsDoubleData> rawValuesByKey = adpClient.getValuesByKey(code, start, end, 60);
  193. //Map<Long, Double> 电压Map = rawValuesByKey.stream().collect(Collectors.toMap(TsDoubleData::getTs, TsDoubleData::getDoubleValue));
  194. //电压MapMap.put(i, 电压Map);
  195. 电压ListList.add(rawValuesByKey);
  196. }
  197. for (int j = 19; j < 37; j++) {
  198. String s = String.valueOf(j);
  199. String code = faultCode.substring(0, faultCode.length() - s.length()) + s;
  200. List<TsDoubleData> valuesByKey = adpClient.getValuesByKey(code, start, end, 60);
  201. //Map<Long, Double> 电流Map = valuesByKey.stream().collect(Collectors.toMap(TsDoubleData::getTs, TsDoubleData::getDoubleValue));
  202. //电流MapMap.put(j - 18, 电流Map);
  203. 电流ListList.add(valuesByKey);
  204. }
  205. /*电压MapMap.forEach((i,pdata)->{
  206. Map<Long, Double> 功率Map = new HashMap<>();
  207. pdata.forEach((ts,data)->{
  208. //电流值
  209. Double aDouble = 电流MapMap.get(i).get(ts);
  210. 功率Map.put(ts, data * 0.001 * aDouble);
  211. });
  212. 功率MapMap.put(i, 功率Map);
  213. });
  214. 功率MapMap.forEach((i,pdata)->{
  215. System.out.println(i);
  216. pdata.forEach((ts,data)->{
  217. System.out.print(ts+",");
  218. });
  219. pdata.forEach((ts,data)->{
  220. System.out.print(data+",");
  221. });
  222. });*/
  223. for (int i = 0; i < 电压ListList.size(); i++) {
  224. List<TsDoubleData> dyData = 电压ListList.get(i);
  225. List<TsDoubleData> dlData = 电流ListList.get(i);
  226. List<Double> glData = new ArrayList<>();
  227. System.out.println("\n\n\n" + i);
  228. for (int j = 0; j < dyData.size(); j++) {
  229. double v = dyData.get(j).getDoubleValue() * 0.001 * dlData.get(j).getDoubleValue();
  230. glData.add(v);
  231. }
  232. for (Double glDatum : glData) {
  233. System.out.printf("%.2f", glDatum);
  234. System.out.print(",");
  235. }
  236. 功率ListList.add(glData);
  237. }
  238. System.out.println("\n\n\n");
  239. for (TsDoubleData dyDatum : 电压ListList.get(0)) {
  240. System.out.print(dyDatum.getTs() + ",");
  241. }
  242. }
  243. }