-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNLGNNdiff_par_basic.m
More file actions
4763 lines (3966 loc) · 186 KB
/
NLGNNdiff_par_basic.m
File metadata and controls
4763 lines (3966 loc) · 186 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
% 2022-02-21: This code will do parameter estimation over each partitioned
% sub-network.
% Before running, check:
% (1) The setting of parameters.
clear all
%% Set up parallel compuation for matlab
Max_para_num=3; % maximum number of parallel threads.
delete(gcp('nocreate'));%delete existing pool.
parpool('local',Max_para_num); %initialize parallel pool.
%% Load input data
Input_struct=[];
load('SaveData/z20220220_input_for_para_est')
w_start=Input_struct.w_start;
w_z_correct=Input_struct.w_z_correct;
line_node_list=Input_struct.line_node_list;
main_line_config=Input_struct.main_line_config;
partition_table=Input_struct.partition_table;
% partition_table=[];
global_source_node=Input_struct.global_source_node;
app_P=Input_struct.app_P;
app_Q=Input_struct.app_Q;
app_V_mag=Input_struct.app_V_mag;
app_PMU_P=Input_struct.app_PMU_P;
app_PMU_Q=Input_struct.app_PMU_Q;
load_id_phase=Input_struct.load_id_phase;
load_to_main_node=Input_struct.load_to_main_node;
source_V_mag=Input_struct.source_V_mag;
source_V_rad=Input_struct.source_V_rad;
source_P=Input_struct.source_P;
source_Q=Input_struct.source_Q;
PMU_V_mag=Input_struct.PMU_V_mag;
PMU_V_rad=Input_struct.PMU_V_rad;
%% parameter definition
% alg_seed_list=[79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199];
alg_seed_list=[1]; % seed for SGD algorithm random number.
error_ratio=0.5;
batch_size=10;
max_iter=2e3; % 2e3 by default
% end_threshold_ratio=1e-20;
threshold_ratio=1e-20; % threshold to stop iteration. It is a ratio indicating how much
% is changed compared with the previous iteration.
early_stop_patience=10; % number of epochs of no improvement, after which stop the iteration.
early_stop_threshold_ratio=0.01; % decide improvement: at least early_stop_threshold_ratio*initial obj function value.
% Backtracking linesearch parameter
initial_step_size=1e3; % standard step size
alpha=0.3; % Backtracking line search parameter.
beta=0.5; % Backtracking line search parameter.
step_dynamic_beta=2; % parameter to decide dynamic initial step size
min_step_threshold=1e-20;% minimum initial step size when using dynamic initial step size, default=1e-20.
max_step_threshold=1e3;
folder_dir='SaveResult/20220222_37b_GitHub';
description_txt='3-parition NLGNNdiff, 37 bus balanced. No noise. No prior. No partition';
limit_range_txt='no'; % yes or no, if range limit is used.
prior_adjust_ratio=0; % a ratio to adjust the regularization factor.
if isfolder(folder_dir) % if the folder already exists
disp('SAVE FOLDER ALREADY EXISTS!!')
return
else
mkdir(folder_dir);%else, create the folder
end
%% load and prepare data
% TUNABLE HERE
if ~isempty(partition_table)
all_source_node_list=[global_source_node;partition_table(:,1)]; % this is the full source and pseudo source node
else
all_source_node_list=global_source_node;
end
% nodal_data_clean=nodal_data; %remember to change back!!!!
% prepare intermediate node data (here add PMU 3-phase meter data)
% Note that not all such inter node volt will be used in the subnets.
inter_node=all_source_node_list;
inter_V_mag=[source_V_mag;PMU_V_mag];
%% build weight strucutre (line parameter) from the primitive Y of each line.
% The main_node_list is also the order for constructing the MLE model.
main_node_list=unique([main_line_config(:,1);main_line_config(:,2)]); %this is the node list for the reduced feeder model
% main_line_config=line3p_config_matrix(:,1:3);% a table used for MLE code. Col 1--node A, col 2--node B, col 3--line length in ft.
main_node_num=length(main_node_list);
num_L=size(main_line_config,1); % number of lines
%% Prepare label (nodal real and reactive power)
% source_node=650;
source_idx=find(main_node_list==global_source_node);%find the index of the source node
main_nonsource_node_list=main_node_list;
main_nonsource_node_list(source_idx)=[];
main_nonsource_node_num=length(main_nonsource_node_list);
% remove the unmeasured point
load_id_list=load_id_phase(:,1);
% load_id_phase=nonsource_id_phase;
I1=ismember(load_id_list,load_to_main_node(:,1));
temp_idx=find(~I1);
load_id_list(temp_idx)=[];
load_id_phase(temp_idx,:)=[];
load_num=length(load_id_list);
temp_idx_3=temp_idx*3-3+(1:3);
app_P(temp_idx,:)=[];
app_Q(temp_idx,:)=[];
app_V_mag(temp_idx,:)=[];
% the linear model, U_hats correspond to main node's nodal power input, U1 and U2
% correspond to nonsource main node nodal volt, X correspond to matching
% phase to input load and output volt measurement. In the GNN model, we
% modify to include source node in the U_hats, for easier coding.
X=[];
U1=zeros(3*load_num,3*main_nonsource_node_num); % U1 correspond to output, not changed for label calculation.
U2=zeros(3*load_num,3*main_nonsource_node_num);
U_hat_1=zeros(3*main_node_num,3*load_num); % change in order to properly calcualte label
U_hat_2=zeros(3*main_node_num,3*load_num);
U_hat_3=zeros(3*main_node_num,3*load_num);
U_hat_4=zeros(3*main_node_num,3*load_num);
for i=1:load_num % loop through every cust node (cust_id_list is the order of the node in the data tables)
temp_id=load_id_list(i);
I1=find(load_id_phase(:,1)==temp_id);
temp_phase=load_id_phase(I1,2);
temp_3p_phase=load_id_phase(I1,3); % if it is 3-phase, which phase does it measure volt.
% temp_3p_phase=1; % remember to change back!!!!
u_2p=0.5*sqrt(3)*[1 1 0; 0 1 1; 1 0 1];
u_hat_1=0.5*[1 0 1;1 1 0;0 1 1];
u_hat_2=(1/2/sqrt(3))*[1 0 -1;-1 1 0;0 -1 1];
u_hat_3=(1/2/sqrt(3))*[-1 0 1;1 -1 0;0 1 -1];
u_hat_4=0.5*[1 0 1;1 1 0;0 1 1];
temp_u2_2p=0.5*[1 -1 0; 0 1 -1; -1 0 1];
switch temp_phase % each phaes connection case
case 1 %phase a
temp_x=[1 0 0];
temp_u1=eye(3);
temp_u2=zeros(3);
temp_u_hat_1=eye(3);
temp_u_hat_2=zeros(3);
temp_u_hat_3=zeros(3);
temp_u_hat_4=eye(3);
case 2 %phase b
temp_x=[0 1 0];
temp_u1=eye(3);
temp_u2=zeros(3);
temp_u_hat_1=eye(3);
temp_u_hat_2=zeros(3);
temp_u_hat_3=zeros(3);
temp_u_hat_4=eye(3);
case 3 % phase c
temp_x=[0 0 1];
temp_u1=eye(3);
temp_u2=zeros(3);
temp_u_hat_1=eye(3);
temp_u_hat_2=zeros(3);
temp_u_hat_3=zeros(3);
temp_u_hat_4=eye(3);
case 12 %phase ab
temp_x=[1 0 0];
temp_u1=u_2p;
temp_u2=temp_u2_2p;
temp_u_hat_1=u_hat_1;
temp_u_hat_2=u_hat_2;
temp_u_hat_3=u_hat_3;
temp_u_hat_4=u_hat_4;
case 23 %phase bc
temp_x=[0 1 0];
temp_u1=u_2p;
temp_u2=temp_u2_2p;
temp_u_hat_1=u_hat_1;
temp_u_hat_2=u_hat_2;
temp_u_hat_3=u_hat_3;
temp_u_hat_4=u_hat_4;
case 31 % phase ca
temp_x=[0 0 1];
temp_u1=u_2p;
temp_u2=temp_u2_2p;
temp_u_hat_1=u_hat_1;
temp_u_hat_2=u_hat_2;
temp_u_hat_3=u_hat_3;
temp_u_hat_4=u_hat_4;
case 123 % phase abc, assume measuring the phase a volt
switch temp_3p_phase
case 1 %3 phase measure a
temp_x=[1 0 0];
case 2 %3 phase measure b
temp_x=[0 1 0];
case 3 % 3 phase measure c
temp_x=[0 0 1];
end
temp_u1=eye(3);
temp_u2=zeros(3);
temp_u_hat_1=1/3*ones(3);
temp_u_hat_2=zeros(3);
temp_u_hat_3=zeros(3);
temp_u_hat_4=1/3*ones(3);
end
temp_idx=find(load_to_main_node(:,1)==temp_id);
temp_main_node=load_to_main_node(temp_idx,2);
temp_j=find(main_node_list==temp_main_node);
temp_select_i=i*3-3+(1:3);
temp_select_j=temp_j*3-3+(1:3);
X=blkdiag(X,temp_x);
U1(temp_select_i,temp_select_j)=temp_u1;
U2(temp_select_i,temp_select_j)=temp_u2;
U_hat_1(temp_select_j,temp_select_i)=temp_u_hat_1;
U_hat_2(temp_select_j,temp_select_i)=temp_u_hat_2;
U_hat_3(temp_select_j,temp_select_i)=temp_u_hat_3;
U_hat_4(temp_select_j,temp_select_i)=temp_u_hat_4;
end
% Pack the nodal P Q injection corrsponding to main_node_list
% temp_M=[U_hat_1,U_hat_2;-U_hat_2,U_hat_1]*blkdiag(X',X')*[app_P;app_Q];
main_nodal_P=[U_hat_1,U_hat_2]*blkdiag(X',X')*[app_P;app_Q];
main_nodal_Q=[-U_hat_2,U_hat_1]*blkdiag(X',X')*[app_P;app_Q];
temp_length=size(main_nodal_P,2);
% I1=find(node_id_list==global_source_node);
% source load has no noise.
source_idx=find(main_node_list==global_source_node);%find the index of the source node in the main_node_list, nonsource_main_node_list is the same as main_node_list except it removes the source node.
temp_idx=(source_idx-1)*3+(1:3); % index corresponding to source 3-phase power injection.
main_nodal_P(temp_idx,:)=source_P; % insert source node injection
main_nodal_Q(temp_idx,:)=source_Q; % insert source node injection
% pack the label into label structure
% Structure of label:
% label(i).node--name of node i.
% label(i).conj--a 3-by-T matrix of nodal conjugate complex power of node i of all
% time through T.
label=[];
for i=1:length(main_node_list)
label(i).node=main_node_list(i);
temp_idx=(i-1)*3+(1:3);
label(i).conj=main_nodal_P(temp_idx,:)-1i*main_nodal_Q(temp_idx,:);
label(i).P=main_nodal_P(temp_idx,:);
label(i).Q=main_nodal_Q(temp_idx,:);
end
%% prepare state structure
% Prepare the initial state and the correct state to compare.
% Structure of in_state:
% in_state(i).node--name of node i.
% in_state(i).mag--a 3-by-T matrix of nodal volt magnitude of node i of all
% time through T.
% in_state(i).rad--a 3-by-T matrix of nodal volt angle (in rad) of node i of
% all time through T.
state_initial=[];
temp_T=size(app_P,2); % time length
for i=1:length(main_node_list)
target_node=main_node_list(i);
state_initial(i).node=target_node;
state_initial(i).mag=ones(3,temp_T);
state_initial(i).rad=[0;-2*pi/3;2*pi/3]*ones(1,temp_T);
end
% will use the complex form of the state.
state_initial_complex=f_state2complex(state_initial);
%% build output model.
% Prepare output_model:
% output_model--structure containing how the state is mapped to the output.
% Structure of output_model:
% output_model(i).meter--the name of the meter i.
% output_model(i).node--the node which the meter is connected to.
% output_model(i).phase--the phase of the smart meter i.
% 1,2,3,12,23,31,301,302,303 represents phase A, B, C, AB, BC, CA, 3-phase
% A, 3-phase B, and 3-phase C.
% output_model(i).f_v--the vector model of meter i. f_v is 1-by-3, a
% selection vetor of 1,-1, and 0 indicating which phase has which sign. f_v
% is the same for both real and imaginary part of a state of one particular
% meter.
% 2020-05-15: add the measurement at the intermediate node.
output_model=[];
u_2p=0.5*sqrt(3)*[1 1 0; 0 1 1; 1 0 1];
temp_u2_2p=0.5*[1 -1 0; 0 1 -1; -1 0 1];
for i_meter=1:length(load_id_list)
target_meter=load_id_list(i_meter);
output_model(i_meter).meter=target_meter;
I1=find(load_to_main_node(:,1)==target_meter);
target_node=load_to_main_node(I1,2);
output_model(i_meter).node=target_node;
I1=find(load_id_phase(:,1)==target_meter);
target_phase=load_id_phase(I1,2);
temp_3p_phase=load_id_phase(I1,3);
% temp_3p_phase=1;%remember to change back!!!!
switch target_phase % each phaes connection case
case 1 %phase a
output_model(i_meter).phase=target_phase;
output_model(i_meter).f_v=[1 0 0];
case 2 %phase b
output_model(i_meter).phase=target_phase;
output_model(i_meter).f_v=[0 1 0];
case 3 %phase c
output_model(i_meter).phase=target_phase;
output_model(i_meter).f_v=[0 0 1];
case 12 %phase ab
output_model(i_meter).phase=target_phase;
output_model(i_meter).f_v=[1 -1 0];
case 23 %phase bc
output_model(i_meter).phase=target_phase;
output_model(i_meter).f_v=[0 1 -1];
case 31 %phase ca
output_model(i_meter).phase=target_phase;
output_model(i_meter).f_v=[-1 0 1];
case 123 % phase abc, assume measuring the phase a volt
switch temp_3p_phase
case 1 %3 phase measure a
output_model(i_meter).phase=301;
output_model(i_meter).f_v=[1 0 0];
case 2 %3 phase measure b
output_model(i_meter).phase=302;
output_model(i_meter).f_v=[0 1 0];
case 3 % 3 phase measure c
output_model(i_meter).phase=303;
output_model(i_meter).f_v=[0 0 1];
end
end
end
% add the measurement of intermediate node
i_meter=length(load_id_list); % index
for i_inter_node=1:length(inter_node)
target_meter=inter_node(i_inter_node);
for i_phase=1:3 % each inter node has 3 phase meter
i_meter=i_meter+1;
output_model(i_meter).meter=target_meter*10+i_phase; % define meter id as node id+phase number
output_model(i_meter).node=target_meter;
output_model(i_meter).phase=i_phase;
temp_v=zeros(1,3);
temp_v(i_phase)=1;
output_model(i_meter).f_v=temp_v;
end
end
% the correct output
% output--a structure of output. output(t).meter_list is a vector containing the
% list of meter names (same for all t). output(t).value is a vector of
% output value (in this case the volt measurement) matching to each meter
% at time t.
% app_V_mag_diff=diff(app_V_mag,1,2); % use time difference of meter volt measures
inter_V_mag=inter_V_mag;
T=size(app_V_mag,2); % time length of measures.
output_correct=[];
temp_meter_list=[output_model(:).meter]';
for i_t=1:T
output_correct(i_t).meter_list=temp_meter_list;
if ~isempty(inter_node)
output_correct(i_t).value=[app_V_mag(:,i_t);inter_V_mag(:,i_t)];
else
output_correct(i_t).value=[app_V_mag(:,i_t)];
end
end
T_length=length(output_correct); % the length in time of actual smart meter data (before taking time difference).
T_diff_mat_full=[(1:T_length-1)',(2:T_length)']; % time difference index table full: time difference is column 2's time data minus column 1's.
output_correct_diff=f_output2diff(output_correct,T_diff_mat_full);
%% Build d_GB_d_w
% d_GB_d_w--a structure storing the fixed derivative matrices for line
% parameter.d_GB_d_w(i).M is a 3-by-3 matrix. i=1~6, representing 6 phase
% types: aa, ab, ac, bb, bc, cc. G and B share the same patter, so we only
% i=1~6 to represent these cases.
d_GB_d_w=[];
for i=1:6
temp_M=zeros(3);
switch i
case 1 % phase aa
temp_M(1,1)=1;
case 2 % phase ab
temp_M(1,2)=1;
temp_M(2,1)=1;
case 3 % phase ac
temp_M(1,3)=1;
temp_M(3,1)=1;
case 4 % phase bb
temp_M(2,2)=1;
case 5 % phase bc
temp_M(2,3)=1;
temp_M(3,2)=1;
case 6 % phase cc
temp_M(3,3)=1;
end
d_GB_d_w(i).M=temp_M;
end
%% prepare data for global estimation setup
all_source_state(1).node=global_source_node;
all_source_state(1).mag=source_V_mag;
all_source_state(1).rad=source_V_rad;
for i=2:length(all_source_node_list) % accurate data of all feeder head and PMU
temp_idx=(1:3)+(i-2)*3;
all_source_state(i).node=all_source_node_list(i);
all_source_state(i).mag=PMU_V_mag(temp_idx,:);
all_source_state(i).rad=PMU_V_rad(temp_idx,:);
end
all_source_state_complex=f_state2complex(all_source_state);
% TUNABLE HERE.
w_start=Input_struct.w_start;
w_iter=w_start;
%%% prepare the range limit
% TUNABLE HERE.
if strcmp(limit_range_txt,'yes')
[w_min,w_max]=f_w_generate_limit(w_start,error_ratio);
elseif strcmp(limit_range_txt,'no')
[w_min,w_max]=f_w_generate_inf_limit(w_start);
else
disp('RANGE LIMIT USAGE NOT CORRECTLY DFINED')
return
end
%%% Generate structures that store the variance and mean value of each
%%% parameter. The mean should be the initial parameter value, the sigma
%%% should be 1/3 of the parameter’s error range value.
prior_dist=f_generate_prior_dist(w_start,error_ratio);
%% Separate the netowrk into sub-networks.
%%% use matlab's graph functions, first use index number to indicate each
%%% node.
line_node_list=unique([main_line_config(:,1);main_line_config(:,2)]);
% start building the line configuration of break-up network
[Lia,main_line_sep_idx_table]=ismember(main_line_config(:,1:2),line_node_list); % represent config table in terms of indices.
[Lia,partition_table_idx]=ismember(partition_table,line_node_list); % represent partition table in indices
for i=1:size(partition_table_idx,1) %remove cutting edge from the full connection table
temp_idx_1=partition_table_idx(i,1);
temp_idx_2=partition_table_idx(i,2);
I1=find(main_line_sep_idx_table(:,1)==temp_idx_1 & main_line_sep_idx_table(:,2)==temp_idx_2);
I2=find(main_line_sep_idx_table(:,1)==temp_idx_2 & main_line_sep_idx_table(:,2)==temp_idx_1); % consider the swaping of indices
main_line_sep_idx_table([I1;I2],:)=[]; % remove i-th cutting edge.
end
temp_G=graph(main_line_sep_idx_table(:,1),main_line_sep_idx_table(:,2),[],length(line_node_list));
bins=conncomp(temp_G); % the bins number represents different groups of connected nodes
%%% Now build sub-networks, each cell contains the line config table of a
%%% sub-net.
sub_net_struct=[];
for i=1:max(bins) % each loop organize the sub-net of the i-th bin
I1=find(bins==i);
Lia=ismember(main_line_sep_idx_table,I1);
temp_net_idx=main_line_sep_idx_table(Lia(:,1)&Lia(:,2),:);
sub_net_struct(i).line_config=line_node_list(temp_net_idx); % store the sub-network in terms actual node number.
end
% Add pesudo-substation to the downstream subnetwork
for i=1:length(sub_net_struct)
for i2=1:size(partition_table,1) % check if the cutting edge's downstream connects the sub-net
temp_line=partition_table(i2,:);
Lia=ismember(temp_line(2),sub_net_struct(i).line_config);
if Lia % if the downstream node is in the sub-net
sub_net_struct(i).line_config=[sub_net_struct(i).line_config;temp_line];
end
end
end
%% Prepare the data and parameters for each sub-network.
%%% define parameter set first, some values are the same for all sub-nets,
%%% define here first:
% para_set.weight_seed=weight_seed;
para_set.batch_size=batch_size;
para_set.max_iter=max_iter;
para_set.threshold_ratio=threshold_ratio;
para_set.early_stop_patience=early_stop_patience; % number of epochs of no improvement, after which stop the iteration.
para_set.early_stop_threshold_ratio=early_stop_threshold_ratio; % decide improvement: at least early_stop_threshold_ratio*initial obj function value.
para_set.prior_adjust_ratio=prior_adjust_ratio; % a ratio to adjust the regularization factor.
% Backtracking linesearch parameter
para_set.initial_step_size=initial_step_size; % standard step size
para_set.alpha=alpha; % Backtracking line search parameter.
para_set.beta=beta; % Backtracking line search parameter.
% Step size adjusting parameter
para_set.step_dynamic_beta=step_dynamic_beta; % parameter to decide dynamic initial step size
para_set.min_step_threshold=min_step_threshold;% minimum initial step size when using dynamic initial step size
para_set.max_step_threshold=max_step_threshold;
% para_set.primal_var=primal_var;
para_set.folder_dir=folder_dir; % folder name to save the result
para_set.description=description_txt;
para_set.limit_range_txt=limit_range_txt;
% global parameter setup define here
global_para_set=para_set;
global_para_set.w_start=w_start;
global_para_set.w_z_correct=w_z_correct;
global_para_set.prior_dist=prior_dist;
global_para_set.limit_range.w_max=w_max;
global_para_set.limit_range.w_min=w_min;
global_para_set.sub_net_struct=sub_net_struct;
% para_set_array_by_subnet=[]; % each subnet has the same para_set
% data_set_array_by_subnet=struct;
for i_net=1:length(sub_net_struct)
temp_line_config=sub_net_struct(i_net).line_config;
temp_subnet_node_list=unique([temp_line_config(:,1);temp_line_config(:,2)]);
temp_para_set=para_set;
temp_data_set=[];
%%% continue building sub-net para_set
temp_para_set.line_config=temp_line_config;
temp_para_set.subnet_id=i_net;
temp_para_set.w_start=f_build_subnet_w(w_start,temp_line_config);
temp_para_set.w_correct=f_build_subnet_w(w_z_correct,temp_line_config);
temp_para_set.w_min=f_build_subnet_w(w_min,temp_line_config);
temp_para_set.w_max=f_build_subnet_w(w_max,temp_line_config);
% the para set of each subnet
para_set_array_by_subnet(i_net)=temp_para_set;
%%% build sub-net data_set's label
% build label
temp_label=f_filter_subnet_label(label,temp_subnet_node_list); % filter to keep only the label of nodes in the subnet
temp_label_node_list=[temp_label.node]';
Lia=ismember(partition_table,temp_line_config);
% I1=find(Lia(:,1)&Lia(:,2)); % I1 indicates the cutting edge's both nodes are in this subnet
if ~isempty(Lia)
I1=find(Lia(:,1)&Lia(:,2)); % I1 indicates the cutting edge's both nodes are in this subnet
else
I1=[];
end
temp_partition=partition_table(I1,:);
for i_cut=1:size(temp_partition,1) % adjust label for each pseudo substation for this downstream subnet
temp_node_1=temp_partition(i_cut,1);
temp_node_2=temp_partition(i_cut,2);
temp_txt=[num2str(temp_node_1),'_',num2str(temp_node_2)]; %the corresponding PMU id
% I1=find(cell2mat(cellfun(@(x) strcmp(x,temp_txt),PMU_id_list,'UniformOutput',false))); % find the pseudo substation's corresponding source load
I1=find(partition_table(:,1)==temp_node_1 & partition_table(:,2)==temp_node_2); % find the pseudo substation's corresponding source load
temp_idx=(I1-1)*3+(1:3);
temp_P=app_PMU_P(temp_idx,:);
temp_Q=app_PMU_Q(temp_idx,:);
I2=find(temp_label_node_list==temp_node_1); % find the label index of the pseudo substation
temp_label(I2).P=temp_P; % adjust the label value (the pesodo-source's power injection)
temp_label(I2).Q=temp_Q;
temp_label(I2).conj=temp_P-1i*temp_Q;
end
if ~isempty(Lia)
I1=find(Lia(:,1)&~Lia(:,2)); % I1 indicates the cutting edges has only upstream node in this subnet
else
I1=[];
end
% I1=find(Lia(:,1)&~Lia(:,2)); % I1 indicates the cutting edges has only upstream node in this subnet
temp_partition=partition_table(I1,:);
for i_cut=1:size(temp_partition,1) % adjust label for each pseudo substation for this upstream subnet
temp_node_1=temp_partition(i_cut,1);
temp_node_2=temp_partition(i_cut,2);
temp_txt=[num2str(temp_node_1),'_',num2str(temp_node_2)]; %the corresponding PMU id
% I1=find(cell2mat(cellfun(@(x) strcmp(x,temp_txt),PMU_id_list,'UniformOutput',false))); % find the pseudo substation's corresponding source load
I1=find(partition_table(:,1)==temp_node_1 & partition_table(:,2)==temp_node_2); % find the pseudo substation's corresponding source load
temp_idx=(I1-1)*3+(1:3);
temp_P=app_PMU_P(temp_idx,:);
temp_Q=app_PMU_Q(temp_idx,:);
I2=find(temp_label_node_list==temp_node_1); % find the label index of the pseudo substation
% pay attention to the sign below, for upstream subnet, the
% substation absorbs the power.
temp_label(I2).P=temp_label(I2).P-temp_P; % adjust the label value (the pesodo-source's power injection)
temp_label(I2).Q=temp_label(I2).Q-temp_Q;
temp_label(I2).conj=temp_label(I2).conj-temp_P+1i*temp_Q;
end
temp_data_set.label=temp_label; % label is build.
% build subnet's state
temp_data_set.state_initial_complex=f_build_subnet_complex_state(state_initial_complex,temp_subnet_node_list);
% build source node
Lia=ismember(all_source_node_list,temp_subnet_node_list);
temp_node_list=all_source_node_list(Lia); % the souce nodes in this subnet
% only keep one node as source.
I1=find(temp_node_list==global_source_node);
temp_PMU_list=temp_node_list;
if ~isempty(I1)
temp_source_node=global_source_node;
temp_PMU_list(I1)=[];
else
temp_source_node=temp_node_list(1);
temp_PMU_list(1)=[];
end
% [Lia,Locb]=ismember(temp_node_list,[state_measure_complex.node]');
% temp_data_set.source_state_complex=state_measure_complex(Locb);
[Lia,Locb]=ismember(temp_source_node,[all_source_state_complex.node]');
temp_data_set.source_state_complex=all_source_state_complex(Locb);
% build ouput model and correct output
temp_data_set.output_model=f_build_subnet_output_model(output_model,temp_subnet_node_list,temp_source_node);
temp_data_set.T_diff_mat_full=T_diff_mat_full;
temp_data_set.output_correct_diff=f_build_subnet_output(output_correct_diff,temp_data_set.output_model);
temp_data_set.output_correct=f_build_subnet_output(output_correct,temp_data_set.output_model);
% build prior
temp_data_set.prior_dist=f_build_subnet_prior_dist(prior_dist,temp_line_config);
% other data set values
temp_data_set.d_GB_d_w=d_GB_d_w;
% The data set of each subnet
data_set_array_by_subnet(i_net)=temp_data_set;
end
%% Prepare the data for each sub-network's case of one algorithm seed.
% This is different from data/parameter set by subnet, because each subnet
% may have more than one algorithm seed, and each case will have a
% different save file name.
subnet_list=1:length(sub_net_struct);
Test_case=[];% this is the test case, containing the circuit id and alg_seed
for i_alg=1:length(alg_seed_list)
for i_net=1:length(subnet_list)
Test_case=[Test_case;i_net,alg_seed_list(i_alg)]; % col 1--net id, col 2--alg_seed
end
end
global_para_set.alg_seed_list=alg_seed_list;
save([folder_dir,'/global_setup_save'],'global_para_set');
%% (parallel) simulation of parameter estimation in each case of subnet-alg_seed combination
% To do parallel computing, use parfor.
parfor (i_sim=1:size(Test_case,1),Max_para_num)
% for i_sim=3:3
i_net=Test_case(i_sim,1);
alg_seed=Test_case(i_sim,2);
case_para_set=para_set_array_by_subnet(i_net);
case_para_set.alg_seed=alg_seed;
case_para_set.file_dir=['subnet-',num2str(i_net),'_algseed-',num2str(alg_seed)];
[save_result]=f_subnet_est_NL_GNNdiff_lm(case_para_set,data_set_array_by_subnet(i_net));
end
%% This is the end of the main code.
%% Define functions for the gradient method
%%% function that generate a state only on the batched time stamps.
function out_state_batch=f_state_batch(in_state,idx)
% Input:
% in_state--input state structure containing the nodal volt magnitude and
% angle. In this problem, it is the initialized state.
% idx--index of selected time stamps.
% Output:
% out_state_batch--same kind of structure as in_state, but only contains
% time of indexed timestampls.
%%% Details of the structures---------------------------------
% Structure of in_state:
% in_state(i).node--name of node i.
% in_state(i).mag--a 3-by-T matrix of nodal volt magnitude of node i of all
% time through T.
% in_state(i).rad--a 3-by-T matrix of nodal volt angle (in rad) of node i of
% all time through T.
out_state_batch=in_state;
for i=1:length(in_state)
out_state_batch(i).real=out_state_batch(i).real(:,idx);
out_state_batch(i).imag=out_state_batch(i).imag(:,idx);
end
end
%%% function that generate a label only on the batched time stamps.
function out_label_batch=f_label_batch(in_label,idx)
% Input:
% label--label structure containing the nodal real and reactive power
% injection.
% idx--index of selected time stamps.
% Output:
% out_label_batch--same kind of structure as label, but only contains
% time of indexed timestampls.
%%% Details of the structures---------------------------------
% Structure of label:
% label(i).node--name of node i.
% label(i).conj--a 3-by-T matrix of nodal conjugate complex power of node i of all
% time through T.
% out_label_batch=in_label;
for i=1:length(in_label)
out_label_batch(i).node=in_label(i).node;
out_label_batch(i).conj=in_label(i).conj(:,idx);
out_label_batch(i).P=in_label(i).P(:,idx);
out_label_batch(i).Q=in_label(i).Q(:,idx);
end
end
%%% function to update parameter value
function w_out=f_update_w(w_in,w_gradient,step_size)
% Input:
% w_in--the weight/parameter. In our problem this is the set of line
% parameters. The structure is the same as w.
% w_gradient--the gradient structure representing the gradient of the
% weight, has the same structure as w.
% step_size--the step size. Note that in gradient descent, the step size
% input here should be a negative value. The step size is a multiplier to
% multiply the gradient.
% Output:
% w_out--the new weight structure updated by the step size.
% Structure of w:
% w(i).line--a 2-element vector containing the vertices of the line. Length
% of w is the number of lines.
% w(i).g--a vector of conductance of line i, contain 6 elements.
% w(i).b--a vector of susceptance of line i, contain 6 elements.
% The 6 elements are in the order of phase aa, ab, ac, bb, bc, cc.
w_out=w_in;
for i=1:length(w_in)
w_out(i).g=w_out(i).g+w_gradient(i).g*step_size;
w_out(i).b=w_out(i).b+w_gradient(i).b*step_size;
end
end
%%% function to reorganize a weight strucutre to a vector
function w_vec=f_w_struct2vec(w_in)
% Input:
% w_in--the weight/parameter. In our problem this is the set of line
% parameters. The structure is the same as w.
% Output:
% w_vec--a vector that packs the structure values.
% Structure of w:
% w(i).line--a 2-element vector containing the vertices of the line. Length
% of w is the number of lines.
% w(i).g--a vector of conductance of line i, contain 6 elements.
% w(i).b--a vector of susceptance of line i, contain 6 elements.
% The 6 elements are in the order of phase aa, ab, ac, bb, bc, cc.
w_vec=[];
for i=1:length(w_in) % for each line, pack g 1st, b 2nd.
w_vec=[w_vec;w_in(i).g];
w_vec=[w_vec;w_in(i).b];
end
end
%%% function to calculate MADR of two sets of parameters
function MADR=f_MADR(vec_est,vec_true)
vec_diff=vec_est-vec_true;
MADR=sum(abs(vec_diff))/sum(abs(vec_true))*100;
end
%%% function to calculate the sign of weight or weight gradient structure
function w_sign=f_weight_sign(w_in)
% Input:
% w_in--the weight/parameter. In our problem this is the set of line
% parameters. The structure is the same as w.
% Output:
% w_sign--a structure same as w_in, but only store the sign of the w_in.
% Structure of w:
% w(i).line--a 2-element vector containing the vertices of the line. Length
% of w is the number of lines.
% w(i).g--a vector of conductance of line i, contain 6 elements.
% w(i).b--a vector of susceptance of line i, contain 6 elements.
% The 6 elements are in the order of phase aa, ab, ac, bb, bc, cc.
w_sign=w_in;
temp_x=1e-16; % a term for equilibrium values
for i=1:length(w_sign)
w_sign(i).g=sign(w_in(i).g)+temp_x;
w_sign(i).b=sign(w_in(i).b)+temp_x;
end
end
%%% function to update the step size based on Rprop rule
function step_new=f_Rprop_update_step(step_old,grad_sign_old,grad_sign_new,eta_plus,eta_minus)
% Input:
% step_old--old step structure, structure same as w.
% grad_sign_old--old sign structure. structure same as w.
% grad_sign_new--new sign structure. structure same as w.
% eta_plus--parameter for Rprop.
% eta_minus--parameter for Rprop.
% Output:
% step new--new step structure. structure same as w.
% Structure of w:
% w(i).line--a 2-element vector containing the vertices of the line. Length
% of w is the number of lines.
% w(i).g--a vector of conductance of line i, contain 6 elements.
% w(i).b--a vector of susceptance of line i, contain 6 elements.
% The 6 elements are in the order of phase aa, ab, ac, bb, bc, cc.
step_new=step_old;
for i=1:length(step_old)
sign_g=(grad_sign_old(i).g).*(grad_sign_new(i).g);
sign_b=(grad_sign_old(i).b).*(grad_sign_new(i).b);
for i_2=1:length(sign_g)
if sign_g(i_2)>=0
step_new(i).g(i_2)=step_old(i).g(i_2)*eta_plus;
else
step_new(i).g(i_2)=step_old(i).g(i_2)*eta_minus;
end
end
for i_2=1:length(sign_b)
if sign_b(i_2)>=0
step_new(i).b(i_2)=step_old(i).b(i_2)*eta_plus;
else
step_new(i).b(i_2)=step_old(i).b(i_2)*eta_minus;
end
end
end
end
%%% function to update weight using Rprop
function w_new=f_Rprop_update_weight(w_old,step_w,grad_sign,direction)
% Input:
% w_old--old weight, same structure as w.
% step_w--step size, same structure as w.
% grad_sign--sign of gradient, same structure as w.
% direction--+1 for gradient ascending, -1 for descending.
% Output:
% w_new--updated weight, same structure as w.
% Structure of w:
% w(i).line--a 2-element vector containing the vertices of the line. Length
% of w is the number of lines.
% w(i).g--a vector of conductance of line i, contain 6 elements.
% w(i).b--a vector of susceptance of line i, contain 6 elements.
% The 6 elements are in the order of phase aa, ab, ac, bb, bc, cc.
w_new=w_old;
for i=1:length(w_new)
w_new(i).g=w_old(i).g+(grad_sign(i).g).*(step_w(i).g)*direction;
w_new(i).b=w_old(i).b+(grad_sign(i).b).*(step_w(i).b)*direction;
end
end
%% Define functions for forward and other functions.
%%% calculate the primitive admittance matrix from a line's paramters
function Y=f_Ynk_Matrix(g,b)
% Input:
% g--a vector of conductance of a line, contain 6 elements.
% b--a vector of conductance of a line, contain 6 elements.
% the elements are in the order of phase aa, ab, ac, bb, bc, cc
% Output:
% Y-- a complex primitive Y matrix, it is 3-by-3.
m=g;
Lambda_g=[m(1),m(2),m(3); ...
m(2),m(4),m(5); ...
m(3),m(5),m(6)];
m=b;
Lambda_b=[m(1),m(2),m(3); ...
m(2),m(4),m(5); ...
m(3),m(5),m(6)];
Y=-Lambda_g-1i*Lambda_b;
end
%%% function to calculate rotation matrix for L_num lines (designed 2020-03-30)
function R=Rotate_phi_fun(L_num,power_sign)
% input:
% L_num: number of lines.
% power_sign: +1 or -1
% output:
% R: rotation matrix used to transform the G,B to linearized power flow
% model.
if power_sign==1
v1=zeros(1,L_num);
v2=-2*pi/3*ones(1,L_num);
v3=2*pi/3*ones(1,L_num);
elseif power_sign==-1
v1=zeros(1,L_num);
v2=2*pi/3*ones(1,L_num);
v3=-2*pi/3*ones(1,L_num);
else
disp('wrong power_sign')
return
end
angle_v=[v1,v2,v3];
M_cos=diag(cos(angle_v));
M_sin=diag(sin(angle_v));
R=[M_cos,M_sin;-M_sin,M_cos];
end
%%% function to calculate the MSE of the differences between two state
%%% structures.
function MSE=f_state_update_MSE(new_state,old_state)
% Input:
% new_state--input state structure containing the nodal volt magnitude and
% angle.
% old_state--another state structure to compare with, the same structure as
% new_state.
% Output:
% MSE--the mean square error.
% Structure of new_state:
% structure containing the nodal volt magnitude and
% angle. Organized in a 6N x T big matrix. Each 6 rows represent a node's
% volt magnitude and angle. It contains 2 field, the node_list, and magrad_mat.
T=size(new_state.magrad_mat,2); % number of timestamps
N_6=size(new_state.magrad_mat,1);
temp_sum=norm(new_state.magrad_mat-old_state.magrad_mat,'fro')^2;
MSE=temp_sum/(T*N_6);
end
%%% function to calculate the Mean square value of the state structures.
function MSE=f_state_MSE(in_state)
% Input:
% in_state--input state structure containing the nodal volt magnitude and
% angle.
% Output:
% MSE--the mean square value.
% Structure of in_state:
% structure containing the nodal volt magnitude and
% angle. Organized in a 6N x T big matrix. Each 6 rows represent a node's
% volt magnitude and angle. It contains 2 field, the node_list, and magrad_mat.
T=size(in_state.magrad_mat,2); % number of timestamps
N_6=size(in_state.magrad_mat,1); % number of timestamps
temp_sum=norm(in_state.magrad_mat,'fro')^2;
MSE=temp_sum/(T*N_6);
end
%%% A global transistion function, which is a stack of all local transition
%%% function
function out_state_compact=f_global_transition(in_state_compact,Model,label)
% Input:
% in_state_compact--input state structure containing the nodal volt real
% and imaginary part. Of compact state structure
% Model--the structure that stores the needed A1 matrix and A2 matrics for
% each node's local transition function.
% label--label structure containing the nodal real and reactive power
% injection.
% Output:
% out_state_compact--output state structure containing the nodal volt real
% and imaginary part. Of compact state structure.
% Structur of the state_compact
% state_compact.node_list--list of node (N node)
% state_compact.scalor_struct= a N-by-1 structure array,
% state_compact.scalor_struct(i).scalor is a 6-by-T matrix of nodal volt real
% and imaginary part of node i of all time through T.
% state_compact.complex_struct= a N-by-1 structure array,
% state_compact.conj_struct(i).conj is a 3-by-T matrix of nodal conjugate complex volt
% of node i of all time through T.
% Structure of the Model:
% Model(i).node--node name of node i.
% Model(i).neighbor--vector of node i's neighbors.
% Model(i).rZ_nn--Z_nn real matrix for node i's local transition function
% (6-by-6).
% Model(i).rY_nk--structure of Y_nk real matrix for node i's local transition function.
% Model(i).rY_nk(j).M--the Y_nk real matrix corresponding to node ji's
% neighbor node j (6-by-6).
% Structure of label:
% label(i).node--name of node i.
% label(i).P--a 3-by-T matrix of nodal real power of node i of all
% time through T.