Path: blob/devel/elmerice/Solvers/AdjointSSA/AdjointSSA_CostContSolver.F90
3206 views
!/*****************************************************************************/1! *2! * Elmer/Ice, a glaciological add-on to Elmer3! * http://elmerice.elmerfem.org4! *5! *6! * This program is free software; you can redistribute it and/or7! * modify it under the terms of the GNU General Public License8! * as published by the Free Software Foundation; either version 29! * of the License, or (at your option) any later version.10! *11! * This program is distributed in the hope that it will be useful,12! * but WITHOUT ANY WARRANTY; without even the implied warranty of13! * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14! * GNU General Public License for more details.15! *16! * You should have received a copy of the GNU General Public License17! * along with this program (in file fem/GPL-2); if not, write to the18! * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,19! * Boston, MA 02110-1301, USA.20! *21! *****************************************************************************/22! ******************************************************************************23! *24! * Authors: f. Gillet-Chaulet (LGGE, Grenoble,France)25! * Email: [email protected]26! * Web: http://elmerice.elmerfem.org27! *28! * Original Date:29! *30! *****************************************************************************31!32! *****************************************************************************33SUBROUTINE AdjointSSA_CostContSolver( Model,Solver,dt,TransientSimulation )34!------------------------------------------------------------------------------35!Compute the Cost function of the SSA Adjoint inverse Problem as: Integral Node_Cost ds36!37! Serial/Parallel 2D/3D38!39! OUTPUT are : J and DJDu (==Velocityb variable used as forcing of the SSA adjoint problem)40!41! !! Be careful this solver will reset the cost and DJDu to 0; so it has to42! be used as the first cost solver if regularistaion of flux divergence cost43! solvers are used in the simulation!!44!45!46! INPUT PARAMETERS are:47!48! In solver section:49! Problem Dimension = Integer (default = Coordinate system dimension)50! Cost Filename = File (default = 'CostOfT.dat')51! Cost Variable Name = String (default= 'CostValue')52!53! Variables54! Velocityb (forcing for the adjoint pb;DOFs== Pb dimension)55!56! In body Forces:57! 'Adjoint Cost = Real ...' : The nodal value of the cost58! 'Adjoint Cost der 1 = Real ...' : The derivative of 'Adjoint Cost' w.r.t. u-velocity59! 'Adjoint Cost der 2 = Real ...' : The derivative of 'Adjoint Cost' w.r.t. v-velocity60!61!******************************************************************************62USE DefUtils63IMPLICIT NONE64!------------------------------------------------------------------------------65TYPE(Solver_t) :: Solver66TYPE(Model_t) :: Model6768REAL(KIND=dp) :: dt69LOGICAL :: TransientSimulation70!71CHARACTER(LEN=MAX_NAME_LEN), PARAMETER :: DefaultCostFile = 'CostOfT.dat'72CHARACTER(LEN=MAX_NAME_LEN) :: SolverName='CostSolver_Adjoint'73CHARACTER(LEN=MAX_NAME_LEN) :: CostFile74CHARACTER(LEN=MAX_NAME_LEN) :: CostSolName75TYPE(Element_t),POINTER :: Element76TYPE(Variable_t), POINTER :: TimeVar,CostVar77TYPE(Variable_t), POINTER :: VelocitybSol78TYPE(ValueList_t), POINTER :: SolverParams,BodyForce79TYPE(Nodes_t) :: ElementNodes80TYPE(GaussIntegrationPoints_t) :: IntegStuff81REAL(KIND=dp), POINTER :: Vb(:)82INTEGER, POINTER :: NodeIndexes(:)83INTEGER, POINTER :: VbPerm(:)84Logical :: Firsttime=.true.,Found,Parallel,stat,Gotit85integer :: i,j,k,l,t,n,NMAX,DIM,ierr,c86real(kind=dp) :: Cost,Cost_surf,Cost_S87real(kind=dp) :: u,v,w,s,coeff,SqrtElementMetric,x88REAL(KIND=dp) :: NodeCost(Model % MaxElementNodes)89REAL(KIND=dp) :: Basis(Model % MaxElementNodes), dBasisdx(Model % MaxElementNodes,3)90REAL(KIND=dp) :: NodeCostb(Model % MaxElementNodes),NodeCost_der(3,Model %MaxElementNodes)91CHARACTER*10 :: date,temps9293save Firsttime,Parallel94save SolverName,CostSolName,CostFile95save ElementNodes9697CALL Info(SolverName,'***********************',level=0)98CALL Info(SolverName,' This solver has been replaced by:',level=0)99CALL Info(SolverName,' Adjoint_CostContSolver ',level=0)100CALL Info(SolverName,' See documentation under: ',level=0)101CALL Info(SolverName,' elmerice/Solvers/Documentation ',level=0)102CALL Info(SolverName,'***********************',level=0)103CALL FATAL(SolverName,' Use new solver !!')104105SolverParams => GetSolverParams()106DIM=GetInteger(SolverParams ,'Problem Dimension',Found)107If (.NOT.Found) then108CALL WARN(SolverName,'Keyword >Problem Dimension< not found, assume DIM = CoordinateSystemDimension()')109DIM = CoordinateSystemDimension()110Endif111112If (Firsttime) then113N = model % MaxElementNodes114allocate(ElementNodes % x(N), ElementNodes % y(N), ElementNodes % z(N))115116!!!!!!! Check for parallel run117Parallel = .FALSE.118IF ( ASSOCIATED( Solver % Matrix % ParMatrix ) ) THEN119IF ( Solver % Matrix % ParMatrix % ParEnv % PEs > 1 ) THEN120Parallel = .TRUE.121END IF122END IF123124WRITE(SolverName, '(A)') 'CostSolver_Adjoint'125126!!!!!!!!!!! get Solver Variables127128CostFile = ListGetString(Solver % Values,'Cost Filename',Found )129IF (.NOT. Found) CostFile = DefaultCostFile130CALL DATE_AND_TIME(date,temps)131If (Parallel) then132if (ParEnv % MyPe.EQ.0) then133OPEN (12, FILE=CostFile)134write(12,1000) date(5:6),date(7:8),date(1:4),temps(1:2),temps(3:4),temps(5:6)135write(12,'(A)') '#, 1.0'136write(12,'(A)') '# iter, J0'137CLOSE(12)138End if139Else140OPEN (12, FILE=CostFile)141write(12,1000) date(5:6),date(7:8),date(1:4),temps(1:2),temps(3:4),temps(5:6)142write(12,'(A)') '#, 1.0'143write(12,'(A)') '# iter, J0'144CLOSE(12)145End if146147CostSolName = GetString( SolverParams,'Cost Variable Name', Found)148IF(.NOT.Found) THEN149CALL WARN(SolverName,'Keyword >Cost Variable Name< not found in section >Solver<')150CALL WARN(SolverName,'Taking default value >CostValue<')151WRITE(CostSolName,'(A)') 'CostValue'152END IF153154!!! End of First visit155Firsttime=.false.156Endif157158159VelocitybSol => VariableGet( Solver % Mesh % Variables, 'Velocityb' )160IF ( ASSOCIATED( VelocitybSol ) ) THEN161Vb => VelocitybSol % Values162VbPerm => VelocitybSol % Perm163ELSE164WRITE(Message,'(A)') &165'No variable > Velocityb < found'166CALL FATAL(SolverName,Message)167END IF168c=DIM ! size of the velocity variable169IF (VelocitybSol % DOFs.NE.c) then170WRITE(Message,'(A,I1,A,I1)') &171'Variable Velocityb has ',VelocitybSol % DOFs,' DOFs, should be',c172CALL FATAL(SolverName,Message)173End If174Vb=0.0_dp175176177Cost=0._dp178Cost_surf=0._dp179180DO t=1,Solver % NumberOfActiveElements181Element => GetActiveElement(t)182IF (ParEnv % myPe .NE. Element % partIndex) CYCLE183n = GetElementNOFNodes()184185NodeIndexes => Element % NodeIndexes186187! set coords of highest occurring dimension to zero (to get correct path element)188!-------------------------------------------------------------------------------189ElementNodes % x(1:n) = Solver % Mesh % Nodes % x(NodeIndexes)190IF (DIM == 1) THEN !1D SSA191ElementNodes % y(1:n) = 0.0_dp192ElementNodes % z(1:n) = 0.0_dp193ELSE IF (DIM == 2) THEN !2D SSA194ElementNodes % y(1:n) = Solver % Mesh % Nodes % y(NodeIndexes)195ElementNodes % z(1:n) = 0.0_dp196ELSE197WRITE(Message,'(a,i1,a)')&198'It is not possible to compute SSA problems with DOFs=',&199DIM, ' . Aborting'200CALL Fatal( SolverName, Message)201STOP202END IF203204BodyForce => GetBodyForce()205206NodeCost=0.0_dp207NodeCost(1:n) = ListGetReal( BodyForce, 'Adjoint Cost', n, NodeIndexes, GotIt)208IF (.NOT.GotIt) Then209WRITE(Message,'(A)') &210'No variable >Adjoint Cost< found in "Body Forces"'211CALL FATAL(SolverName,Message)212END IF213NodeCost_der=0.0_dp214215NodeCost_der(1,1:n)=ListGetReal( BodyForce, 'Adjoint Cost der 1', n, NodeIndexes, GotIt)216NodeCost_der(2,1:n)=ListGetReal( BodyForce, 'Adjoint Cost der 2', n, NodeIndexes, GotIt)217218!------------------------------------------------------------------------------219! Numerical integration220!------------------------------------------------------------------------------221IntegStuff = GaussPoints( Element )222223224NodeCostb=0.0_dp225226DO i=1,IntegStuff % n227U = IntegStuff % u(i)228V = IntegStuff % v(i)229W = IntegStuff % w(i)230!------------------------------------------------------------------------------231! Basis function values & derivatives at the integration point232!------------------------------------------------------------------------------233stat = ElementInfo( Element,ElementNodes,U,V,W,SqrtElementMetric, &234Basis,dBasisdx )235236x = SUM( ElementNodes % x(1:n) * Basis(1:n) )237s = 1.0d0238239IF ( CurrentCoordinateSystem() /= Cartesian ) THEN240s = 2.0d0 * PI * x241END IF242s = s * SqrtElementMetric * IntegStuff % s(i)243244coeff = SUM(NodeCost(1:n) * Basis(1:n))245Cost_surf=Cost_surf+coeff*s246NodeCostb(1:n)=NodeCostb(1:n) + s*Basis(1:n)247248End do249250c=DIM ! size of the velocity variable251Do j=1,n252Do i=1,DIM253k=(VbPerm(NodeIndexes(j))-1)*c+i254Vb(k)=Vb(k)+NodeCostb(j)*NodeCost_der(i,j)255End Do256End Do257End do258259Cost=Cost_surf260261TimeVar => VariableGet( Solver % Mesh % Variables, 'Time' )262263IF (Parallel) THEN264CALL MPI_ALLREDUCE(Cost,Cost_S,1,&265MPI_DOUBLE_PRECISION,MPI_SUM,ELMER_COMM_WORLD,ierr)266CostVar => VariableGet( Solver % Mesh % Variables, CostSolName )267IF (ASSOCIATED(CostVar)) THEN268CostVar % Values(1)=Cost_S269END IF270IF (Solver % Matrix % ParMatrix % ParEnv % MyPE == 0) then271OPEN (12, FILE=CostFile,POSITION='APPEND')272write(12,'(e13.5,2x,e15.8)') TimeVar % Values(1),Cost_S273CLOSE(12)274End if275ELSE276CostVar => VariableGet( Solver % Mesh % Variables, CostSolName )277IF (ASSOCIATED(CostVar)) THEN278CostVar % Values(1)=Cost279END IF280OPEN (12, FILE=CostFile,POSITION='APPEND')281write(12,'(e13.5,2x,e15.8)') TimeVar % Values(1),Cost282close(12)283END IF284285Return2862871000 format('#date,time,',a1,'/',a1,'/',a4,',',a2,':',a2,':',a2)288!------------------------------------------------------------------------------289END SUBROUTINE AdjointSSA_CostContSolver290!------------------------------------------------------------------------------291! *****************************************************************************292293294