MatrixI.H 11.6 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2019 OpenCFD Ltd.
6
     \\/     M anipulation  |
OpenFOAM bot's avatar
OpenFOAM bot committed
7 8
-------------------------------------------------------------------------------
                            | Copyright (C) 2011-2016 OpenFOAM Foundation
9 10 11 12
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

13 14 15 16
    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
17 18 19 20 21 22 23

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
24
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
25 26 27

\*---------------------------------------------------------------------------*/

28 29
#include "MatrixBlock.H"

30 31 32 33 34 35 36 37 38 39 40 41 42 43
// * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //

template<class Form, class Type>
inline void Foam::Matrix<Form, Type>::doAlloc()
{
    const label len = size();

    if (len)
    {
        v_ = new Type[len];
    }
}


44 45
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

46 47
template<class Form, class Type>
inline Foam::Matrix<Form, Type>::Matrix()
48
:
49
    mRows_(0),
50
    nCols_(0),
51
    v_(nullptr)
52 53 54
{}


55
template<class Form, class Type>
56 57
inline Foam::autoPtr<Foam::Matrix<Form, Type>>
Foam::Matrix<Form, Type>::clone() const
58
{
59
    return autoPtr<Matrix<Form, Type>>::New(*this);
60 61 62 63 64
}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

65 66 67
template<class Form, class Type>
inline const Foam::Matrix<Form, Type>& Foam::Matrix<Form, Type>::null()
{
68
    return NullObjectRef<Matrix<Form, Type>>();
69 70 71
}


72
template<class Form, class Type>
73
inline Foam::label Foam::Matrix<Form, Type>::m() const noexcept
74
{
75
    return mRows_;
76 77 78
}


79
template<class Form, class Type>
80
inline Foam::label Foam::Matrix<Form, Type>::n() const noexcept
81
{
82
    return nCols_;
83 84 85
}


86 87
template<class Form, class Type>
inline Foam::label Foam::Matrix<Form, Type>::size() const
88
{
89 90 91 92 93 94 95 96
    return mRows_ * nCols_;
}


template<class Form, class Type>
inline bool Foam::Matrix<Form, Type>::empty() const noexcept
{
    return !mRows_ || !nCols_;
97 98 99
}


100 101
template<class Form, class Type>
inline void Foam::Matrix<Form, Type>::checki(const label i) const
102
{
103
    #ifdef FULLDEBUG
104
    if (!mRows_ || !nCols_)
105
    {
106
        FatalErrorInFunction
107
            << "Attempt to access element from empty matrix"
108 109
            << abort(FatalError);
    }
110
    if (i < 0 || i >= mRows_)
111
    {
112
        FatalErrorInFunction
113
            << "Index " << i << " out of range 0 ... " << mRows_-1
114 115
            << abort(FatalError);
    }
116
    #endif
117 118 119
}


120 121
template<class Form, class Type>
inline void Foam::Matrix<Form, Type>::checkj(const label j) const
122
{
123
    #ifdef FULLDEBUG
124
    if (!mRows_ || !nCols_)
125
    {
126
        FatalErrorInFunction
127
            << "Attempt to access element from empty matrix"
128 129
            << abort(FatalError);
    }
130
    if (j < 0 || j >= nCols_)
131
    {
132
        FatalErrorInFunction
133
            << "index " << j << " out of range 0 ... " << nCols_-1
134 135
            << abort(FatalError);
    }
136
    #endif
137 138 139
}


140
template<class Form, class Type>
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
inline void Foam::Matrix<Form, Type>::checkSize() const
{
    if (mRows_ < 0 || nCols_ < 0)
    {
        FatalErrorInFunction
            << "Incorrect size (" << mRows_ << ", " << nCols_ << ')' << nl
            << abort(FatalError);
    }
    // Could also check for odd sizes, like (0, N) and make (0,0)
}


template<class Form, class Type>
inline bool Foam::Matrix<Form, Type>::uniform() const
{
    const label len = size();

    if (len == 0)
    {
        return false;
    }

163
    for (label idx = 1; idx < len; ++idx)
164 165 166 167 168 169 170 171 172 173 174 175 176
    {
        if (v_[0] != v_[idx])
        {
            return false;
        }
    }

    return true;
}


template<class Form, class Type>
inline const Type* Foam::Matrix<Form, Type>::cdata() const noexcept
177 178 179 180 181 182
{
    return v_;
}


template<class Form, class Type>
183
inline Type* Foam::Matrix<Form, Type>::data() noexcept
184 185 186 187 188
{
    return v_;
}


189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
template<class Form, class Type>
inline const Type* Foam::Matrix<Form, Type>::rowData(const label irow) const
{
    #ifdef FULLDEBUG
    checki(irow);
    #endif
    return (v_ + irow*nCols_);
}


template<class Form, class Type>
inline Type* Foam::Matrix<Form, Type>::rowData(const label irow)
{
    #ifdef FULLDEBUG
    checki(irow);
    #endif
    return (v_ + irow*nCols_);
}


template<class Form, class Type>
inline const Type& Foam::Matrix<Form, Type>::at(const label idx) const
{
    #ifdef FULLDEBUG
    if (idx < 0 || idx >= this->size())
    {
        FatalErrorInFunction
            << "index " << idx << " out of range 0 ... " << this->size()
            << abort(FatalError);
    }
    #endif
    return (v_ + idx);
}


template<class Form, class Type>
inline Type& Foam::Matrix<Form, Type>::at(const label idx)
{
    #ifdef FULLDEBUG
    if (idx < 0 || idx >= this->size())
    {
        FatalErrorInFunction
            << "index " << idx << " out of range 0 ... " << this->size()
            << abort(FatalError);
    }
    #endif
    return (v_ + idx);
}


239
template<class Form, class Type>
240
inline Foam::ConstMatrixBlock<Foam::Matrix<Form, Type>>
241 242 243 244 245 246 247 248
Foam::Matrix<Form, Type>::block
(
    const label m,
    const label n,
    const label mStart,
    const label nStart
) const
{
249
    return ConstMatrixBlock<mType>
250 251 252 253 254 255 256 257 258 259 260 261
    (
        *this,
        m,
        n,
        mStart,
        nStart
    );
}


template<class Form, class Type>
template<class VectorSpace>
262
inline Foam::ConstMatrixBlock<Foam::Matrix<Form, Type>>
263 264 265 266 267 268
Foam::Matrix<Form, Type>::block
(
    const label mStart,
    const label nStart
) const
{
269
    return ConstMatrixBlock<mType>
270 271
    (
        *this,
272
        VectorSpace::mRows,
273 274 275 276 277 278 279 280
        VectorSpace::nCols,
        mStart,
        nStart
    );
}


template<class Form, class Type>
281
inline Foam::ConstMatrixBlock<Foam::Matrix<Form, Type>>
282 283 284 285 286 287
Foam::Matrix<Form, Type>::col
(
    const label m,
    const label mStart
) const
{
288
    return ConstMatrixBlock<mType>
289 290 291 292 293 294 295 296 297 298 299
    (
        *this,
        m,
        1,
        mStart,
        0
    );
}


template<class Form, class Type>
300
inline Foam::ConstMatrixBlock<Foam::Matrix<Form, Type>>
301 302 303 304 305 306 307
Foam::Matrix<Form, Type>::col
(
    const label m,
    const label mStart,
    const label nStart
) const
{
308
    return ConstMatrixBlock<mType>
309 310 311 312 313 314 315 316 317 318 319
    (
        *this,
        m,
        1,
        mStart,
        nStart
    );
}


template<class Form, class Type>
320
inline Foam::MatrixBlock<Foam::Matrix<Form, Type>>
321 322 323 324 325 326 327 328
Foam::Matrix<Form, Type>::block
(
    const label m,
    const label n,
    const label mStart,
    const label nStart
)
{
329
    return MatrixBlock<mType>
330 331 332 333 334 335 336 337 338 339 340 341
    (
        *this,
        m,
        n,
        mStart,
        nStart
    );
}


template<class Form, class Type>
template<class VectorSpace>
342
inline Foam::MatrixBlock<Foam::Matrix<Form, Type>>
343 344
Foam::Matrix<Form, Type>::block(const label mStart, const label nStart)
{
345
    return MatrixBlock<mType>
346 347
    (
        *this,
348
        VectorSpace::mRows,
349 350 351 352 353 354 355 356
        VectorSpace::nCols,
        mStart,
        nStart
    );
}


template<class Form, class Type>
357
inline Foam::MatrixBlock<Foam::Matrix<Form, Type>>
358 359
Foam::Matrix<Form, Type>::col(const label m, const label mStart)
{
360
    return MatrixBlock<mType>
361 362 363 364 365 366 367 368 369 370 371
    (
        *this,
        m,
        1,
        mStart,
        0
    );
}


template<class Form, class Type>
372
inline Foam::MatrixBlock<Foam::Matrix<Form, Type>>
373 374 375 376 377 378 379
Foam::Matrix<Form, Type>::col
(
    const label m,
    const label mStart,
    const label nStart
)
{
380
    return MatrixBlock<mType>
381 382 383 384 385 386 387 388 389 390
    (
        *this,
        m,
        1,
        mStart,
        nStart
    );
}


391 392 393 394 395 396 397
template<class Form, class Type>
inline void Foam::Matrix<Form, Type>::setSize(const label m, const label n)
{
    resize(m, n);
}


398 399 400 401 402 403 404 405
template<class Form, class Type>
void Foam::Matrix<Form, Type>::shallowResize(const label m, const label n)
{
    mRows_ = m;
    nCols_ = n;
}


406
template<class Form, class Type>
407
inline Foam::tmp<Foam::Field<Type>> Foam::Matrix<Form, Type>::Amul
408
(
409
    const UList<Type>& x
410 411
) const
{
412
    return this->AmulImpl(x);
413 414 415 416 417
}


template<class Form, class Type>
template<class Addr>
418
inline Foam::tmp<Foam::Field<Type>> Foam::Matrix<Form, Type>::Amul
419
(
420
    const IndirectListBase<Type, Addr>& x
421 422
) const
{
423
    return this->AmulImpl(x);
424 425 426 427
}


template<class Form, class Type>
428
inline Foam::tmp<Foam::Field<Type>> Foam::Matrix<Form, Type>::Tmul
429
(
430
    const UList<Type>& x
431 432
) const
{
433
    return this->TmulImpl(x);
434 435 436 437 438
}


template<class Form, class Type>
template<class Addr>
439
inline Foam::tmp<Foam::Field<Type>> Foam::Matrix<Form, Type>::Tmul
440
(
441
    const IndirectListBase<Type, Addr>& x
442 443
) const
{
444
    return this->TmulImpl(x);
445 446 447
}


448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
// * * * * * * * * * * * * * * * * * Iterators * * * * * * * * * * * * * * * //

template<class Form, class Type>
inline typename Foam::Matrix<Form, Type>::iterator
Foam::Matrix<Form, Type>::begin()
{
    return v_;
}


template<class Form, class Type>
inline typename Foam::Matrix<Form, Type>::iterator
Foam::Matrix<Form, Type>::end()
{
    return v_ + (mRows_ * nCols_);
}


template<class Form, class Type>
inline typename Foam::Matrix<Form, Type>::const_iterator
Foam::Matrix<Form, Type>::cbegin() const
{
    return v_;
}


template<class Form, class Type>
inline typename Foam::Matrix<Form, Type>::const_iterator
Foam::Matrix<Form, Type>::cend() const
{
    return v_ + (mRows_ * nCols_);
}


template<class Form, class Type>
inline typename Foam::Matrix<Form, Type>::const_iterator
Foam::Matrix<Form, Type>::begin() const
{
    return v_;
}


template<class Form, class Type>
inline typename Foam::Matrix<Form, Type>::const_iterator
Foam::Matrix<Form, Type>::end() const
{
    return v_ + (mRows_ * nCols_);
}


498 499
// * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //

500 501 502
template<class Form, class Type>
inline const Type& Foam::Matrix<Form, Type>::operator()
(
503 504
    const label irow,
    const label jcol
505
) const
506
{
507 508 509
    checki(irow);
    checkj(jcol);
    return v_[irow*nCols_ + jcol];
510 511 512 513 514 515
}


template<class Form, class Type>
inline Type& Foam::Matrix<Form, Type>::operator()
(
516 517
    const label irow,
    const label jcol
518 519
)
{
520 521 522
    checki(irow);
    checkj(jcol);
    return v_[irow*nCols_ + jcol];
523 524 525
}


526
template<class Form, class Type>
527
inline const Type* Foam::Matrix<Form, Type>::operator[](const label irow) const
528
{
529 530
    checki(irow);
    return v_ + irow*nCols_;
531 532 533 534
}


template<class Form, class Type>
535
inline Type* Foam::Matrix<Form, Type>::operator[](const label irow)
536
{
537 538
    checki(irow);
    return v_ + irow*nCols_;
539 540 541
}


542 543 544 545 546 547 548
template<class Form, class Type>
inline Foam::tmp<Foam::Field<Type>> Foam::operator*
(
    const Matrix<Form, Type>& mat,
    const UList<Type>& x
)
{
549
    return mat.Amul(x);
550 551 552 553 554 555 556 557 558 559
}


template<class Form, class Type, class Addr>
inline Foam::tmp<Foam::Field<Type>> Foam::operator*
(
    const Matrix<Form, Type>& mat,
    const IndirectListBase<Type, Addr>& x
)
{
560
    return mat.Amul(x);
561 562 563 564 565 566 567 568 569 570
}


template<class Form, class Type>
inline Foam::tmp<Foam::Field<Type>> Foam::operator*
(
    const UList<Type>& x,
    const Matrix<Form, Type>& mat
)
{
571
    return mat.Tmul(x);
572 573 574 575 576 577 578 579 580 581
}


template<class Form, class Type, class Addr>
inline Foam::tmp<Foam::Field<Type>> Foam::operator*
(
    const IndirectListBase<Type, Addr>& x,
    const Matrix<Form, Type>& mat
)
{
582
    return mat.Tmul(x);
583 584 585
}


586
// ************************************************************************* //