Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/elmerice/Solvers/AdjointSSA/AdjointSSA_CostContSolver.F90
3206 views
1
!/*****************************************************************************/
2
! *
3
! * Elmer/Ice, a glaciological add-on to Elmer
4
! * http://elmerice.elmerfem.org
5
! *
6
! *
7
! * This program is free software; you can redistribute it and/or
8
! * modify it under the terms of the GNU General Public License
9
! * as published by the Free Software Foundation; either version 2
10
! * of the License, or (at your option) any later version.
11
! *
12
! * This program is distributed in the hope that it will be useful,
13
! * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
! * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
! * GNU General Public License for more details.
16
! *
17
! * You should have received a copy of the GNU General Public License
18
! * along with this program (in file fem/GPL-2); if not, write to the
19
! * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20
! * Boston, MA 02110-1301, USA.
21
! *
22
! *****************************************************************************/
23
! ******************************************************************************
24
! *
25
! * Authors: f. Gillet-Chaulet (LGGE, Grenoble,France)
26
! * Email: [email protected]
27
! * Web: http://elmerice.elmerfem.org
28
! *
29
! * Original Date:
30
! *
31
! *****************************************************************************
32
!
33
! *****************************************************************************
34
SUBROUTINE AdjointSSA_CostContSolver( Model,Solver,dt,TransientSimulation )
35
!------------------------------------------------------------------------------
36
!Compute the Cost function of the SSA Adjoint inverse Problem as: Integral Node_Cost ds
37
!
38
! Serial/Parallel 2D/3D
39
!
40
! OUTPUT are : J and DJDu (==Velocityb variable used as forcing of the SSA adjoint problem)
41
!
42
! !! Be careful this solver will reset the cost and DJDu to 0; so it has to
43
! be used as the first cost solver if regularistaion of flux divergence cost
44
! solvers are used in the simulation!!
45
!
46
!
47
! INPUT PARAMETERS are:
48
!
49
! In solver section:
50
! Problem Dimension = Integer (default = Coordinate system dimension)
51
! Cost Filename = File (default = 'CostOfT.dat')
52
! Cost Variable Name = String (default= 'CostValue')
53
!
54
! Variables
55
! Velocityb (forcing for the adjoint pb;DOFs== Pb dimension)
56
!
57
! In body Forces:
58
! 'Adjoint Cost = Real ...' : The nodal value of the cost
59
! 'Adjoint Cost der 1 = Real ...' : The derivative of 'Adjoint Cost' w.r.t. u-velocity
60
! 'Adjoint Cost der 2 = Real ...' : The derivative of 'Adjoint Cost' w.r.t. v-velocity
61
!
62
!******************************************************************************
63
USE DefUtils
64
IMPLICIT NONE
65
!------------------------------------------------------------------------------
66
TYPE(Solver_t) :: Solver
67
TYPE(Model_t) :: Model
68
69
REAL(KIND=dp) :: dt
70
LOGICAL :: TransientSimulation
71
!
72
CHARACTER(LEN=MAX_NAME_LEN), PARAMETER :: DefaultCostFile = 'CostOfT.dat'
73
CHARACTER(LEN=MAX_NAME_LEN) :: SolverName='CostSolver_Adjoint'
74
CHARACTER(LEN=MAX_NAME_LEN) :: CostFile
75
CHARACTER(LEN=MAX_NAME_LEN) :: CostSolName
76
TYPE(Element_t),POINTER :: Element
77
TYPE(Variable_t), POINTER :: TimeVar,CostVar
78
TYPE(Variable_t), POINTER :: VelocitybSol
79
TYPE(ValueList_t), POINTER :: SolverParams,BodyForce
80
TYPE(Nodes_t) :: ElementNodes
81
TYPE(GaussIntegrationPoints_t) :: IntegStuff
82
REAL(KIND=dp), POINTER :: Vb(:)
83
INTEGER, POINTER :: NodeIndexes(:)
84
INTEGER, POINTER :: VbPerm(:)
85
Logical :: Firsttime=.true.,Found,Parallel,stat,Gotit
86
integer :: i,j,k,l,t,n,NMAX,DIM,ierr,c
87
real(kind=dp) :: Cost,Cost_surf,Cost_S
88
real(kind=dp) :: u,v,w,s,coeff,SqrtElementMetric,x
89
REAL(KIND=dp) :: NodeCost(Model % MaxElementNodes)
90
REAL(KIND=dp) :: Basis(Model % MaxElementNodes), dBasisdx(Model % MaxElementNodes,3)
91
REAL(KIND=dp) :: NodeCostb(Model % MaxElementNodes),NodeCost_der(3,Model %MaxElementNodes)
92
CHARACTER*10 :: date,temps
93
94
save Firsttime,Parallel
95
save SolverName,CostSolName,CostFile
96
save ElementNodes
97
98
CALL Info(SolverName,'***********************',level=0)
99
CALL Info(SolverName,' This solver has been replaced by:',level=0)
100
CALL Info(SolverName,' Adjoint_CostContSolver ',level=0)
101
CALL Info(SolverName,' See documentation under: ',level=0)
102
CALL Info(SolverName,' elmerice/Solvers/Documentation ',level=0)
103
CALL Info(SolverName,'***********************',level=0)
104
CALL FATAL(SolverName,' Use new solver !!')
105
106
SolverParams => GetSolverParams()
107
DIM=GetInteger(SolverParams ,'Problem Dimension',Found)
108
If (.NOT.Found) then
109
CALL WARN(SolverName,'Keyword >Problem Dimension< not found, assume DIM = CoordinateSystemDimension()')
110
DIM = CoordinateSystemDimension()
111
Endif
112
113
If (Firsttime) then
114
N = model % MaxElementNodes
115
allocate(ElementNodes % x(N), ElementNodes % y(N), ElementNodes % z(N))
116
117
!!!!!!! Check for parallel run
118
Parallel = .FALSE.
119
IF ( ASSOCIATED( Solver % Matrix % ParMatrix ) ) THEN
120
IF ( Solver % Matrix % ParMatrix % ParEnv % PEs > 1 ) THEN
121
Parallel = .TRUE.
122
END IF
123
END IF
124
125
WRITE(SolverName, '(A)') 'CostSolver_Adjoint'
126
127
!!!!!!!!!!! get Solver Variables
128
129
CostFile = ListGetString(Solver % Values,'Cost Filename',Found )
130
IF (.NOT. Found) CostFile = DefaultCostFile
131
CALL DATE_AND_TIME(date,temps)
132
If (Parallel) then
133
if (ParEnv % MyPe.EQ.0) then
134
OPEN (12, FILE=CostFile)
135
write(12,1000) date(5:6),date(7:8),date(1:4),temps(1:2),temps(3:4),temps(5:6)
136
write(12,'(A)') '#, 1.0'
137
write(12,'(A)') '# iter, J0'
138
CLOSE(12)
139
End if
140
Else
141
OPEN (12, FILE=CostFile)
142
write(12,1000) date(5:6),date(7:8),date(1:4),temps(1:2),temps(3:4),temps(5:6)
143
write(12,'(A)') '#, 1.0'
144
write(12,'(A)') '# iter, J0'
145
CLOSE(12)
146
End if
147
148
CostSolName = GetString( SolverParams,'Cost Variable Name', Found)
149
IF(.NOT.Found) THEN
150
CALL WARN(SolverName,'Keyword >Cost Variable Name< not found in section >Solver<')
151
CALL WARN(SolverName,'Taking default value >CostValue<')
152
WRITE(CostSolName,'(A)') 'CostValue'
153
END IF
154
155
!!! End of First visit
156
Firsttime=.false.
157
Endif
158
159
160
VelocitybSol => VariableGet( Solver % Mesh % Variables, 'Velocityb' )
161
IF ( ASSOCIATED( VelocitybSol ) ) THEN
162
Vb => VelocitybSol % Values
163
VbPerm => VelocitybSol % Perm
164
ELSE
165
WRITE(Message,'(A)') &
166
'No variable > Velocityb < found'
167
CALL FATAL(SolverName,Message)
168
END IF
169
c=DIM ! size of the velocity variable
170
IF (VelocitybSol % DOFs.NE.c) then
171
WRITE(Message,'(A,I1,A,I1)') &
172
'Variable Velocityb has ',VelocitybSol % DOFs,' DOFs, should be',c
173
CALL FATAL(SolverName,Message)
174
End If
175
Vb=0.0_dp
176
177
178
Cost=0._dp
179
Cost_surf=0._dp
180
181
DO t=1,Solver % NumberOfActiveElements
182
Element => GetActiveElement(t)
183
IF (ParEnv % myPe .NE. Element % partIndex) CYCLE
184
n = GetElementNOFNodes()
185
186
NodeIndexes => Element % NodeIndexes
187
188
! set coords of highest occurring dimension to zero (to get correct path element)
189
!-------------------------------------------------------------------------------
190
ElementNodes % x(1:n) = Solver % Mesh % Nodes % x(NodeIndexes)
191
IF (DIM == 1) THEN !1D SSA
192
ElementNodes % y(1:n) = 0.0_dp
193
ElementNodes % z(1:n) = 0.0_dp
194
ELSE IF (DIM == 2) THEN !2D SSA
195
ElementNodes % y(1:n) = Solver % Mesh % Nodes % y(NodeIndexes)
196
ElementNodes % z(1:n) = 0.0_dp
197
ELSE
198
WRITE(Message,'(a,i1,a)')&
199
'It is not possible to compute SSA problems with DOFs=',&
200
DIM, ' . Aborting'
201
CALL Fatal( SolverName, Message)
202
STOP
203
END IF
204
205
BodyForce => GetBodyForce()
206
207
NodeCost=0.0_dp
208
NodeCost(1:n) = ListGetReal( BodyForce, 'Adjoint Cost', n, NodeIndexes, GotIt)
209
IF (.NOT.GotIt) Then
210
WRITE(Message,'(A)') &
211
'No variable >Adjoint Cost< found in "Body Forces"'
212
CALL FATAL(SolverName,Message)
213
END IF
214
NodeCost_der=0.0_dp
215
216
NodeCost_der(1,1:n)=ListGetReal( BodyForce, 'Adjoint Cost der 1', n, NodeIndexes, GotIt)
217
NodeCost_der(2,1:n)=ListGetReal( BodyForce, 'Adjoint Cost der 2', n, NodeIndexes, GotIt)
218
219
!------------------------------------------------------------------------------
220
! Numerical integration
221
!------------------------------------------------------------------------------
222
IntegStuff = GaussPoints( Element )
223
224
225
NodeCostb=0.0_dp
226
227
DO i=1,IntegStuff % n
228
U = IntegStuff % u(i)
229
V = IntegStuff % v(i)
230
W = IntegStuff % w(i)
231
!------------------------------------------------------------------------------
232
! Basis function values & derivatives at the integration point
233
!------------------------------------------------------------------------------
234
stat = ElementInfo( Element,ElementNodes,U,V,W,SqrtElementMetric, &
235
Basis,dBasisdx )
236
237
x = SUM( ElementNodes % x(1:n) * Basis(1:n) )
238
s = 1.0d0
239
240
IF ( CurrentCoordinateSystem() /= Cartesian ) THEN
241
s = 2.0d0 * PI * x
242
END IF
243
s = s * SqrtElementMetric * IntegStuff % s(i)
244
245
coeff = SUM(NodeCost(1:n) * Basis(1:n))
246
Cost_surf=Cost_surf+coeff*s
247
NodeCostb(1:n)=NodeCostb(1:n) + s*Basis(1:n)
248
249
End do
250
251
c=DIM ! size of the velocity variable
252
Do j=1,n
253
Do i=1,DIM
254
k=(VbPerm(NodeIndexes(j))-1)*c+i
255
Vb(k)=Vb(k)+NodeCostb(j)*NodeCost_der(i,j)
256
End Do
257
End Do
258
End do
259
260
Cost=Cost_surf
261
262
TimeVar => VariableGet( Solver % Mesh % Variables, 'Time' )
263
264
IF (Parallel) THEN
265
CALL MPI_ALLREDUCE(Cost,Cost_S,1,&
266
MPI_DOUBLE_PRECISION,MPI_SUM,ELMER_COMM_WORLD,ierr)
267
CostVar => VariableGet( Solver % Mesh % Variables, CostSolName )
268
IF (ASSOCIATED(CostVar)) THEN
269
CostVar % Values(1)=Cost_S
270
END IF
271
IF (Solver % Matrix % ParMatrix % ParEnv % MyPE == 0) then
272
OPEN (12, FILE=CostFile,POSITION='APPEND')
273
write(12,'(e13.5,2x,e15.8)') TimeVar % Values(1),Cost_S
274
CLOSE(12)
275
End if
276
ELSE
277
CostVar => VariableGet( Solver % Mesh % Variables, CostSolName )
278
IF (ASSOCIATED(CostVar)) THEN
279
CostVar % Values(1)=Cost
280
END IF
281
OPEN (12, FILE=CostFile,POSITION='APPEND')
282
write(12,'(e13.5,2x,e15.8)') TimeVar % Values(1),Cost
283
close(12)
284
END IF
285
286
Return
287
288
1000 format('#date,time,',a1,'/',a1,'/',a4,',',a2,':',a2,':',a2)
289
!------------------------------------------------------------------------------
290
END SUBROUTINE AdjointSSA_CostContSolver
291
!------------------------------------------------------------------------------
292
! *****************************************************************************
293
294