OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

fvMatrixSolve.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00028 
00029 template<class Type>
00030 void Foam::fvMatrix<Type>::setComponentReference
00031 (
00032     const label patchi,
00033     const label facei,
00034     const direction cmpt,
00035     const scalar value
00036 )
00037 {
00038     if (psi_.needReference())
00039     {
00040         if (Pstream::master())
00041         {
00042             internalCoeffs_[patchi][facei].component(cmpt) +=
00043                 diag()[psi_.mesh().boundary()[patchi].faceCells()[facei]];
00044 
00045             boundaryCoeffs_[patchi][facei].component(cmpt) +=
00046                 diag()[psi_.mesh().boundary()[patchi].faceCells()[facei]]
00047                *value;
00048         }
00049     }
00050 }
00051 
00052 
00053 template<class Type>
00054 Foam::lduMatrix::solverPerformance Foam::fvMatrix<Type>::solve
00055 (
00056     const dictionary& solverControls
00057 )
00058 {
00059     if (debug)
00060     {
00061         Info<< "fvMatrix<Type>::solve(const dictionary& solverControls) : "
00062                "solving fvMatrix<Type>"
00063             << endl;
00064     }
00065 
00066     lduMatrix::solverPerformance solverPerfVec
00067     (
00068         "fvMatrix<Type>::solve",
00069         psi_.name()
00070     );
00071 
00072     scalarField saveDiag = diag();
00073 
00074     Field<Type> source = source_;
00075 
00076     // At this point include the boundary source from the coupled boundaries.
00077     // This is corrected for the implict part by updateMatrixInterfaces within
00078     // the component loop.
00079     addBoundarySource(source);
00080 
00081     typename Type::labelType validComponents
00082     (
00083         pow
00084         (
00085             psi_.mesh().solutionD(),
00086             pTraits<typename powProduct<Vector<label>, Type::rank>::type>::zero
00087         )
00088     );
00089 
00090     for(direction cmpt=0; cmpt<Type::nComponents; cmpt++)
00091     {
00092         if (validComponents[cmpt] == -1) continue;
00093 
00094         // copy field and source
00095 
00096         scalarField psiCmpt = psi_.internalField().component(cmpt);
00097         addBoundaryDiag(diag(), cmpt);
00098 
00099         scalarField sourceCmpt = source.component(cmpt);
00100 
00101         FieldField<Field, scalar> bouCoeffsCmpt
00102         (
00103             boundaryCoeffs_.component(cmpt)
00104         );
00105 
00106         FieldField<Field, scalar> intCoeffsCmpt
00107         (
00108             internalCoeffs_.component(cmpt)
00109         );
00110 
00111         lduInterfaceFieldPtrsList interfaces =
00112             psi_.boundaryField().interfaces();
00113 
00114         // Use the initMatrixInterfaces and updateMatrixInterfaces to correct
00115         // bouCoeffsCmpt for the explicit part of the coupled boundary
00116         // conditions
00117         initMatrixInterfaces
00118         (
00119             bouCoeffsCmpt,
00120             interfaces,
00121             psiCmpt,
00122             sourceCmpt,
00123             cmpt
00124         );
00125 
00126         updateMatrixInterfaces
00127         (
00128             bouCoeffsCmpt,
00129             interfaces,
00130             psiCmpt,
00131             sourceCmpt,
00132             cmpt
00133         );
00134 
00135         lduMatrix::solverPerformance solverPerf;
00136 
00137         // Solver call
00138         solverPerf = lduMatrix::solver::New
00139         (
00140             psi_.name() + pTraits<Type>::componentNames[cmpt],
00141             *this,
00142             bouCoeffsCmpt,
00143             intCoeffsCmpt,
00144             interfaces,
00145             solverControls
00146         )->solve(psiCmpt, sourceCmpt, cmpt);
00147 
00148         solverPerf.print();
00149 
00150         if
00151         (
00152             solverPerf.initialResidual() > solverPerfVec.initialResidual()
00153          && !solverPerf.singular()
00154         )
00155         {
00156             solverPerfVec = solverPerf;
00157         }
00158 
00159         psi_.internalField().replace(cmpt, psiCmpt);
00160         diag() = saveDiag;
00161     }
00162 
00163     psi_.correctBoundaryConditions();
00164 
00165     return solverPerfVec;
00166 }
00167 
00168 
00169 template<class Type>
00170 Foam::autoPtr<typename Foam::fvMatrix<Type>::fvSolver>
00171 Foam::fvMatrix<Type>::solver()
00172 {
00173     return solver(psi_.mesh().solverDict(psi_.name()));
00174 }
00175 
00176 template<class Type>
00177 Foam::lduMatrix::solverPerformance Foam::fvMatrix<Type>::fvSolver::solve()
00178 {
00179     return solve(psi_.mesh().solverDict(psi_.name()));
00180 }
00181 
00182 
00183 template<class Type>
00184 Foam::lduMatrix::solverPerformance Foam::fvMatrix<Type>::solve()
00185 {
00186     return solve(psi_.mesh().solverDict(psi_.name()));
00187 }
00188 
00189 
00190 template<class Type>
00191 Foam::tmp<Foam::Field<Type> > Foam::fvMatrix<Type>::residual() const
00192 {
00193     tmp<Field<Type> > tres(source_);
00194     Field<Type>& res = tres();
00195 
00196     addBoundarySource(res);
00197 
00198     // Loop over field components
00199     for (direction cmpt=0; cmpt<Type::nComponents; cmpt++)
00200     {
00201         scalarField psiCmpt = psi_.internalField().component(cmpt);
00202 
00203         scalarField boundaryDiagCmpt(psi_.size(), 0.0);
00204         addBoundaryDiag(boundaryDiagCmpt, cmpt);
00205 
00206         FieldField<Field, scalar> bouCoeffsCmpt
00207         (
00208             boundaryCoeffs_.component(cmpt)
00209         );
00210 
00211         res.replace
00212         (
00213             cmpt,
00214             lduMatrix::residual
00215             (
00216                 psiCmpt,
00217                 res.component(cmpt) - boundaryDiagCmpt*psiCmpt,
00218                 bouCoeffsCmpt,
00219                 psi_.boundaryField().interfaces(),
00220                 cmpt
00221             )
00222         );
00223     }
00224 
00225     return tres;
00226 }
00227 
00228 
00229 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd