| 1 |
# SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
# SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception |
| 3 |
|
| 4 |
import dune.common |
| 5 |
|
| 6 |
from ._istl import BCRSMatrix as BCRSMatrix11 |
| 7 |
from ._istl import BlockVector as BlockVector1 |
| 8 |
from ._istl import * |
| 9 |
|
| 10 |
from dune.generator.generator import SimpleGenerator |
| 11 |
from dune.common.hashit import hashIt |
| 12 |
generator = SimpleGenerator("BCRSMatrix","Dune::Python") |
| 13 |
generatorvec = SimpleGenerator("BlockVector","Dune::Python") |
| 14 |
generatormatrixindexset = SimpleGenerator("MatrixIndexSet","Dune::Python") |
| 15 |
|
| 16 |
def load(includes ,typeName ,constructors=None, methods=None): |
| 17 |
includes = includes + ["dune/python/istl/bcrsmatrix.hh"] |
| 18 |
typeHash = "istlbcrsmatrix_" + hashIt(typeName) |
| 19 |
return generator.load(includes ,typeName ,typeHash ,constructors ,methods) |
| 20 |
|
| 21 |
def loadvec(includes ,typeName ,constructors=None, methods=None): |
| 22 |
includes = includes + ["dune/python/common/fvector.hh", |
| 23 |
"dune/python/istl/bvector.hh"] |
| 24 |
typeHash = "istlbvector_" + hashIt(typeName) |
| 25 |
return generatorvec.load(includes ,typeName ,typeHash ,constructors ,methods) |
| 26 |
|
| 27 |
def loadmatrixindexset(includes ,typeName ,constructors=None, methods=None): |
| 28 |
includes = includes + ["dune/python/istl/matrixindexset.hh"] |
| 29 |
typeHash = "matrixindexset_" + hashIt(typeName) |
| 30 |
return generatormatrixindexset.load(includes ,typeName ,typeHash ,constructors ,methods) |
| 31 |
|
| 32 |
def MatrixIndexSet(rows, cols): |
| 33 |
includes = ["dune/istl/matrixindexset.hh"] |
| 34 |
typeName = "Dune::MatrixIndexSet" |
| 35 |
return loadmatrixindexset(includes, typeName).MatrixIndexSet(rows,cols) |
| 36 |
|
| 37 |
def BCRSMatrix(blockType): |
| 38 |
includes = ["dune/istl/bcrsmatrix.hh"] |
| 39 |
try: |
| 40 |
typeName = "Dune::BCRSMatrix<" + blockType.cppTypeName + " >" |
| 41 |
return load(includes,typeName).BCRSMatrix |
| 42 |
except AttributeError: |
| 43 |
#check whether blocksize is 1,1 |
| 44 |
if blockType[0] == blockType[1] == 1: |
| 45 |
return BCRSMatrix11 |
| 46 |
|
| 47 |
typeName = "Dune::BCRSMatrix< Dune::FieldMatrix< double,"\ |
| 48 |
+ str(blockType[0]) + "," + str(blockType[1]) \ |
| 49 |
+ " > >" |
| 50 |
return load(includes, typeName).BCRSMatrix |
| 51 |
|
| 52 |
def bcrsMatrix(size=0, *args, **kwargs): |
| 53 |
blockType = kwargs.get("blockType",[1,1]) |
| 54 |
if size != 0 : |
| 55 |
return BCRSMatrix(blockType)(size,*args) |
| 56 |
return BCRSMatrix(blockType)() |
| 57 |
|
| 58 |
def BlockVector(typeOrBlockSize,size=None): |
| 59 |
""" |
| 60 |
Create a BlockVector object with specified type or block size for a FieldVector. |
| 61 |
|
| 62 |
Args: |
| 63 |
typeOrBlockSize (int or str or object): The type of the block vector or the block size. |
| 64 |
- If int, specifies the block size directly. |
| 65 |
- If typeOrBlockSize is not an int, it should have a 'cppTypeName' attribute specifying the type and 'cppIncludes' attribute specifying additional include files. |
| 66 |
size (int, optional): The size of the vector. Default is None. |
| 67 |
|
| 68 |
Returns: |
| 69 |
BlockVector: The created BlockVector object. |
| 70 |
""" |
| 71 |
typeName=[] |
| 72 |
includes=["dune/python/istl/bvector.hh"] |
| 73 |
if hasattr (typeOrBlockSize, "cppTypeName") and hasattr (typeOrBlockSize, "cppIncludes"): |
| 74 |
|
| 75 |
typeName = f"Dune::BlockVector< {typeOrBlockSize.cppTypeName} >" |
| 76 |
|
| 77 |
includes += typeOrBlockSize.cppIncludes |
| 78 |
if size is None: |
| 79 |
return loadvec(includes,typeName).BlockVector |
| 80 |
bvec = loadvec(includes,typeName).BlockVector(size) |
| 81 |
for i in range(len(bvec)): |
| 82 |
bvec[i]=typeOrBlockSize |
| 83 |
return bvec |
| 84 |
else: |
| 85 |
if typeOrBlockSize == 1: |
| 86 |
if size is None: |
| 87 |
return BlockVector1 |
| 88 |
else: |
| 89 |
return BlockVector1(size) |
| 90 |
dune.common.FieldVector(typeOrBlockSize) # make sure that the FieldVector is registered |
| 91 |
typeName = "Dune::BlockVector<Dune::FieldVector< double, " + str(typeOrBlockSize) + " > >" |
| 92 |
if size is None: |
| 93 |
return loadvec(includes, typeName).BlockVector |
| 94 |
else: |
| 95 |
return loadvec(includes, typeName).BlockVector(size) |
| 96 |
|
| 97 |
def blockVector(size, blockSize=1): |
| 98 |
""" |
| 99 |
Creates a Dune BlockVector object of size `size` with FieldVector<double,`blockSize`> as blocks. |
| 100 |
|
| 101 |
Args: |
| 102 |
size (int): The size of the vector. |
| 103 |
blockSize (int, optional): The block size. Default is 1. |
| 104 |
|
| 105 |
Returns: |
| 106 |
BlockVector: The created BlockVector object. |
| 107 |
""" |
| 108 |
return BlockVector(blockSize,size) |