@@ -432,7 +432,8 @@ class CSysMatrix {
432432 }
433433
434434 /* !
435- * \brief Set the value of a block in the sparse matrix with scaling.
435+ * \brief Set the value of a block (in flat format) in the sparse matrix with scaling.
436+ * \note If the template param Overwrite is false we add to the block (bij += alpha*b).
436437 * \param[in] block_i - Row index.
437438 * \param[in] block_j - Column index.
438439 * \param[in] val_block - Block to set to A(i, j).
@@ -451,7 +452,7 @@ class CSysMatrix {
451452 }
452453
453454 /* !
454- * \brief Add a scaled block (in flat format) to the sparse matrix.
455+ * \brief Add a scaled block (in flat format) to the sparse matrix (see SetBlock) .
455456 * \param[in] block_i - Row index.
456457 * \param[in] block_j - Column index.
457458 * \param[in] val_block - Block to set to A(i, j).
@@ -460,55 +461,61 @@ class CSysMatrix {
460461 template <class OtherType ,
461462 typename enable_if<!is_pointer<OtherType>::value,bool >::type = 0 >
462463 inline void AddBlock (unsigned long block_i, unsigned long block_j,
463- OtherType alpha, const OtherType *val_block) {
464+ const OtherType *val_block, OtherType alpha = 1.0 ) {
464465 SetBlock<OtherType,false >(block_i, block_j, val_block, alpha);
465466 }
466467
467468 /* !
468- * \brief Set the value of a block in the sparse matrix.
469+ * \brief Set the value of a scaled block in the sparse matrix.
470+ * \note If the template param Overwrite is false we add to the block (bij += alpha*b).
469471 * \param[in] block_i - Row index.
470472 * \param[in] block_j - Column index.
471473 * \param[in] val_block - Block to set to A(i, j).
474+ * \param[in] alpha - Scale factor.
472475 */
473- template <class OtherType , int Sign = 1 , bool Overwrite = true >
474- inline void SetBlock (unsigned long block_i, unsigned long block_j, const OtherType* const * val_block) {
476+ template <class OtherType , bool Overwrite = true >
477+ inline void SetBlock (unsigned long block_i, unsigned long block_j,
478+ const OtherType* const * val_block, OtherType alpha = 1.0 ) {
475479
476480 auto mat_ij = GetBlock (block_i, block_j);
477481 if (!mat_ij) return ;
478482 for (auto iVar = 0ul ; iVar < nVar; ++iVar) {
479483 for (auto jVar = 0ul ; jVar < nEqn; ++jVar) {
480- *mat_ij = (Overwrite? ScalarType (0 ) : *mat_ij) + Sign * PassiveAssign ( val_block[iVar][jVar]);
484+ *mat_ij = (Overwrite? ScalarType (0 ) : *mat_ij) + PassiveAssign (alpha * val_block[iVar][jVar]);
481485 ++mat_ij;
482486 }
483487 }
484488 }
485489
486490 /* !
487- * \brief Adds the specified block to the sparse matrix.
491+ * \brief Adds a scaled block to the sparse matrix (see SetBlock) .
488492 * \param[in] block_i - Row index.
489493 * \param[in] block_j - Column index.
490494 * \param[in] val_block - Block to add to A(i, j).
495+ * \param[in] alpha - Scale factor.
491496 */
492497 template <class OtherType >
493- inline void AddBlock (unsigned long block_i, unsigned long block_j, const OtherType* const * val_block) {
494- SetBlock<OtherType,1 ,false >(block_i, block_j, val_block);
498+ inline void AddBlock (unsigned long block_i, unsigned long block_j,
499+ const OtherType* const * val_block, OtherType alpha = 1.0 ) {
500+ SetBlock<OtherType,false >(block_i, block_j, val_block, alpha);
495501 }
496502
497503 /* !
498- * \brief Subtracts the specified block to the sparse matrix.
504+ * \brief Subtracts the specified block to the sparse matrix (see AddBlock) .
499505 * \param[in] block_i - Row index.
500506 * \param[in] block_j - Column index.
501507 * \param[in] val_block - Block to subtract to A(i, j).
502508 */
503509 template <class OtherType >
504510 inline void SubtractBlock (unsigned long block_i, unsigned long block_j, const OtherType* const * val_block) {
505- SetBlock<OtherType,- 1 , false > (block_i, block_j, val_block);
511+ AddBlock (block_i, block_j, val_block, OtherType (- 1 ) );
506512 }
507513
508514 /* !
509515 * \brief Update 4 blocks ii, ij, ji, jj (add to i* sub from j*).
510516 * \note The template parameter Sign, can be used create a "subtractive"
511517 * update i.e. subtract from row i and add to row j instead.
518+ * This method assumes an FVM-type sparse pattern.
512519 * \param[in] edge - Index of edge that connects iPoint and jPoint.
513520 * \param[in] iPoint - Row to which we add the blocks.
514521 * \param[in] jPoint - Row from which we subtract the blocks.
@@ -547,17 +554,18 @@ class CSysMatrix {
547554 }
548555
549556 /* !
550- * \brief Update 2 blocks ij and ji (add to i* sub from j*).
557+ * \brief Sets 2 blocks ij and ji (add to i* sub from j*) associated with
558+ * one edge of an FVM-type sparse pattern.
551559 * \note The template parameter Sign, can be used create a "subtractive"
552560 * update i.e. subtract from row i and add to row j instead.
553561 * The parameter Overwrite allows completely writing over the
554- * current values held by the matrix.
562+ * current values held by the matrix (true), or updating them (false) .
555563 * \param[in] edge - Index of edge that connects iPoint and jPoint.
556564 * \param[in] block_i - Subs from ji.
557565 * \param[in] block_j - Adds to ij.
558566 */
559- template <class OtherType , int Sign = 1 , bool Overwrite = false >
560- inline void UpdateBlocks (unsigned long iEdge, const OtherType* const * block_i, const OtherType* const * block_j) {
567+ template <class OtherType , int Sign = 1 , bool Overwrite = true >
568+ inline void SetBlocks (unsigned long iEdge, const OtherType* const * block_i, const OtherType* const * block_j) {
561569
562570 ScalarType *bij = &matrix[edge_ptr (iEdge,0 )*nVar*nEqn];
563571 ScalarType *bji = &matrix[edge_ptr (iEdge,1 )*nVar*nEqn];
@@ -574,46 +582,55 @@ class CSysMatrix {
574582 }
575583
576584 /* !
577- * \brief Short-hand for the "subtractive " version (sub from i* add to j*) of UpdateBlocks .
585+ * \brief Short-hand for the "additive overwrite " version of SetBlocks .
578586 */
579587 template <class OtherType >
580- inline void UpdateBlocksSub (unsigned long iEdge, const OtherType* const * block_i, const OtherType* const * block_j) {
581- UpdateBlocks <OtherType,- 1 >(iEdge, block_i, block_j);
588+ inline void UpdateBlocks (unsigned long iEdge, const OtherType* const * block_i, const OtherType* const * block_j) {
589+ SetBlocks <OtherType,1 , false >(iEdge, block_i, block_j);
582590 }
583591
584592 /* !
585- * \brief Short-hand for the "additive overwrite " version of UpdateBlocks .
593+ * \brief Short-hand for the "subtractive " version (sub from i* add to j*) of SetBlocks .
586594 */
587595 template <class OtherType >
588- inline void SetBlocks (unsigned long iEdge, const OtherType* const * block_i, const OtherType* const * block_j) {
589- UpdateBlocks <OtherType,1 , true >(iEdge, block_i, block_j);
596+ inline void UpdateBlocksSub (unsigned long iEdge, const OtherType* const * block_i, const OtherType* const * block_j) {
597+ SetBlocks <OtherType,- 1 , false >(iEdge, block_i, block_j);
590598 }
591599
592600 /* !
593- * \brief Adds the specified block to the (i, i) subblock of the matrix-by-blocks structure.
601+ * \brief Sets the specified block to the (i, i) subblock of the sparse matrix.
602+ * Scales the input block by factor alpha. If the Overwrite parameter is
603+ * false we update instead (bii += alpha*b).
594604 * \param[in] block_i - Diagonal index.
595605 * \param[in] val_block - Block to add to the diagonal of the matrix.
606+ * \param[in] alpha - Scale factor.
596607 */
597- template <class OtherType , int Sign = 1 >
598- inline void AddBlock2Diag (unsigned long block_i, const OtherType* const * val_block) {
608+ template <class OtherType , bool Overwrite = true >
609+ inline void SetBlock2Diag (unsigned long block_i, const OtherType* const * val_block, OtherType alpha = 1.0 ) {
599610
600- ScalarType *bii = &matrix[dia_ptr[block_i]*nVar*nEqn];
611+ auto mat_ii = &matrix[dia_ptr[block_i]*nVar*nEqn];
601612
602- unsigned long iVar, jVar, offset = 0 ;
613+ for (auto iVar = 0ul ; iVar < nVar; iVar++)
614+ for (auto jVar = 0ul ; jVar < nEqn; jVar++) {
615+ *mat_ii = (Overwrite? ScalarType (0 ) : *mat_ii) + PassiveAssign (alpha * val_block[iVar][jVar]);
616+ ++mat_ii;
617+ }
618+ }
603619
604- for (iVar = 0 ; iVar < nVar; iVar++)
605- for (jVar = 0 ; jVar < nEqn; jVar++)
606- bii[offset++] += Sign * PassiveAssign (val_block[iVar][jVar]);
620+ /* !
621+ * \brief Non overwrite version of SetBlock2Diag, also with scaling.
622+ */
623+ template <class OtherType >
624+ inline void AddBlock2Diag (unsigned long block_i, const OtherType* const * val_block, OtherType alpha = 1.0 ) {
625+ SetBlock2Diag<OtherType,false >(block_i, val_block, alpha);
607626 }
608627
609628 /* !
610- * \brief Subtracts the specified block from the (i, i) subblock of the matrix-by-blocks structure.
611- * \param[in] block_i - Diagonal index.
612- * \param[in] val_block - Block to subtract from the diagonal of the matrix.
629+ * \brief Short-hand to AddBlock2Diag with alpha = -1, i.e. subtracts from the current diagonal.
613630 */
614631 template <class OtherType >
615632 inline void SubtractBlock2Diag (unsigned long block_i, const OtherType* const * val_block) {
616- AddBlock2Diag<OtherType,- 1 > (block_i, val_block);
633+ AddBlock2Diag (block_i, val_block, OtherType (- 1 ) );
617634 }
618635
619636 /* !
0 commit comments