| 1 |
// Copyright (C) 2008-2011 Anders Logg and Garth N. Wells |
| 2 |
// |
| 3 |
// This file is part of DOLFIN. |
| 4 |
// |
| 5 |
// DOLFIN is free software: you can redistribute it and/or modify |
| 6 |
// it under the terms of the GNU Lesser General Public License as published by |
| 7 |
// the Free Software Foundation, either version 3 of the License, or |
| 8 |
// (at your option) any later version. |
| 9 |
// |
| 10 |
// DOLFIN is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
// GNU Lesser General Public License for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU Lesser General Public License |
| 16 |
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
// |
| 18 |
// Modified by Cecile Daversin-Catty, 2018. |
| 19 |
// |
| 20 |
// First added: 2018-10-03 |
| 21 |
// Last changed: 2018-10-03 |
| 22 |
|
| 23 |
#include <dolfin/common/NoDeleter.h> |
| 24 |
#include <dolfin/function/Function.h> |
| 25 |
#include <dolfin/function/FunctionSpace.h> |
| 26 |
#include <dolfin/la/GenericMatrix.h> |
| 27 |
#include <dolfin/la/GenericVector.h> |
| 28 |
#include <dolfin/la/GenericLinearAlgebraFactory.h> |
| 29 |
#include <dolfin/la/LinearSolver.h> |
| 30 |
#include <dolfin/la/PETScNestMatrix.h> |
| 31 |
#include <dolfin/la/PETScKrylovSolver.h> |
| 32 |
#include <dolfin/la/PETScPreconditioner.h> |
| 33 |
#include "MixedAssembler.h" |
| 34 |
#include "SystemAssembler.h" |
| 35 |
#include "assemble.h" |
| 36 |
#include "DirichletBC.h" |
| 37 |
#include "Form.h" |
| 38 |
#include "MixedNonlinearVariationalProblem.h" |
| 39 |
#include "MixedNonlinearVariationalSolver.h" |
| 40 |
#include "GenericDofMap.h" |
| 41 |
|
| 42 |
#include <dolfin/la/PETScOptions.h> |
| 43 |
using namespace dolfin; |
| 44 |
|
| 45 |
MixedNonlinearVariationalSolver:: |
| 46 |
MixedNonlinearVariationalSolver() |
| 47 |
{ |
| 48 |
// Set parameters |
| 49 |
parameters = default_parameters(); |
| 50 |
} |
| 51 |
//----------------------------------------------------------------------------- |
| 52 |
MixedNonlinearVariationalSolver:: |
| 53 |
MixedNonlinearVariationalSolver(std::shared_ptr<MixedNonlinearVariationalProblem> problem) |
| 54 |
: _problem(problem) |
| 55 |
{ |
| 56 |
// Set parameters |
| 57 |
parameters = default_parameters(); |
| 58 |
} |
| 59 |
//----------------------------------------------------------------------------- |
| 60 |
std::pair<std::size_t, bool> MixedNonlinearVariationalSolver::solve() |
| 61 |
{ |
| 62 |
begin("Solving mixed nonlinear variational problem."); |
| 63 |
|
| •64 |
#ifdef HAS_PETSC |
| 65 |
// Check that the Jacobian has been defined |
| 66 |
dolfin_assert(_problem); |
| 67 |
if (!_problem->has_jacobian()) |
| 68 |
{ |
| 69 |
dolfin_error("NonlinearVariationalSolver.cpp", |
| 70 |
"solve nonlinear variational problem", |
| 71 |
"The Jacobian form has not been defined"); |
| 72 |
} |
| 73 |
// Check that the dolfin is configured with petsc is bounds are set |
| 74 |
if (_problem->has_lower_bound() || _problem->has_upper_bound()) |
| 75 |
{ |
| 76 |
dolfin_error("NonlinearVariationalSolver.cpp", |
| 77 |
"solve nonlinear variational problem", |
| 78 |
"Needs PETSc to solve bound constrained problems"); |
| 79 |
} |
| 80 |
|
| 81 |
// Get problem data |
| 82 |
dolfin_assert(_problem); |
| 83 |
auto u = _problem->solution(); |
| 84 |
|
| 85 |
// Create discrete nonlinear problem - assembly |
| 86 |
if (!nonlinear_problem) |
| 87 |
{ |
| 88 |
nonlinear_problem = std::make_shared<MixedNonlinearDiscreteProblem>(_problem, |
| 89 |
reference_to_no_delete_pointer(*this)); |
| 90 |
} |
| 91 |
|
| 92 |
std::vector<std::shared_ptr<GenericVector>> us; |
| 93 |
MPI_Comm comm = u[0]->vector()->mpi_comm(); |
| 94 |
std::vector<std::shared_ptr<const IndexMap>> index_maps(2); |
| 95 |
|
| 96 |
for (size_t i=0; i<u.size(); ++i) |
| 97 |
{ |
| 98 |
dolfin_assert(u[i]->vector()); |
| 99 |
us.push_back(u[i]->vector()); |
| 100 |
|
| 101 |
dolfin_assert(u[i]->function_space()->mesh()); |
| 102 |
index_maps[0] = u[i]->function_space()->dofmap().get()->index_map(); |
| 103 |
|
| 104 |
// Initialize _bs[i] with the appropriate layout for each block i |
| 105 |
std::shared_ptr<GenericVector> _b = u[i]->vector()->factory().create_vector(comm); |
| 106 |
auto tensor_layout = _b->factory().create_layout(comm, 1); |
| 107 |
tensor_layout->init(index_maps, TensorLayout::Ghosts::UNGHOSTED); |
| 108 |
_b->init(*tensor_layout); |
| 109 |
_b->zero(); |
| 110 |
nonlinear_problem->_bs.push_back(_b); |
| 111 |
|
| 112 |
// Initialize _Js[i,j] with the appropriate layout for each block (i,j) |
| 113 |
for (size_t j=0; j<u.size(); ++j) |
| 114 |
{ |
| 115 |
std::shared_ptr<GenericMatrix> _J = u[j]->vector()->factory().create_matrix(comm); |
| 116 |
index_maps[1] = u[j]->function_space()->dofmap().get()->index_map(); |
| 117 |
auto tensor_layout = _J->factory().create_layout(comm, 2); |
| 118 |
tensor_layout->init(index_maps, TensorLayout::Ghosts::UNGHOSTED); |
| 119 |
_J->init(*tensor_layout); |
| 120 |
_J->zero(); |
| 121 |
_J->apply("add"); |
| 122 |
nonlinear_problem->_Js.push_back(_J); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
auto x = u[0]->vector()->factory().create_vector(comm); |
| 127 |
PETScNestMatrix J(nonlinear_problem->_Js); |
| 128 |
// Recombine x = [ u[i]->vector for each block i ] |
| 129 |
// for(auto &u : us) //TO CHECK |
| 130 |
// as_type<PETScVector>(u)->update_ghost_values(); |
| 131 |
J.init_vectors(*x, us); |
| 132 |
|
| 133 |
std::pair<std::size_t, bool> ret; |
| 134 |
if (std::string(parameters["nonlinear_solver"]) == "newton") |
| 135 |
{ |
| 136 |
if (_problem->has_lower_bound() && _problem->has_upper_bound()) |
| 137 |
{ |
| 138 |
dolfin_error("MixedNonlinearVariationalSolver.cpp", |
| 139 |
"solve nonlinear variational problem", |
| 140 |
"Set the \"nonlinear_solver\" parameter to \"snes\" or remove bounds"); |
| 141 |
} |
| 142 |
|
| 143 |
// Create Newton solver and set parameters |
| 144 |
MPI_Comm comm = u[0]->function_space()->mesh()->mpi_comm(); |
| 145 |
if (!newton_solver) |
| 146 |
{ |
| 147 |
newton_solver = std::make_shared<NewtonSolver>(comm); |
| 148 |
} |
| 149 |
|
| 150 |
// Pass parameters to Newton solver |
| 151 |
dolfin_assert(newton_solver); |
| 152 |
newton_solver->parameters.update(parameters("newton_solver")); |
| 153 |
|
| 154 |
// Solve nonlinear problem using Newton's method |
| 155 |
dolfin_assert(nonlinear_problem); |
| 156 |
ret = newton_solver->solve(*nonlinear_problem, *x); |
| 157 |
} |
| 158 |
// FIXME : SNES Solver need to be setup (SNES norm is zero at 1st iteration) |
| 159 |
else if (std::string(parameters["nonlinear_solver"]) == "snes") |
| 160 |
{ |
| 161 |
dolfin_error("MixedNonlinearVariationalSolver.cpp", |
| 162 |
"solve nonlinear variational problem", |
| 163 |
"SNES solver is not (yet) available for mixed problems (use newton)"); |
| 164 |
|
| 165 |
// Create SNES solver and set parameters |
| 166 |
MPI_Comm comm = u[0]->function_space()->mesh()->mpi_comm(); |
| 167 |
if (!snes_solver) |
| 168 |
{ |
| 169 |
snes_solver = std::make_shared<PETScSNESSolver>(comm); |
| 170 |
} |
| 171 |
snes_solver->parameters.update(parameters("snes_solver")); |
| 172 |
|
| 173 |
// Solve nonlinear problem using PETSc's SNES |
| 174 |
dolfin_assert(nonlinear_problem); |
| 175 |
if (_problem->has_lower_bound() && _problem->has_upper_bound()) |
| 176 |
{ |
| 177 |
ret = snes_solver->solve(*nonlinear_problem, *x, |
| 178 |
*_problem->lower_bound()[0], |
| 179 |
*_problem->upper_bound()[0]); |
| 180 |
} |
| 181 |
else |
| 182 |
{ |
| 183 |
ret = snes_solver->solve(*nonlinear_problem, *x); |
| 184 |
} |
| 185 |
} |
| 186 |
else |
| 187 |
{ |
| 188 |
dolfin_error("MixedNonlinearVariationalSolver.cpp", |
| 189 |
"solve nonlinear variational problem", |
| 190 |
"Unknown nonlinear solver type"); |
| 191 |
} |
| •192 |
#else |
| 193 |
dolfin_error("MixedNonlinearVariationalSolver.cpp", "solve problem", "MixedNonlinearVariationalSolver::solve requires PETSc"); |
| 194 |
#endif |
| 195 |
|
| 196 |
end(); |
| •197 |
#ifdef HAS_PETSC |
| 198 |
return ret; |
| •199 |
#endif |
| 200 |
} |
| 201 |
//----------------------------------------------------------------------------- |
| 202 |
// Implementation of NonlinearDiscreteProblem |
| 203 |
//----------------------------------------------------------------------------- |
| 204 |
MixedNonlinearVariationalSolver::MixedNonlinearDiscreteProblem:: |
| 205 |
MixedNonlinearDiscreteProblem(std::shared_ptr<const MixedNonlinearVariationalProblem> problem, |
| 206 |
std::shared_ptr<const MixedNonlinearVariationalSolver> solver) |
| 207 |
: _problem(problem), _solver(solver) |
| 208 |
{ |
| 209 |
// Do nothing |
| 210 |
} |
| 211 |
//----------------------------------------------------------------------------- |
| 212 |
MixedNonlinearVariationalSolver::MixedNonlinearDiscreteProblem::~MixedNonlinearDiscreteProblem() |
| 213 |
{ |
| 214 |
// Do nothing |
| 215 |
} |
| 216 |
//----------------------------------------------------------------------------- |
| 217 |
void MixedNonlinearVariationalSolver:: |
| 218 |
MixedNonlinearDiscreteProblem::F(GenericVector& b, const GenericVector& x) |
| 219 |
{ |
| •220 |
#ifdef HAS_PETSC |
| 221 |
const auto bform = _problem->residual_form(); |
| 222 |
auto u = _problem->solution(); |
| 223 |
auto bcs = _problem->bcs(); |
| 224 |
|
| 225 |
bool has_ufc_form = false; |
| 226 |
MPI_Comm comm = u[0]->vector()->mpi_comm(); |
| 227 |
|
| 228 |
for(size_t i=0; i<u.size(); ++i) |
| 229 |
{ |
| 230 |
has_ufc_form = false; |
| 231 |
std::shared_ptr<GenericVector> _b = u[i]->vector()->factory().create_vector(comm); |
| 232 |
|
| 233 |
for(size_t j=0; j<bform[i].size(); ++j) |
| 234 |
{ |
| 235 |
if (bform[i][j]->ufc_form()) |
| 236 |
{ |
| 237 |
has_ufc_form = true; |
| 238 |
assemble_mixed(*(_b), *(bform[i][j]), bool(j>0)); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
// Update _bs |
| 243 |
if(has_ufc_form) |
| 244 |
_bs[i] = _b; |
| 245 |
} |
| 246 |
|
| 247 |
// Boundary conditions |
| 248 |
PETScNestMatrix J(_Js); |
| 249 |
std::vector<IS> is(u.size()); |
| 250 |
MatNestGetISs(J.mat(), is.data(), NULL); |
| 251 |
|
| 252 |
for(size_t i=0; i<u.size(); ++i) |
| 253 |
{ |
| 254 |
// Extract the subvector x[i] of x corresponding to block i |
| 255 |
Vec __x; |
| 256 |
VecGetSubVector(as_type<const PETScVector>(x).vec(),is[i], &__x); |
| 257 |
PETScVector _x(__x); |
| 258 |
|
| 259 |
for (size_t c = 0; c < bcs[i].size(); c++) |
| 260 |
{ |
| 261 |
dolfin_assert(bcs[i][c]); |
| 262 |
bcs[i][c]->apply(*_bs[i], _x); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
// Update b from _bs[] |
| 267 |
J.init_vectors(b, _bs); |
| •268 |
#else |
| 269 |
dolfin_error("MixedNonlinearVariationalSolver.cpp", "assemble residual", "MixedNonlinearVariationalSolver::F requires PETSc"); |
| 270 |
#endif |
| 271 |
} |
| 272 |
//----------------------------------------------------------------------------- |
| 273 |
void MixedNonlinearVariationalSolver:: |
| 274 |
MixedNonlinearDiscreteProblem::J(GenericMatrix& A, const GenericVector& x) |
| 275 |
{ |
| •276 |
#ifdef HAS_PETSC |
| 277 |
const auto jform = _problem->jacobian_form(); |
| 278 |
auto u = _problem->solution(); |
| 279 |
auto bcs = _problem->bcs(); |
| 280 |
|
| 281 |
MPI_Comm comm = u[0]->vector()->mpi_comm(); |
| 282 |
|
| 283 |
for (size_t i=0; i<u.size(); ++i) |
| 284 |
{ |
| 285 |
for (size_t j=0; j<u.size(); ++j) |
| 286 |
{ |
| 287 |
for(size_t k=0; k<jform[i*u.size() + j].size(); ++k) |
| 288 |
{ |
| 289 |
if(jform[i*u.size() + j][k]->ufc_form()) // If block(i,j) not empty |
| 290 |
{ |
| 291 |
_Js[i*u.size() + j] = u[j]->vector()->factory().create_matrix(comm); |
| 292 |
assemble_mixed(*(_Js[i*u.size() + j]), *(jform[i*u.size() + j][k]), bool(k>0)); |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
// Boundary conditions |
| 299 |
PETScNestMatrix J(_Js); |
| 300 |
std::vector<IS> is(u.size()); |
| 301 |
MatNestGetISs(J.mat(), is.data(), NULL); |
| 302 |
|
| 303 |
for(size_t i=0; i<u.size(); ++i) |
| 304 |
{ |
| 305 |
Vec __x; |
| 306 |
VecGetSubVector(as_type<const PETScVector>(x).vec(),is[i], &__x); |
| 307 |
PETScVector _x(__x); |
| 308 |
|
| 309 |
for (size_t c = 0; c < bcs[i].size(); c++) |
| 310 |
{ |
| 311 |
dolfin_assert(bcs[i][c]); |
| 312 |
for (size_t j=0; j<u.size(); ++j) |
| 313 |
{ |
| 314 |
if(i!=j && jform[i*u.size() + j][0]->ufc_form()) |
| 315 |
bcs[i][c]->zero_columns(*(_Js[i*u.size() + j]), *(_bs[i]), 0, true); |
| 316 |
else |
| 317 |
bcs[i][c]->apply(*(_Js[i*u.size() + i]), *(_bs[i]), _x); |
| 318 |
} |
| 319 |
} |
| 320 |
_Js[i*u.size() + i]->apply("insert"); |
| 321 |
} |
| 322 |
|
| 323 |
// Update A from _Js, as a PETScNestMatrix |
| 324 |
// FIXME : We should not need petsc_mats and find a way to call set_nest on _Js |
| 325 |
std::vector<Mat> petsc_mats(u.size()*u.size()); |
| 326 |
for(size_t i=0; i<u.size(); ++i) |
| 327 |
for(size_t j=0; j<u.size(); ++j) |
| 328 |
petsc_mats[i*u.size() + j] = as_type<PETScMatrix>(_Js[i*u.size() + j])->mat(); |
| 329 |
as_type<PETScMatrix>(A).set_nest(petsc_mats); |
| 330 |
as_type<PETScMatrix>(A).apply("insert"); |
| •331 |
#else |
| 332 |
dolfin_error("MixedNonlinearVariationalSolver.cpp", "assemble jacobian", "MixedNonlinearVariationalSolver::J requires PETSc"); |
| 333 |
#endif |
| 334 |
} |