| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Galeri: Finite Element and Matrix Generation Package |
| 4 |
// |
| 5 |
// Copyright 2006 ETHZ/NTESS and the Galeri contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#ifndef CREATEADRMATRIX_HPP |
| 11 |
#define CREATEADRMATRIX_HPP |
| 12 |
|
| 13 |
#include <Teuchos_RCP.hpp> |
| 14 |
|
| 15 |
#include "ADR_Problem.hpp" |
| 16 |
#include "Galeri_XpetraMatrixTypes.hpp" |
| 17 |
|
| 18 |
namespace ADR { |
| 19 |
|
| 20 |
namespace Xpetra { |
| 21 |
|
| 22 |
// ============================================= ADR1D ============================================= |
| 23 |
template <typename Scalar, typename LocalOrdinal, typename GlobalOrdinal, typename Map, typename Matrix, typename MultiVector> |
| 24 |
class ADR1DProblem : public Problem<Map, Matrix, MultiVector> { |
| 25 |
public: |
| 26 |
ADR1DProblem(Teuchos::ParameterList& list, const Teuchos::RCP<const Map>& map) |
| 27 |
: Problem<Map, Matrix, MultiVector>(list, map) {} |
| 28 |
Teuchos::RCP<Matrix> BuildMatrix(); |
| 29 |
|
| 30 |
private: |
| 31 |
// domain definition |
| 32 |
Scalar xleft = 0.0; |
| 33 |
Scalar xright = 1.0; |
| 34 |
|
| 35 |
// Function that defines the diffusion coefficient |
| 36 |
inline Scalar diff(Scalar x) { return 1.0 + x; }; |
| 37 |
|
| 38 |
// Function that defines the first derivative of the diffusion coefficient |
| 39 |
inline Scalar diff_prime(Scalar x) { return 1.0; }; |
| 40 |
|
| 41 |
// Function that defines the advection coefficient |
| 42 |
inline Scalar adv(Scalar x) { return 10.0 * x; }; |
| 43 |
|
| 44 |
// Function taht defines the reaction coefficient |
| 45 |
inline Scalar reac(Scalar x) { return 0.0; }; |
| 46 |
}; |
| 47 |
|
| 48 |
template <typename Scalar, typename LocalOrdinal, typename GlobalOrdinal, typename Map, typename Matrix, typename MultiVector> |
| 49 |
Teuchos::RCP<Matrix> ADR1DProblem<Scalar, LocalOrdinal, GlobalOrdinal, Map, Matrix, MultiVector>::BuildMatrix() { |
| 50 |
GlobalOrdinal nx = this->list_.get("nx", (GlobalOrdinal)-1); |
| 51 |
|
| 52 |
if (nx == -1) |
| 53 |
nx = this->Map_->getGlobalNumElements(); |
| 54 |
|
| 55 |
const Scalar dx = (Scalar)(xright - xleft) / static_cast<Scalar>(nx - 1); |
| 56 |
|
| 57 |
const Scalar a = 2.0; |
| 58 |
const Scalar b = -1.0; |
| 59 |
const Scalar c = -1.0; |
| 60 |
|
| 61 |
// this->A_ = Galeri::Xpetra::TriDiag<Scalar,LocalOrdinal,GlobalOrdinal,Map,Matrix>(this->Map_, nx, 2.0, -1.0, -1.0); |
| 62 |
Teuchos::RCP<Matrix> mtx = Galeri::Xpetra::MatrixTraits<Map, Matrix>::Build(this->Map_, 3); |
| 63 |
|
| 64 |
LocalOrdinal NumMyElements = this->Map_->getLocalNumElements(); |
| 65 |
Teuchos::ArrayView<const GlobalOrdinal> MyGlobalElements = this->Map_->getLocalElementList(); |
| 66 |
GlobalOrdinal indexBase = this->Map_->getIndexBase(); |
| 67 |
|
| 68 |
Teuchos::RCP<const Teuchos::Comm<int> > comm = this->Map_->getComm(); |
| 69 |
|
| 70 |
GlobalOrdinal NumGlobalElements = this->Map_->getGlobalNumElements(); |
| 71 |
|
| 72 |
GlobalOrdinal NumEntries; |
| 73 |
LocalOrdinal nnz = 2; |
| 74 |
std::vector<Scalar> Values(nnz); |
| 75 |
std::vector<GlobalOrdinal> Indices(nnz); |
| 76 |
|
| 77 |
comm->barrier(); |
| 78 |
|
| 79 |
Teuchos::RCP<Teuchos::Time> timer = Teuchos::rcp(new Teuchos::Time("1D Assembler global insert")); |
| 80 |
timer->start(true); |
| 81 |
|
| 82 |
// c a b |
| 83 |
for (LocalOrdinal i = 0; i < NumMyElements; i++) { |
| 84 |
if (MyGlobalElements[i] == indexBase) { |
| 85 |
// off-diagonal for first row |
| 86 |
Indices[0] = 1 + indexBase; |
| 87 |
|
| 88 |
// Diffusive term |
| 89 |
Values[0] = b * diff(xleft + MyGlobalElements[i] * dx) / (dx * dx); |
| 90 |
Values[0] = Values[0] - diff_prime(xleft + MyGlobalElements[i] * dx) / (2.0 * dx); |
| 91 |
|
| 92 |
// Advective term |
| 93 |
Values[0] = Values[0] + adv(xleft + MyGlobalElements[i] * dx) / (2.0 * dx); |
| 94 |
|
| 95 |
NumEntries = 1; |
| 96 |
|
| 97 |
} else if (MyGlobalElements[i] == NumGlobalElements + indexBase - 1) { |
| 98 |
// off-diagonal for last row |
| 99 |
Indices[0] = NumGlobalElements - 2 + indexBase; |
| 100 |
|
| 101 |
// Diffusive term |
| 102 |
Values[0] = c * diff(xleft + MyGlobalElements[i] * dx) / (dx * dx); |
| 103 |
Values[0] = Values[0] + diff_prime(xleft + MyGlobalElements[i] * dx) / (2.0 * dx); |
| 104 |
|
| 105 |
// Advective term |
| 106 |
Values[0] = Values[0] - adv(xleft + MyGlobalElements[i] * dx) / (2.0 * dx); |
| 107 |
|
| 108 |
NumEntries = 1; |
| 109 |
|
| 110 |
} else { |
| 111 |
// off-diagonal for internal row |
| 112 |
Indices[0] = MyGlobalElements[i] - 1; |
| 113 |
Indices[1] = MyGlobalElements[i] + 1; |
| 114 |
|
| 115 |
// Diffusive term |
| 116 |
Values[0] = c * diff(xleft + MyGlobalElements[i] * dx) / (dx * dx); |
| 117 |
Values[0] = Values[0] + diff_prime(xleft + MyGlobalElements[i] * dx) / (2.0 * dx); |
| 118 |
Values[1] = b * diff(xleft + MyGlobalElements[i] * dx) / (dx * dx); |
| 119 |
Values[1] = Values[1] - diff_prime(xleft + MyGlobalElements[i] * dx) / (2.0 * dx); |
| 120 |
|
| 121 |
// Advective term |
| 122 |
Values[0] = Values[0] - adv(xleft + MyGlobalElements[i] * dx) / (2.0 * dx); |
| 123 |
Values[1] = Values[1] + adv(xleft + MyGlobalElements[i] * dx) / (2.0 * dx); |
| 124 |
|
| 125 |
NumEntries = 2; |
| 126 |
} |
| 127 |
|
| 128 |
// put the off-diagonal entries |
| 129 |
// Xpetra wants ArrayViews (sigh) |
| 130 |
Teuchos::ArrayView<Scalar> av(&Values[0], NumEntries); |
| 131 |
Teuchos::ArrayView<GlobalOrdinal> iv(&Indices[0], NumEntries); |
| 132 |
mtx->insertGlobalValues(MyGlobalElements[i], iv, av); |
| 133 |
|
| 134 |
// Put in the diagonal entry |
| 135 |
// Diffusion |
| 136 |
Scalar diag_entry = a * diff(xleft + MyGlobalElements[i] * dx) / (dx * dx); |
| 137 |
// Reaction |
| 138 |
diag_entry = diag_entry + reac(xleft + MyGlobalElements[i] * dx); |
| 139 |
|
| 140 |
mtx->insertGlobalValues(MyGlobalElements[i], |
| 141 |
Teuchos::tuple<GlobalOrdinal>(MyGlobalElements[i]), |
| 142 |
Teuchos::tuple<Scalar>(diag_entry)); |
| 143 |
} |
| 144 |
|
| 145 |
timer->stop(); |
| 146 |
|
| 147 |
timer = Teuchos::rcp(new Teuchos::Time("1D Assembler fillComplete")); |
| 148 |
timer->start(true); |
| 149 |
|
| 150 |
mtx->fillComplete(); |
| 151 |
this->A_ = mtx; |
| 152 |
|
| 153 |
timer->stop(); |
| 154 |
|
| 155 |
this->A_->setObjectLabel(this->getObjectLabel()); |
| 156 |
return this->A_; |
| 157 |
} |
| 158 |
|
| 159 |
// ============================================= ADR2D ============================================= |
| 160 |
template <typename Scalar, typename LocalOrdinal, typename GlobalOrdinal, typename Map, typename Matrix, typename MultiVector> |
| 161 |
class ADR2DProblem : public Problem<Map, Matrix, MultiVector> { |
| 162 |
public: |
| 163 |
ADR2DProblem(Teuchos::ParameterList& list, const Teuchos::RCP<const Map>& map) |
| 164 |
: Problem<Map, Matrix, MultiVector>(list, map) {} |
| 165 |
Teuchos::RCP<Matrix> BuildMatrix(); |
| 166 |
|
| 167 |
private: |
| 168 |
// Function that defines the diffusion coefficient |
| 169 |
inline Scalar diff(Scalar x, Scalar y) { return 1.0 + 0.0 * x + 0.0 * y; }; |
| 170 |
|
| 171 |
// Function that defines the first derivative in x-direction of the diffusion coefficient |
| 172 |
inline Scalar diff_primex(Scalar x, Scalar y) { return 0.0 + 0.0 * x + 0.0 * y; }; |
| 173 |
|
| 174 |
// Function that defines the first derivative in x-direction of the diffusion coefficient |
| 175 |
inline Scalar diff_primey(Scalar x, Scalar y) { return 0.0 + 0.0 * x + 0.0 * y; }; |
| 176 |
|
| 177 |
// Function that defines the advection coefficient in the x-direction |
| 178 |
inline Scalar advx(Scalar x, Scalar y) { return 10.0 + 0.0 * x + 0.0 * y; }; |
| 179 |
|
| 180 |
// Function that defines the advection coefficient in the x-direction |
| 181 |
inline Scalar advy(Scalar x, Scalar y) { return 10.0 + 0.0 * x + 0.0 * y; }; |
| 182 |
|
| 183 |
// Function taht defines the reaction coefficient |
| 184 |
inline Scalar reac(Scalar x, Scalar y) { return 0.0 + 0.0 * x + 0.0 * y; }; |
| 185 |
}; |
| 186 |
|
| 187 |
template <typename Scalar, typename LocalOrdinal, typename GlobalOrdinal, typename Map, typename Matrix, typename MultiVector> |
| 188 |
Teuchos::RCP<Matrix> ADR2DProblem<Scalar, LocalOrdinal, GlobalOrdinal, Map, Matrix, MultiVector>::BuildMatrix() { |
| 189 |
GlobalOrdinal nx = this->list_.get("nx", (GlobalOrdinal)-1); |
| 190 |
GlobalOrdinal ny = this->list_.get("ny", (GlobalOrdinal)-1); |
| 191 |
double one = 1.0; |
| 192 |
Scalar stretchx = (Scalar)this->list_.get("stretchx", one); |
| 193 |
Scalar stretchy = (Scalar)this->list_.get("stretchy", one); |
| 194 |
|
| 195 |
if (nx == -1 || ny == -1) { |
| 196 |
GlobalOrdinal n = this->Map_->getGlobalNumElements(); |
| 197 |
nx = (GlobalOrdinal)sqrt((double)n); |
| 198 |
ny = nx; |
| 199 |
TEUCHOS_TEST_FOR_EXCEPTION(nx * ny != n, std::logic_error, "You need to specify nx and ny."); |
| 200 |
} |
| 201 |
bool keepBCs = this->list_.get("keepBCs", false); |
| 202 |
|
| 203 |
// Diffusion stencil |
| 204 |
Scalar c1 = (Scalar)-one / (stretchx * stretchx); |
| 205 |
Scalar b1 = (Scalar)-one / (stretchx * stretchx); |
| 206 |
Scalar e1 = (Scalar)-one / (stretchy * stretchy); |
| 207 |
Scalar d1 = (Scalar)-one / (stretchy * stretchy); |
| 208 |
Scalar a1 = -(b1 + c1 + d1 + e1); |
| 209 |
|
| 210 |
// Advection stencil |
| 211 |
Scalar c2 = (Scalar)one / (2.0 * stretchx); |
| 212 |
Scalar b2 = (Scalar)one / (2.0 * stretchx); |
| 213 |
Scalar e2 = (Scalar)one / (2.0 * stretchy); |
| 214 |
Scalar d2 = (Scalar)one / (2.0 * stretchy); |
| 215 |
|
| 216 |
// this->A_ = Cross2D<Scalar,LocalOrdinal,GlobalOrdinal,Map,Matrix>(this->Map_, nx, ny, center, west, east, south, north, this->DirichletBC_, keepBCs); |
| 217 |
LocalOrdinal nnz = 5; |
| 218 |
|
| 219 |
Teuchos::RCP<Matrix> mtx = Galeri::Xpetra::MatrixTraits<Map, Matrix>::Build(this->Map_, nnz); |
| 220 |
|
| 221 |
LocalOrdinal numMyElements = (this->Map_)->getLocalNumElements(); |
| 222 |
GlobalOrdinal indexBase = (this->Map_)->getIndexBase(); |
| 223 |
|
| 224 |
Teuchos::ArrayView<const GlobalOrdinal> myGlobalElements = (this->Map_)->getLocalElementList(); |
| 225 |
|
| 226 |
GlobalOrdinal center, left, right, lower, upper; |
| 227 |
std::vector<Scalar> vals(nnz); |
| 228 |
std::vector<GlobalOrdinal> inds(nnz); |
| 229 |
|
| 230 |
// e |
| 231 |
// b a c |
| 232 |
// d |
| 233 |
for (LocalOrdinal i = 0; i < numMyElements; ++i) { |
| 234 |
size_t n = 0; |
| 235 |
|
| 236 |
center = myGlobalElements[i] - indexBase; |
| 237 |
|
| 238 |
// Determine coordinates |
| 239 |
Scalar x1 = (Scalar)(center % nx) * stretchx; |
| 240 |
Scalar x2 = (Scalar)(std::floor(center / nx)) * stretchy; |
| 241 |
|
| 242 |
Galeri::Xpetra::GetNeighboursCartesian2d(center, nx, ny, left, right, lower, upper); |
| 243 |
|
| 244 |
bool isDirichlet = (left == -1 && (this->DirichletBC_ & DIR_LEFT)) || |
| 245 |
(right == -1 && (this->DirichletBC_ & DIR_RIGHT)) || |
| 246 |
(lower == -1 && (this->DirichletBC_ & DIR_BOTTOM)) || |
| 247 |
(upper == -1 && (this->DirichletBC_ & DIR_TOP)); |
| 248 |
|
| 249 |
if (isDirichlet && keepBCs) { |
| 250 |
// Dirichlet unknown we want to keep |
| 251 |
inds[n] = center; |
| 252 |
vals[n++] = Teuchos::ScalarTraits<Scalar>::one(); |
| 253 |
|
| 254 |
} else { |
| 255 |
// The Neumann b.c. are treated in a sane way. The Dirichlet b.c., however, are treated |
| 256 |
// insane when the option keepBCs=false. Speicifically, in this case we don't want to keep |
| 257 |
// Dirichlet b.c., but that would result in inconsistency between the map and the number of |
| 258 |
// degrees of freedom, plus the problem with GIDs. Therefore, we virtually expand domain by |
| 259 |
// one node in the direction of the Dirichlet b.c., and then assume that that node was |
| 260 |
// not kept. But we use an old GIDs. So yes, that's weird. |
| 261 |
|
| 262 |
if (left != -1) { |
| 263 |
inds[n] = left; |
| 264 |
vals[n++] = b1 * diff(x1, x2) + b2 * diff_primex(x1, x2) - b2 * advx(x1, x2); |
| 265 |
} |
| 266 |
if (right != -1) { |
| 267 |
inds[n] = right; |
| 268 |
vals[n++] = c1 * diff(x1, x2) - c2 * diff_primex(x1, x2) + c2 * advx(x1, x2); |
| 269 |
} |
| 270 |
if (lower != -1) { |
| 271 |
inds[n] = lower; |
| 272 |
vals[n++] = d1 * diff(x1, x2) + d2 * diff_primey(x1, x2) - d2 * advy(x1, x2); |
| 273 |
} |
| 274 |
if (upper != -1) { |
| 275 |
inds[n] = upper; |
| 276 |
vals[n++] = e1 * diff(x1, x2) - e2 * diff_primey(x1, x2) + e2 * advy(x1, x2); |
| 277 |
} |
| 278 |
|
| 279 |
// diagonal |
| 280 |
Scalar z = a1 * diff(x1, x2) + reac(x1, x2); |
| 281 |
if (Galeri::Xpetra::IsBoundary2d(center, nx, ny) && !isDirichlet) { |
| 282 |
// Neumann boundary unknown (diagonal = sum of all offdiagonal) |
| 283 |
z = Teuchos::ScalarTraits<Scalar>::zero(); |
| 284 |
for (size_t j = 0; j < n; j++) |
| 285 |
z -= vals[j]; |
| 286 |
} |
| 287 |
inds[n] = center; |
| 288 |
vals[n++] = z; |
| 289 |
} |
| 290 |
|
| 291 |
for (size_t j = 0; j < n; j++) |
| 292 |
inds[j] += indexBase; |
| 293 |
|
| 294 |
Teuchos::ArrayView<GlobalOrdinal> iv(&inds[0], n); |
| 295 |
Teuchos::ArrayView<Scalar> av(&vals[0], n); |
| 296 |
mtx->insertGlobalValues(myGlobalElements[i], iv, av); |
| 297 |
} |
| 298 |
|
| 299 |
mtx->fillComplete(); |
| 300 |
this->A_ = mtx; |
| 301 |
|
| 302 |
this->A_->setObjectLabel(this->getObjectLabel()); |
| 303 |
return this->A_; |
| 304 |
} |
| 305 |
|
| 306 |
// ============================================= ADR3D ============================================= |
| 307 |
template <typename Scalar, typename LocalOrdinal, typename GlobalOrdinal, typename Map, typename Matrix, typename MultiVector> |
| 308 |
class ADR3DProblem : public Problem<Map, Matrix, MultiVector> { |
| 309 |
public: |
| 310 |
ADR3DProblem(Teuchos::ParameterList& list, const Teuchos::RCP<const Map>& map) |
| 311 |
: Problem<Map, Matrix, MultiVector>(list, map) {} |
| 312 |
Teuchos::RCP<Matrix> BuildMatrix(); |
| 313 |
|
| 314 |
private: |
| 315 |
// Function that defines the diffusion coefficient |
| 316 |
inline Scalar diff(Scalar x, Scalar y, Scalar z) { return 1.0 + 0.0 * x + 0.0 * y + 0.0 * z; }; |
| 317 |
|
| 318 |
// Function that defines the first derivative in x-direction of the diffusion coefficient |
| 319 |
inline Scalar diff_primex(Scalar x, Scalar y, Scalar z) { return 0.0 + 0.0 * x + 0.0 * y + 0.0 * z; }; |
| 320 |
|
| 321 |
// Function that defines the first derivative in x-direction of the diffusion coefficient |
| 322 |
inline Scalar diff_primey(Scalar x, Scalar y, Scalar z) { return 0.0 + 0.0 * x + 0.0 * y + 0.0 * z; }; |
| 323 |
|
| 324 |
// Function that defines the first derivative in x-direction of the diffusion coefficient |
| 325 |
inline Scalar diff_primez(Scalar x, Scalar y, Scalar z) { return 0.0 + 0.0 * x + 0.0 * y + 0.0 * z; }; |
| 326 |
|
| 327 |
// Function that defines the advection coefficient in the x-direction |
| 328 |
inline Scalar advx(Scalar x, Scalar y, Scalar z) { return 10.0 + 0.0 * x + 0.0 * y + 0.0 * z; }; |
| 329 |
|
| 330 |
// Function that defines the advection coefficient in the x-direction |
| 331 |
inline Scalar advy(Scalar x, Scalar y, Scalar z) { return 10.0 + 0.0 * x + 0.0 * y + 0.0 * z; }; |
| 332 |
|
| 333 |
// Function that defines the advection coefficient in the x-direction |
| 334 |
inline Scalar advz(Scalar x, Scalar y, Scalar z) { return 10.0 + 0.0 * x + 0.0 * y + 0.0 * z; }; |
| 335 |
|
| 336 |
// Function taht defines the reaction coefficient |
| 337 |
inline Scalar reac(Scalar x, Scalar y, Scalar z) { return 0.0 + 0.0 * x + 0.0 * y + 0.0 * z; }; |
| 338 |
}; |
| 339 |
|
| 340 |
template <typename Scalar, typename LocalOrdinal, typename GlobalOrdinal, typename Map, typename Matrix, typename MultiVector> |
| 341 |
Teuchos::RCP<Matrix> ADR3DProblem<Scalar, LocalOrdinal, GlobalOrdinal, Map, Matrix, MultiVector>::BuildMatrix() { |
| 342 |
GlobalOrdinal nx = this->list_.get("nx", (GlobalOrdinal)-1); |
| 343 |
GlobalOrdinal ny = this->list_.get("ny", (GlobalOrdinal)-1); |
| 344 |
GlobalOrdinal nz = this->list_.get("nz", (GlobalOrdinal)-1); |
| 345 |
double one = 1.0; |
| 346 |
Scalar stretchx = (Scalar)this->list_.get("stretchx", one); |
| 347 |
Scalar stretchy = (Scalar)this->list_.get("stretchy", one); |
| 348 |
Scalar stretchz = (Scalar)this->list_.get("stretchz", one); |
| 349 |
|
| 350 |
if (nx == -1 || ny == -1 || nz == -1) { |
| 351 |
GlobalOrdinal n = this->Map_->getGlobalNumElements(); |
| 352 |
nx = (GlobalOrdinal)Teuchos::ScalarTraits<double>::pow(n, 0.33334); |
| 353 |
ny = nx; |
| 354 |
nz = nx; |
| 355 |
TEUCHOS_TEST_FOR_EXCEPTION(nx * ny * nz != n, std::logic_error, "You need to specify nx, ny, and nz"); |
| 356 |
} |
| 357 |
bool keepBCs = this->list_.get("keepBCs", false); |
| 358 |
|
| 359 |
// Diffusion stencil |
| 360 |
Scalar c1 = (Scalar)-one / (stretchx * stretchx); |
| 361 |
Scalar b1 = (Scalar)-one / (stretchx * stretchx); |
| 362 |
Scalar g1 = (Scalar)-one / (stretchy * stretchy); |
| 363 |
Scalar f1 = (Scalar)-one / (stretchy * stretchy); |
| 364 |
Scalar e1 = (Scalar)-one / (stretchz * stretchz); |
| 365 |
Scalar d1 = (Scalar)-one / (stretchz * stretchz); |
| 366 |
Scalar a1 = -(c1 + b1 + g1 + f1 + e1 + d1); |
| 367 |
|
| 368 |
// Advection stencil |
| 369 |
Scalar c2 = (Scalar)one / (2.0 * stretchx); |
| 370 |
Scalar b2 = (Scalar)one / (2.0 * stretchx); |
| 371 |
Scalar g2 = (Scalar)one / (2.0 * stretchy); |
| 372 |
Scalar f2 = (Scalar)one / (2.0 * stretchy); |
| 373 |
Scalar e2 = (Scalar)one / (2.0 * stretchz); |
| 374 |
Scalar d2 = (Scalar)one / (2.0 * stretchz); |
| 375 |
|
| 376 |
// this->A_ = Cross3D<Scalar,LocalOrdinal,GlobalOrdinal,Map,Matrix>(this->Map_, nx, ny, nz, center, left, right, front, back, down, up, this->DirichletBC_, keepBCs); |
| 377 |
LocalOrdinal nnz = 7; |
| 378 |
|
| 379 |
Teuchos::RCP<Matrix> mtx = Galeri::Xpetra::MatrixTraits<Map, Matrix>::Build(this->Map_, nnz); |
| 380 |
|
| 381 |
LocalOrdinal numMyElements = this->Map_->getLocalNumElements(); |
| 382 |
GlobalOrdinal indexBase = this->Map_->getIndexBase(); |
| 383 |
|
| 384 |
Teuchos::ArrayView<const GlobalOrdinal> myGlobalElements = (this->Map_)->getLocalElementList(); |
| 385 |
|
| 386 |
GlobalOrdinal center, left, right, bottom, top, front, back; |
| 387 |
std::vector<GlobalOrdinal> inds(nnz); |
| 388 |
std::vector<Scalar> vals(nnz); |
| 389 |
|
| 390 |
// e |
| 391 |
// b a c |
| 392 |
// d |
| 393 |
// + f bottom and g top |
| 394 |
for (LocalOrdinal i = 0; i < numMyElements; ++i) { |
| 395 |
size_t n = 0; |
| 396 |
|
| 397 |
center = myGlobalElements[i] - indexBase; |
| 398 |
|
| 399 |
// Determine coordinates |
| 400 |
Scalar x3 = (Scalar)(std::floor(center / (nx * ny))) * stretchz; |
| 401 |
int plane = center % (nx * ny); |
| 402 |
Scalar x1 = (Scalar)(plane % nx) * stretchx; |
| 403 |
Scalar x2 = (Scalar)(std::floor(plane / nx)) * stretchy; |
| 404 |
|
| 405 |
Galeri::Xpetra::GetNeighboursCartesian3d(center, nx, ny, nz, |
| 406 |
left, right, front, back, bottom, top); |
| 407 |
|
| 408 |
bool isDirichlet = (left == -1 && (this->DirichletBC_ & DIR_LEFT)) || |
| 409 |
(right == -1 && (this->DirichletBC_ & DIR_RIGHT)) || |
| 410 |
(front == -1 && (this->DirichletBC_ & DIR_BOTTOM)) || |
| 411 |
(back == -1 && (this->DirichletBC_ & DIR_TOP)) || |
| 412 |
(front == -1 && (this->DirichletBC_ & DIR_FRONT)) || |
| 413 |
(back == -1 && (this->DirichletBC_ & DIR_BACK)); |
| 414 |
|
| 415 |
if (isDirichlet && keepBCs) { |
| 416 |
// Dirichlet unknown we want to keep |
| 417 |
inds[n] = center; |
| 418 |
vals[n++] = Teuchos::ScalarTraits<Scalar>::one(); |
| 419 |
|
| 420 |
} else { |
| 421 |
// See comments about weird in Cross2D |
| 422 |
if (left != -1) { |
| 423 |
inds[n] = left; |
| 424 |
vals[n++] = b1 * diff(x1, x2, x3) + b2 * diff_primex(x1, x2, x3) - b2 * advx(x1, x2, x3); |
| 425 |
} |
| 426 |
if (right != -1) { |
| 427 |
inds[n] = right; |
| 428 |
vals[n++] = c1 * diff(x1, x2, x3) - c2 * diff_primex(x1, x2, x3) + c2 * advx(x1, x2, x3); |
| 429 |
} |
| 430 |
if (front != -1) { |
| 431 |
inds[n] = front; |
| 432 |
vals[n++] = d1 * diff(x1, x2, x3) + d2 * diff_primey(x1, x2, x3) - d2 * advy(x1, x2, x3); |
| 433 |
} |
| 434 |
if (back != -1) { |
| 435 |
inds[n] = back; |
| 436 |
vals[n++] = e1 * diff(x1, x2, x3) - e2 * diff_primey(x1, x2, x3) + e2 * advy(x1, x2, x3); |
| 437 |
} |
| 438 |
if (bottom != -1) { |
| 439 |
inds[n] = bottom; |
| 440 |
vals[n++] = f1 * diff(x1, x2, x3) + f2 * diff_primez(x1, x2, x3) - f2 * advz(x1, x2, x3); |
| 441 |
} |
| 442 |
if (top != -1) { |
| 443 |
inds[n] = top; |
| 444 |
vals[n++] = g1 * diff(x1, x2, x3) - g2 * diff_primez(x1, x2, x3) + g2 * advz(x1, x2, x3); |
| 445 |
} |
| 446 |
|
| 447 |
// diagonal |
| 448 |
Scalar z = a1 * diff(x1, x2, x3) + reac(x1, x2, x3); |
| 449 |
if (Galeri::Xpetra::IsBoundary3d(center, nx, ny, nz) && !isDirichlet) { |
| 450 |
// Neumann boundary unknown (diagonal = sum of all offdiagonal) |
| 451 |
z = Teuchos::ScalarTraits<Scalar>::zero(); |
| 452 |
for (size_t j = 0; j < n; j++) |
| 453 |
z -= vals[j]; |
| 454 |
} |
| 455 |
inds[n] = center; |
| 456 |
vals[n++] = z; |
| 457 |
} |
| 458 |
|
| 459 |
for (size_t j = 0; j < n; j++) |
| 460 |
inds[j] += indexBase; |
| 461 |
|
| 462 |
Teuchos::ArrayView<GlobalOrdinal> iv(&inds[0], n); |
| 463 |
Teuchos::ArrayView<Scalar> av(&vals[0], n); |
| 464 |
mtx->insertGlobalValues(myGlobalElements[i], iv, av); |
| 465 |
} |
| 466 |
|
| 467 |
mtx->fillComplete(); |
| 468 |
this->A_ = mtx; |
| 469 |
|
| 470 |
this->A_->setObjectLabel(this->getObjectLabel()); |
| 471 |
return this->A_; |
| 472 |
} |
| 473 |
|
| 474 |
} // namespace Xpetra |
| 475 |
|
| 476 |
} // namespace ADR |
| 477 |
|
| 478 |
#endif // CREATEADRMATRIX_HPP |