-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathActiveSetUpdate.m
executable file
·326 lines (226 loc) · 13 KB
/
ActiveSetUpdate.m
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
function [UserVar,RunInfo,BCs1,lambdahpos,isActiveSetModified,isActiveSetCyclical,Activated,DeActivated]=ActiveSetUpdate(UserVar,RunInfo,CtrlVar,MUA,F1,l1,BCs1,iActiveSetIteration,LastReleased,LastActivated)
narginchk(10,10)
nargoutchk(8,8)
RunInfo.Forward.ActiveSetConverged=1;
RunInfo.Forward.uvhIterationsTotal=0;
isActiveSetCyclical=NaN;
BCs1Input=BCs1;
if ~CtrlVar.ThicknessConstraints
return
end
% Thickness constraints used
% NodesFixed: holds the nodal numbers nodes in the active set
% ihactive: number nodes in active set
%
% -If min thickness is significantly greater than CtrlVar.ThickMin, eliminate all thickness constraints
% -If there are no thickness constraints, but some thicknesses (h1) are smaller than CtrlVar.ThickMin, introduce a new initial active set
% and set velocity estimates to zero, and thickness to ThickMin at nodes in the active set
% Enter loop:
% -Where h0 is at ThickMin set h1 to ThickMin and u1,v1 to u0, v0. This modification is important for convergence
% -Solve system by calling uvh2D
% -If solution did not converge use an inner loop that tries to reset initial estimates for u1,v1 and h1.
% -Based on sign of the Lagrange multipliers, take nodes out of active set
% -Where h1<=ThickMin add those to active set, provided 1) they are not already in the active set and 2) they are to be taken out
%
%
% active set method is used to enforce thickness constraints
% new approach: just keep track of new hFixedNode
%
% have new fields in object BoundaryConditions
% hPosNode
% hPosValue
%% Make sure that no thickness constraints have been added to nodes with user-defined boundary conditions
BCs1.hPosNode=setdiff(BCs1.hPosNode,BCs1.hFixedNode) ;
%%
if CtrlVar.ThicknessConstraintsInfoLevel>=1
if numel(BCs1.hPosNode)>0 || CtrlVar.ThicknessConstraintsInfoLevel>=10
fprintf(CtrlVar.fidlog,' Number of active thickness constraints is %-i \n',numel(BCs1.hPosNode));
end
end
%% Here we begin: I have an active set already, now need to update it
% keep a copy of the old active set
LastActiveSet=BCs1.hPosNode;
%must now find the lambda values corresponding to nodes that where constrained to positive thickness
% hLambda=l1.h;
% lambdahpos=hLambda(numel(BCs1.hFixedNode)+numel(BCs1.hTiedNodeA)+1:end) ;% I always put the hPos constraints at end of all other h constraints
lambdahpos=l1.h(numel(BCs1.hFixedNode)+numel(BCs1.hTiedNodeA)+1:end) ;% I always put the hPos constraints at end of all other h constraints
% then this will work
%% Mapping into 'physical' nodal basis if required
% remember lambdahpos=hLambda(numel(BCs1.hFixedNode)+numel(BCs1.hTiedNodeA)+1:end)
% actually most likely only need to do this if numel(hPosNode)>0
% If the L matrix was not in the FE basis, I simply calculate the reactions.
% The Reactions are calculated correctly irrespective of how the
%
if ~CtrlVar.LinFEbasis
if numel(BCs1.hPosNode) >0
Reactions=CalculateReactions(CtrlVar,MUA,BCs1,l1);
ah=-Reactions.h./(F1.rho.*F1.dt) ;
ah=ah(BCs1.hPosNode);
lambdahpos=Reactions.h(BCs1.hPosNode);
else
lambdahpos=[];
ah=[];
end
end
%%
if numel(lambdahpos) ~= numel(BCs1.hPosNode)
save TestSave ; error(' # of elements in lambdahpos must equal # of elements in BCs1.hPosNode')
end
%% Print information the solution for the Lagrange multipliers
if CtrlVar.ThicknessConstraintsInfoLevel>=10
[~,I]=sort(lambdahpos); % print out fixed nodes in the order of increasing lambda values
fprintf(CtrlVar.fidlog,' Pos. thick. contraints: ') ;
fprintf(CtrlVar.fidlog,' \t %9i \t %9i \t %9i \t %9i \t %9i \t %9i \t %9i \t %9i \t %9i \t %9i \n \t \t \t \t \t \t',BCs1.hPosNode(I));
fprintf(CtrlVar.fidlog,'\n Lagrange multipliers: ') ;
fprintf(CtrlVar.fidlog,' \t %+9.0g \t %+9.0g \t %+9.0g \t %+9.0g \t %+9.0g \t %+9.0g \t %+9.0g \t %+9.0g \t %+9.0f \t %+9.0f \n \t \t \t \t \t \t',lambdahpos(I)) ;
fprintf(CtrlVar.fidlog,'\n mass flux: ') ;
fprintf(CtrlVar.fidlog,' \t %+9.0f \t %+9.0f \t %+9.0f \t %+9.0f \t %+9.0f \t %+9.0f \t %+9.0f \t %+9.0f \t %+9.0f \t %+9.0f \n \t \t \t \t \t \t',ah(I)) ;
fprintf(CtrlVar.fidlog,'\n');
end
%% Updating the active set
% Now I've solved for h1 and if needed a new active set must be defined
%
% The new active set contains all nodes where h1 less than hmin that were not in the previous active set
% Those of the nodes in the previous active set with positive slack values
% Nodes in the previous set with negative slack values must be taken out of the set
% if the active-set method is selected, update active set
% The active set is created/modified and the problem solved again if the active set has changed
% Do I need to in-activate some thickness constraints?
% if any of the lambdahpos are positive, then these constraints must be in-activated
if numel(BCs1.hPosNode)>0 % are there any thickness constraints? If so see if some should be in-activated
% I divide here with rho and dt for this to have the same units as the mass balance
% (distance/time)
%
% isNegavtiveMassFluxSmall=lambdahpos>CtrlVar.ThicknessConstraintsLambdaPosThreshold./(F1.rho(BCs1.hPosNode).*F1.dt); % if any of the Lagrange multipliers `lambdahpos' are positive, then these should be in-activated
% Clearly only inactivate if the mass flux needed to keep them active (ah) is negative.
% But to also consider only inactivate if the negative flux is
%
% ah < 0.01 hMin /dt
%
% that is, if one were to stop subtracting this mass balance, then the thickness would increase to 0.01 above the minimum
% thickness value over a time interval corresponding to one time unit.
isNegavtiveMassFluxSmall=ah < -0.001*CtrlVar.ThickMin/F1.dt ;
NewInActiveConstraints=find(isNegavtiveMassFluxSmall);
iNewInActiveConstraints=numel(NewInActiveConstraints);
if iNewInActiveConstraints>0 % have any become inactive?
BCs1.hPosNode(isNegavtiveMassFluxSmall)=[];
end
else
NewInActiveConstraints=[];
iNewInActiveConstraints=numel(NewInActiveConstraints);
end
NodesReleased=LastActiveSet(NewInActiveConstraints);
% Do I need to activate some new thickness constraints?
%I=h1<=CtrlVar.ThickMin; % if thickness is less than ThickMin then further new thickness constraints must be introduced
I=F1.h<CtrlVar.ThickMin; % if thickness is less than ThickMin then further new thickness constraints must be introduced
NewActive=find(I);
NewActive=setdiff(NewActive,BCs1.hFixedNode) ; % do not add active thickness constraints for nodes that already are included in the user-defined thickness boundary conditions,
% even if this means that thicknesses at those nodes are less then MinThick
NewActive=setdiff(NewActive,BCs1.hPosNode); % exclude those already in the active set
NewActive=setdiff(NewActive,NodesReleased); % do not include those nodes at min thick that I now must release
iNewActiveConstraints=numel(NewActive);
if iNewActiveConstraints> CtrlVar.MaxNumberOfNewlyIntroducedActiveThicknessConstraints
if CtrlVar.ThicknessConstraintsInfoLevel>=1
fprintf(CtrlVar.fidlog,' Number of new active-set thickness constraints %-i larger then max number or newly added constraints %-i \n ',...
iNewActiveConstraints,CtrlVar.MaxNumberOfNewlyIntroducedActiveThicknessConstraints);
fprintf(CtrlVar.fidlog,' Only the smallest %-i thickness values are constrained \n',CtrlVar.MaxNumberOfNewlyIntroducedActiveThicknessConstraints);
end
[~,II]=sort(F1.h);
NewActive=II(1:CtrlVar.MaxNumberOfNewlyIntroducedActiveThicknessConstraints);
iNewActiveConstraints=CtrlVar.MaxNumberOfNewlyIntroducedActiveThicknessConstraints;
end
if iNewActiveConstraints>0
if CtrlVar.ThicknessConstraintsInfoLevel>=1
fprintf(CtrlVar.fidlog,' %i new active constraints \n',iNewActiveConstraints);
end
BCs1.hPosNode=[BCs1.hPosNode;NewActive] ;
end
%% Consider adding LSF F1.LSFMask.NodesOut to thickness constraints
if CtrlVar.LevelSetMethod && CtrlVar.LevelSetMethodThicknessConstraints
if isempty(F1.LSFMask) %
F1.LSFMask=CalcMeshMask(CtrlVar,MUA,F1.LSF,0);
end
LSFhPosNode=find(F1.LSFMask.NodesOut);
LSFhAdditionalPosNodes=setdiff(LSFhPosNode,BCs1.hPosNode) ;
if CtrlVar.ThicknessConstraintsInfoLevel>=1
fprintf(' %i additional LSF active constraints \n',numel(LSFhAdditionalPosNodes))
end
BCs1.hPosNode=union(BCs1.hPosNode,LSFhPosNode);
end
% LastActiveSet is simply a copy of hPosNode at the beginning of the call
DeActivated=setdiff(LastActiveSet,BCs1.hPosNode) ; % nodes in last active set that are no longer in the new one
Activated=setdiff(BCs1.hPosNode,LastActiveSet) ; % nodes in new active set that were not in the previous one
nReleased=numel(DeActivated);
nActivated=numel(Activated);
BCs1.hPosNodeDeActivated=DeActivated; % I'm not really using this at the moment, but in the future it might be best to use this to determine if set has become cyclical
BCs1.hPosNodeActivated=Activated;
%% print information on new active set
if CtrlVar.ThicknessConstraintsInfoLevel>=1
if nReleased > 0 || nActivated > 0
fprintf(CtrlVar.fidlog,'\n Updating pos. thickness constraints: in-activated: %-i, activated: %-i, total number of thickness constrains: %-i \n',...
nReleased,nActivated,numel(BCs1.hPosNode));
fprintf(CtrlVar.fidlog,' Nodes inactivated: ') ;
fprintf(CtrlVar.fidlog,' \t %7i \t %7i \t %7i \t %7i \t %7i \t %7i \t %7i \t %7i \t %7i \t %7i \n \t \t \t \t \t',DeActivated);
fprintf(CtrlVar.fidlog,'\n Nodes activated: ') ;
fprintf(CtrlVar.fidlog,' \t %7i \t %7i \t %7i \t %7i \t %7i \t %7i \t %7i \t %7i \t %7i \t %7i \n \t \t \t \t \t',Activated);
fprintf(CtrlVar.fidlog,'\n ') ;
else
fprintf(CtrlVar.fidlog,'No pos.-thickness constraints activated or deactivated. \n') ;
end
end
%% check if set has become cyclical
%LastReleased=Released; LastActivated=Activated;
isActiveSetCyclical=false ;
ActivatedAndPreviouslyReleasedDifference=setxor(Activated,LastReleased); % if empty then the sets of activated and previously de-activated nodes are identical
ReleasedAndPreviouslyActivatedDifference=setxor(DeActivated,LastActivated); % if empty then the sets of de-activated and previously activated nodes are identical
if CtrlVar.ThicknessConstraintsInfoLevel>=1
if ~isempty(LastReleased)
if isempty(ActivatedAndPreviouslyReleasedDifference)
fprintf(' Active-set: The set of nodes being activated is identical to the one previously in-activated. \n')
end
end
if ~isempty(LastActivated)
if isempty(ReleasedAndPreviouslyActivatedDifference)
fprintf(' Active-set: The set of nodes being in-activated is identical to the one previously activated. \n')
end
end
end
if ~(isempty(Activated) && isempty(DeActivated))
if isempty(ActivatedAndPreviouslyReleasedDifference) && isempty(ReleasedAndPreviouslyActivatedDifference)
isActiveSetCyclical=true ;
fprintf(' Active-set is cyclical. \n')
end
end
% I now have a dilemma, since the set has become cyclical it is
% clear that if I deactivate any new nodes the thickness at
% those nodes will become too small in the next active-set
% interaction. A solution is simply not to deactivate and to add
% the cyclically deactivated nodes to the active set.
if isActiveSetCyclical
BCs1.hPosNode=[BCs1.hPosNode;DeActivated] ;
end
%% Set the hPosValues, only need to do this once at the end
BCs1.hPosValue=BCs1.hPosNode*0+CtrlVar.ThickMin;
%%
if nReleased> 0 || nActivated>0
if nReleased<CtrlVar.MinNumberOfNewlyIntroducedActiveThicknessConstraints && nActivated<CtrlVar.MinNumberOfNewlyIntroducedActiveThicknessConstraints
fprintf("ActiveSetInitialisation: Not introducing any new thickness constraints as:\n")
fprintf("\t #released=%i and #activated=%i nodes, both less than CtrlVar.MinNumberOfNewlyIntroducedActiveThicknessConstraints=%i. \n",nReleased,nActivated,CtrlVar.MinNumberOfNewlyIntroducedActiveThicknessConstraints)
BCs1=BCs1Input;
Activated=[];
DeActivated=[];
end
end
ChangeInActiveSet=setxor(BCs1.hPosNode,LastActiveSet) ;
nChangeInActiveSet=numel(ChangeInActiveSet);
if nChangeInActiveSet == 0 || isActiveSetCyclical
isActiveSetModified=false;
fprintf("ActiveSetUpdate: Active set not modified.\n")
else
isActiveSetModified=true;
fprintf("ActiveSetUpdate: Active set modified.\n")
end
RunInfo.Forward.uvhActiveSetIterations(CtrlVar.CurrentRunStepNumber)=iActiveSetIteration-1 ;
RunInfo.Forward.uvhActiveSetCyclical(CtrlVar.CurrentRunStepNumber)=isActiveSetCyclical;
RunInfo.Forward.uvhActiveSetConstraints(CtrlVar.CurrentRunStepNumber)=numel(BCs1.hPosNode);
end