@@ -126,7 +126,7 @@ private double[] calculatePostContingencyStates(DcLoadFlowContext loadFlowContex
126126 */
127127 private double [] calculatePostContingencyAndOperatorStrategyStates (DcLoadFlowContext loadFlowContext , DenseMatrix contingenciesStates , double [] flowStates ,
128128 ConnectivityAnalysisResult connectivityAnalysisResult , Map <String , ComputedContingencyElement > contingencyElementByBranch ,
129- Map <LfAction , ComputedElement > actionElementByLfAction , DenseMatrix actionsStates , ReportNode reportNode ) {
129+ Map <LfAction , List < ComputedElement > > actionElementByLfAction , DenseMatrix actionsStates , ReportNode reportNode ) {
130130 PropagatedContingency contingency = connectivityAnalysisResult .getPropagatedContingency ();
131131 Set <LfBus > disabledBuses = connectivityAnalysisResult .getDisabledBuses ();
132132 Set <LfBranch > partialDisabledBranches = connectivityAnalysisResult .getPartialDisabledBranches ();
@@ -145,6 +145,7 @@ private double[] calculatePostContingencyAndOperatorStrategyStates(DcLoadFlowCon
145145 .collect (Collectors .toList ());
146146 List <ComputedElement > actionElements = operatorStrategyLfActions .stream ()
147147 .map (actionElementByLfAction ::get )
148+ .flatMap (Collection ::stream )
148149 .filter (actionElement -> !elementsToReconnect .contains (actionElement .getLfBranch ().getId ()))
149150 .collect (Collectors .toList ());
150151
@@ -365,29 +366,31 @@ private void addPostContingencyAndOperatorStrategyResults(WoodburyContext woodbu
365366 });
366367 }
367368
368- private static Map <LfAction , ComputedElement > createActionElementsIndexByLfAction (Map <String , LfAction > lfActionById , EquationSystem <DcVariableType , DcEquationType > equationSystem ) {
369- Map <LfAction , ComputedElement > computedElements = lfActionById .values ().stream ()
370- .map (lfAction -> {
371- ComputedElement element ;
372- if (lfAction instanceof AbstractLfTapChangerAction <?> abstractLfTapChangerAction ) {
373- element = new ComputedTapPositionChangeElement (abstractLfTapChangerAction .getChange (), equationSystem );
374- } else if (lfAction instanceof AbstractLfBranchAction <?> abstractLfBranchAction && !abstractLfBranchAction .getEnabledBranches ().isEmpty ()) {
375- element = new ComputedSwitchBranchElement (abstractLfBranchAction .getEnabledBranches ().getFirst (), true , equationSystem );
376- } else if (lfAction instanceof AbstractLfBranchAction <?> abstractLfBranchAction && !abstractLfBranchAction .getDisabledBranches ().isEmpty ()) {
377- element = new ComputedSwitchBranchElement (abstractLfBranchAction .getDisabledBranches ().getFirst (), false , equationSystem );
378- } else {
379- throw new IllegalStateException ("Only tap position change and branch enabling/disabling are supported in WoodburyDcSecurityAnalysis" );
380- }
381- return Map .entry (lfAction , element );
382- })
383- .filter (e -> e .getValue ().getLfBranchEquation () != null )
384- .collect (Collectors .toMap (
385- Map .Entry ::getKey ,
386- Map .Entry ::getValue ,
387- (existing , replacement ) -> existing ,
388- LinkedHashMap ::new
389- ));
390- ComputedElement .setComputedElementIndexes (computedElements .values ());
369+ private static Map <LfAction , List <ComputedElement >> createActionElementsIndexByLfAction (Map <String , LfAction > lfActionById , EquationSystem <DcVariableType , DcEquationType > equationSystem ) {
370+ Map <LfAction , List <ComputedElement >> computedElements = new HashMap <>();
371+ lfActionById .values ().forEach (lfAction -> {
372+ List <ComputedElement > elements = new ArrayList <>();
373+ switch (lfAction ) {
374+ case AbstractLfTapChangerAction <?> abstractLfTapChangerAction -> {
375+ elements .add (new ComputedTapPositionChangeElement (abstractLfTapChangerAction .getChange (), equationSystem ));
376+ }
377+ case
378+ AbstractLfBranchAction <?> abstractLfBranchAction when !abstractLfBranchAction .getEnabledBranches ().isEmpty () -> {
379+ elements .addAll (abstractLfBranchAction .getEnabledBranches ().stream ().map (
380+ e -> new ComputedSwitchBranchElement (e , true , equationSystem )).toList ());
381+ }
382+ case
383+ AbstractLfBranchAction <?> abstractLfBranchAction when !abstractLfBranchAction .getDisabledBranches ().isEmpty () -> {
384+ elements .addAll (abstractLfBranchAction .getDisabledBranches ().stream ().map (
385+ e -> new ComputedSwitchBranchElement (e , false , equationSystem )).toList ());
386+ }
387+ default -> {
388+ throw new IllegalStateException ("Only tap position change and branch enabling/disabling are supported in WoodburyDcSecurityAnalysis" );
389+ }
390+ }
391+ computedElements .put (lfAction , elements .stream ().filter (e -> e .getLfBranchEquation () != null ).toList ());
392+ });
393+ ComputedElement .setComputedElementIndexes (computedElements .values ().stream ().flatMap (Collection ::stream ).toList ());
391394 return computedElements ;
392395 }
393396
@@ -449,11 +452,11 @@ protected SecurityAnalysisResult runSimulations(LfNetwork lfNetwork, List<Propag
449452 ConnectivityBreakAnalysis .ConnectivityBreakAnalysisResults connectivityBreakAnalysisResults = ConnectivityBreakAnalysis .run (context , propagatedContingencies );
450453
451454 // the map is indexed by lf actions as different kind of actions can be given on the same branch
452- Map <LfAction , ComputedElement > actionElementsIndexByLfAction = createActionElementsIndexByLfAction (lfActionById , context .getEquationSystem ());
455+ Map <LfAction , List < ComputedElement > > actionElementsIndexByLfAction = createActionElementsIndexByLfAction (lfActionById , context .getEquationSystem ());
453456
454457 // compute states with +1 -1 to model the actions in Woodbury engine
455458 // note that the number of columns in the matrix depends on the number of distinct branches affected by the action elements
456- DenseMatrix actionsStates = ComputedElement .calculateElementsStates (context , actionElementsIndexByLfAction .values ());
459+ DenseMatrix actionsStates = ComputedElement .calculateElementsStates (context , actionElementsIndexByLfAction .values (). stream (). flatMap ( Collection :: stream ). toList () );
457460
458461 // save base state for later restoration after each contingency/action
459462 NetworkState networkState = NetworkState .save (lfNetwork );
0 commit comments