139b2de37Sjeremylt# Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at 239b2de37Sjeremylt# the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights 339b2de37Sjeremylt# reserved. See files LICENSE and NOTICE for details. 439b2de37Sjeremylt# 539b2de37Sjeremylt# This file is part of CEED, a collection of benchmarks, miniapps, software 639b2de37Sjeremylt# libraries and APIs for efficient high-order finite element and spectral 739b2de37Sjeremylt# element discretizations for exascale applications. For more information and 839b2de37Sjeremylt# source code availability see http://github.com/ceed. 939b2de37Sjeremylt# 1039b2de37Sjeremylt# The CEED research is supported by the Exascale Computing Project 17-SC-20-SC, 1139b2de37Sjeremylt# a collaborative effort of two U.S. Department of Energy organizations (Office 1239b2de37Sjeremylt# of Science and the National Nuclear Security Administration) responsible for 1339b2de37Sjeremylt# the planning and preparation of a capable exascale ecosystem, including 1439b2de37Sjeremylt# software, applications, hardware, advanced system engineering and early 1539b2de37Sjeremylt# testbed platforms, in support of the nation's exascale computing imperative. 1639b2de37Sjeremylt 1739b2de37Sjeremyltfrom _ceed_cffi import ffi, lib 1839b2de37Sjeremyltimport sys 1939b2de37Sjeremyltimport io 20*0a0da059Sjeremyltimport tempfile 2139b2de37Sjeremyltfrom abc import ABC 2239b2de37Sjeremyltfrom .ceed_vector import Vector 2339b2de37Sjeremyltfrom .ceed_basis import BasisTensorH1, BasisTensorH1Lagrange, BasisH1 2469a53589Sjeremyltfrom .ceed_elemrestriction import ElemRestriction, StridedElemRestriction, BlockedElemRestriction, BlockedStridedElemRestriction 2539b2de37Sjeremyltfrom .ceed_qfunction import QFunction, QFunctionByName, IdentityQFunction 2639b2de37Sjeremyltfrom .ceed_operator import Operator, CompositeOperator 2739b2de37Sjeremyltfrom .ceed_constants import * 2839b2de37Sjeremylt 2939b2de37Sjeremylt# ------------------------------------------------------------------------------ 3039b2de37Sjeremyltclass Ceed(): 3139b2de37Sjeremylt """Ceed: core components.""" 3239b2de37Sjeremylt # Attributes 3339b2de37Sjeremylt _pointer = ffi.NULL 3439b2de37Sjeremylt 3539b2de37Sjeremylt # Constructor 3639b2de37Sjeremylt def __init__(self, resource = "/cpu/self"): 3739b2de37Sjeremylt # libCEED object 3839b2de37Sjeremylt self._pointer = ffi.new("Ceed *") 3939b2de37Sjeremylt 4039b2de37Sjeremylt # libCEED call 4139b2de37Sjeremylt resourceAscii = ffi.new("char[]", resource.encode("ascii")) 4239b2de37Sjeremylt lib.CeedInit(resourceAscii, self._pointer) 4339b2de37Sjeremylt 4439b2de37Sjeremylt # Representation 4539b2de37Sjeremylt def __repr__(self): 4639b2de37Sjeremylt return "<Ceed instance at " + hex(id(self)) + ">" 4739b2de37Sjeremylt 48*0a0da059Sjeremylt # String conversion for print() to stdout 49*0a0da059Sjeremylt def __str__(self): 50*0a0da059Sjeremylt """View a Ceed via print().""" 51*0a0da059Sjeremylt 52*0a0da059Sjeremylt # libCEED call 53*0a0da059Sjeremylt with tempfile.NamedTemporaryFile() as key_file: 54*0a0da059Sjeremylt with open(key_file.name, 'r+') as stream_file: 55*0a0da059Sjeremylt stream = ffi.cast("FILE *", stream_file) 56*0a0da059Sjeremylt 57*0a0da059Sjeremylt lib.CeedView(self._pointer[0], stream) 58*0a0da059Sjeremylt 59*0a0da059Sjeremylt stream_file.seek(0) 60*0a0da059Sjeremylt out_string = stream_file.read() 61*0a0da059Sjeremylt 62*0a0da059Sjeremylt return out_string 63*0a0da059Sjeremylt 6439b2de37Sjeremylt # Get Resource 6539b2de37Sjeremylt def get_resource(self): 6639b2de37Sjeremylt """Get the full resource name for a Ceed context. 6739b2de37Sjeremylt 6839b2de37Sjeremylt Returns: 6939b2de37Sjeremylt resource: resource name""" 7039b2de37Sjeremylt 7139b2de37Sjeremylt # libCEED call 7239b2de37Sjeremylt resource = ffi.new("char **") 7339b2de37Sjeremylt lib.CeedGetResource(self._pointer[0], resource) 7439b2de37Sjeremylt 7539b2de37Sjeremylt return ffi.string(resource[0]).decode("UTF-8") 7639b2de37Sjeremylt 7739b2de37Sjeremylt # Preferred MemType 7839b2de37Sjeremylt def get_preferred_memtype(self): 7939b2de37Sjeremylt """Return Ceed preferred memory type. 8039b2de37Sjeremylt 8139b2de37Sjeremylt Returns: 8239b2de37Sjeremylt memtype: Ceed preferred memory type""" 8339b2de37Sjeremylt 8439b2de37Sjeremylt # libCEED call 8539b2de37Sjeremylt memtype = ffi.new("CeedMemType *", MEM_HOST) 8639b2de37Sjeremylt lib.CeedGetPreferredMemType(self._pointer[0], memtype) 8739b2de37Sjeremylt 8839b2de37Sjeremylt return memtype[0] 8939b2de37Sjeremylt 9039b2de37Sjeremylt # CeedVector 9139b2de37Sjeremylt def Vector(self, size): 9239b2de37Sjeremylt """Ceed Vector: storing and manipulating vectors. 9339b2de37Sjeremylt 9439b2de37Sjeremylt Args: 9539b2de37Sjeremylt size: length of vector 9639b2de37Sjeremylt 9739b2de37Sjeremylt Returns: 9839b2de37Sjeremylt vector: Ceed Vector""" 9939b2de37Sjeremylt 10039b2de37Sjeremylt return Vector(self, size) 10139b2de37Sjeremylt 10239b2de37Sjeremylt # CeedElemRestriction 10339b2de37Sjeremylt def ElemRestriction(self, nelem, elemsize, nnodes, ncomp, indices, 104a8d32208Sjeremylt memtype=lib.CEED_MEM_HOST, cmode=lib.CEED_COPY_VALUES, 10561dbc9d2Sjeremylt imode=lib.CEED_NONINTERLACED): 10639b2de37Sjeremylt """Ceed ElemRestriction: restriction from local vectors to elements. 10739b2de37Sjeremylt 10839b2de37Sjeremylt Args: 10939b2de37Sjeremylt nelem: number of elements described in the indices array 11039b2de37Sjeremylt elemsize: size (number of nodes) per element 11139b2de37Sjeremylt nnodes: the number of nodes in the local vector. The input Ceed Vector 11239b2de37Sjeremylt to which the restriction will be applied is of size 11339b2de37Sjeremylt (nnodes * ncomp). This size may include data 11439b2de37Sjeremylt used by other Ceed ElemRestriction objects describing 11539b2de37Sjeremylt different types of elements. 11639b2de37Sjeremylt ncomp: number of field components per interpolation node (1 for scalar fields) 11739b2de37Sjeremylt *indices: Numpy array of shape [nelem, elemsize]. Row i holds the 11839b2de37Sjeremylt ordered list of the indices (into the input Ceed Vector) 11939b2de37Sjeremylt for the unknowns corresponding to element i, where 12039b2de37Sjeremylt 0 <= i < nelem. All indices must be in the range 12139b2de37Sjeremylt [0, nnodes - 1]. 12239b2de37Sjeremylt **memtype: memory type of the indices array, default CEED_MEM_HOST 12339b2de37Sjeremylt **cmode: copy mode for the indices array, default CEED_COPY_VALUES 12461dbc9d2Sjeremylt **imode: ordering of the ncomp components, i.e. it specifies 125a8d32208Sjeremylt the ordering of the components of the local vector used 12661dbc9d2Sjeremylt by this CeedElemRestriction. CEED_NONINTERLACED indicates 12761dbc9d2Sjeremylt the component is the outermost index and CEED_INTERLACED 128a8d32208Sjeremylt indicates the component is the innermost index in 12961dbc9d2Sjeremylt ordering of the local vector, default CEED_NONINTERLACED 13039b2de37Sjeremylt 13139b2de37Sjeremylt Returns: 13239b2de37Sjeremylt elemrestriction: Ceed ElemRestiction""" 13339b2de37Sjeremylt 13439b2de37Sjeremylt return ElemRestriction(self, nelem, elemsize, nnodes, ncomp, indices, 13561dbc9d2Sjeremylt memtype=memtype, cmode=cmode, imode=imode) 13639b2de37Sjeremylt 13769a53589Sjeremylt def StridedElemRestriction(self, nelem, elemsize, nnodes, ncomp, strides): 13869a53589Sjeremylt """Ceed Identity ElemRestriction: strided restriction from local vectors to elements. 13939b2de37Sjeremylt 14039b2de37Sjeremylt Args: 14139b2de37Sjeremylt nelem: number of elements described in the indices array 14239b2de37Sjeremylt elemsize: size (number of nodes) per element 14339b2de37Sjeremylt nnodes: the number of nodes in the local vector. The input Ceed Vector 14439b2de37Sjeremylt to which the restriction will be applied is of size 14539b2de37Sjeremylt (nnodes * ncomp). This size may include data 14639b2de37Sjeremylt used by other Ceed ElemRestriction objects describing 14739b2de37Sjeremylt different types of elements. 148a8d32208Sjeremylt ncomp: number of field components per interpolation node 149a8d32208Sjeremylt (1 for scalar fields) 15069a53589Sjeremylt *strides: Array for strides between [nodes, components, elements]. 15169a53589Sjeremylt The data for node i, component j, element k in the 15269a53589Sjeremylt L-vector is given by 15369a53589Sjeremylt i*strides[0] + j*strides[1] + k*strides[2] 15439b2de37Sjeremylt 15539b2de37Sjeremylt Returns: 15669a53589Sjeremylt elemrestriction: Ceed Strided ElemRestiction""" 15739b2de37Sjeremylt 15869a53589Sjeremylt return StridedElemRestriction(self, nelem, elemsize, nnodes, ncomp, strides) 15939b2de37Sjeremylt 16039b2de37Sjeremylt def BlockedElemRestriction(self, nelem, elemsize, blksize, nnodes, ncomp, 16139b2de37Sjeremylt indices, memtype=lib.CEED_MEM_HOST, 162a8d32208Sjeremylt cmode=lib.CEED_COPY_VALUES, 16361dbc9d2Sjeremylt imode=lib.CEED_NONINTERLACED): 16439b2de37Sjeremylt """Ceed Blocked ElemRestriction: blocked restriction from local vectors to elements. 16539b2de37Sjeremylt 16639b2de37Sjeremylt Args: 16739b2de37Sjeremylt nelem: number of elements described in the indices array 16839b2de37Sjeremylt elemsize: size (number of nodes) per element 16939b2de37Sjeremylt blksize: number of elements in a block 17039b2de37Sjeremylt nnodes: the number of nodes in the local vector. The input Ceed Vector 17139b2de37Sjeremylt to which the restriction will be applied is of size 17239b2de37Sjeremylt (nnodes * ncomp). This size may include data 17339b2de37Sjeremylt used by other Ceed ElemRestriction objects describing 17439b2de37Sjeremylt different types of elements. 17539b2de37Sjeremylt ncomp: number of field components per interpolation node (1 for scalar fields) 17639b2de37Sjeremylt *indices: Numpy array of shape [nelem, elemsize]. Row i holds the 17739b2de37Sjeremylt ordered list of the indices (into the input Ceed Vector) 17839b2de37Sjeremylt for the unknowns corresponding to element i, where 17939b2de37Sjeremylt 0 <= i < nelem. All indices must be in the range 18039b2de37Sjeremylt [0, nnodes - 1]. The backend will permute and pad this 18139b2de37Sjeremylt array to the desired ordering for the blocksize, which is 18239b2de37Sjeremylt typically given by the backend. The default reordering is 18339b2de37Sjeremylt to interlace elements. 18439b2de37Sjeremylt **memtype: memory type of the indices array, default CEED_MEM_HOST 18539b2de37Sjeremylt **cmode: copy mode for the indices array, default CEED_COPY_VALUES 18661dbc9d2Sjeremylt **imode: ordering of the ncomp components, i.e. it specifies 187a8d32208Sjeremylt the ordering of the components of the local vector used 18861dbc9d2Sjeremylt by this CeedElemRestriction. CEED_NONINTERLACED indicates 18961dbc9d2Sjeremylt the component is the outermost index and CEED_INTERLACED 190a8d32208Sjeremylt indicates the component is the innermost index in 19161dbc9d2Sjeremylt ordering of the local vector, default CEED_NONINTERLACED 19239b2de37Sjeremylt 19339b2de37Sjeremylt Returns: 19439b2de37Sjeremylt elemrestriction: Ceed Blocked ElemRestiction""" 19539b2de37Sjeremylt 19639b2de37Sjeremylt return BlockedElemRestriction(self, nelem, elemsize, blksize, nnodes, 19739b2de37Sjeremylt ncomp, indices, memtype=memtype, 19861dbc9d2Sjeremylt cmode=cmode, imode=imode) 19939b2de37Sjeremylt 20069a53589Sjeremylt def BlockedStridedElemRestriction(self, nelem, elemsize, blksize, nnodes, 20169a53589Sjeremylt ncomp, strides): 20269a53589Sjeremylt """Ceed Blocked Strided ElemRestriction: blocked and strided restriction from local vectors to elements. 20369a53589Sjeremylt 20469a53589Sjeremylt Args: 20569a53589Sjeremylt nelem: number of elements described in the indices array 20669a53589Sjeremylt elemsize: size (number of nodes) per element 20769a53589Sjeremylt blksize: number of elements in a block 20869a53589Sjeremylt nnodes: the number of nodes in the local vector. The input Ceed Vector 20969a53589Sjeremylt to which the restriction will be applied is of size 21069a53589Sjeremylt (nnodes * ncomp). This size may include data 21169a53589Sjeremylt used by other Ceed ElemRestriction objects describing 21269a53589Sjeremylt different types of elements. 21369a53589Sjeremylt ncomp: number of field components per interpolation node 21469a53589Sjeremylt (1 for scalar fields) 21569a53589Sjeremylt *strides: Array for strides between [nodes, components, elements]. 21669a53589Sjeremylt The data for node i, component j, element k in the 21769a53589Sjeremylt L-vector is given by 21869a53589Sjeremylt i*strides[0] + j*strides[1] + k*strides[2] 21969a53589Sjeremylt 22069a53589Sjeremylt Returns: 22169a53589Sjeremylt elemrestriction: Ceed Strided ElemRestiction""" 22269a53589Sjeremylt 22369a53589Sjeremylt return BlockedStridedElemRestriction(self, nelem, elemsize, blksize, 22469a53589Sjeremylt nnodes, ncomp, strides) 22569a53589Sjeremylt 22639b2de37Sjeremylt # CeedBasis 22739b2de37Sjeremylt def BasisTensorH1(self, dim, ncomp, P1d, Q1d, interp1d, grad1d, 22839b2de37Sjeremylt qref1d, qweight1d): 22939b2de37Sjeremylt """Ceed Tensor H1 Basis: finite element tensor-product basis objects for 23039b2de37Sjeremylt H^1 discretizations. 23139b2de37Sjeremylt 23239b2de37Sjeremylt Args: 23339b2de37Sjeremylt dim: topological dimension 23439b2de37Sjeremylt ncomp: number of field components (1 for scalar fields) 23539b2de37Sjeremylt P1d: number of nodes in one dimension 23639b2de37Sjeremylt Q1d: number of quadrature points in one dimension 23739b2de37Sjeremylt *interp1d: Numpy array holding the row-major (Q1d * P1d) matrix expressing the 23839b2de37Sjeremylt values of nodal basis functions at quadrature points 23939b2de37Sjeremylt *grad1d: Numpy array holding the row-major (Q1d * P1d) matrix expressing the 24039b2de37Sjeremylt derivatives of nodal basis functions at quadrature points 24139b2de37Sjeremylt *qref1d: Numpy array of length Q1d holding the locations of quadrature points 24239b2de37Sjeremylt on the 1D reference element [-1, 1] 24339b2de37Sjeremylt *qweight1d: Numpy array of length Q1d holding the quadrature weights on the 24439b2de37Sjeremylt reference element 24539b2de37Sjeremylt 24639b2de37Sjeremylt Returns: 24739b2de37Sjeremylt basis: Ceed Basis""" 24839b2de37Sjeremylt 24939b2de37Sjeremylt return BasisTensorH1(self, dim, ncomp, P1d, Q1d, interp1d, grad1d, 25039b2de37Sjeremylt qref1d, qweight1d) 25139b2de37Sjeremylt 25239b2de37Sjeremylt def BasisTensorH1Lagrange(self, dim, ncomp, P, Q, qmode): 25339b2de37Sjeremylt """Ceed Tensor H1 Lagrange Basis: finite element tensor-product Lagrange basis 25439b2de37Sjeremylt objects for H^1 discretizations. 25539b2de37Sjeremylt 25639b2de37Sjeremylt Args: 25739b2de37Sjeremylt dim: topological dimension 25839b2de37Sjeremylt ncomp: number of field components (1 for scalar fields) 25939b2de37Sjeremylt P: number of Gauss-Lobatto nodes in one dimension. The 26039b2de37Sjeremylt polynomial degree of the resulting Q_k element is k=P-1. 26139b2de37Sjeremylt Q: number of quadrature points in one dimension 26239b2de37Sjeremylt qmode: distribution of the Q quadrature points (affects order of 26339b2de37Sjeremylt accuracy for the quadrature) 26439b2de37Sjeremylt 26539b2de37Sjeremylt Returns: 26639b2de37Sjeremylt basis: Ceed Basis""" 26739b2de37Sjeremylt 26839b2de37Sjeremylt return BasisTensorH1Lagrange(self, dim, ncomp, P, Q, qmode) 26939b2de37Sjeremylt 27039b2de37Sjeremylt def BasisH1(self, topo, ncomp, nnodes, nqpts, interp, grad, qref, qweight): 27139b2de37Sjeremylt """Ceed H1 Basis: finite element non tensor-product basis for H^1 discretizations. 27239b2de37Sjeremylt 27339b2de37Sjeremylt Args: 27439b2de37Sjeremylt topo: topology of the element, e.g. hypercube, simplex, etc 27539b2de37Sjeremylt ncomp: number of field components (1 for scalar fields) 27639b2de37Sjeremylt nnodes: total number of nodes 27739b2de37Sjeremylt nqpts: total number of quadrature points 27839b2de37Sjeremylt *interp: Numpy array holding the row-major (nqpts * nnodes) matrix expressing 27939b2de37Sjeremylt the values of nodal basis functions at quadrature points 28039b2de37Sjeremylt *grad: Numpy array holding the row-major (nqpts * dim * nnodes) matrix 28139b2de37Sjeremylt expressing the derivatives of nodal basis functions at 28239b2de37Sjeremylt quadrature points 28339b2de37Sjeremylt *qref: Numpy array of length (nqpts * dim) holding the locations of quadrature 28439b2de37Sjeremylt points on the reference element [-1, 1] 28539b2de37Sjeremylt *qweight: Numpy array of length nnodes holding the quadrature weights on the 28639b2de37Sjeremylt reference element 28739b2de37Sjeremylt 28839b2de37Sjeremylt Returns: 28939b2de37Sjeremylt basis: Ceed Basis""" 29039b2de37Sjeremylt 29139b2de37Sjeremylt return BasisH1(self, topo, ncomp, nnodes, nqpts, interp, grad, qref, qweight) 29239b2de37Sjeremylt 29339b2de37Sjeremylt # CeedQFunction 29439b2de37Sjeremylt def QFunction(self, vlength, f, source): 29539b2de37Sjeremylt """Ceed QFunction: point-wise operation at quadrature points for evaluating 29639b2de37Sjeremylt volumetric terms. 29739b2de37Sjeremylt 29839b2de37Sjeremylt Args: 29939b2de37Sjeremylt vlength: vector length. Caller must ensure that number of quadrature 30039b2de37Sjeremylt points is a multiple of vlength 30139b2de37Sjeremylt f: ctypes function pointer to evaluate action at quadrature points 30239b2de37Sjeremylt source: absolute path to source of QFunction, 30339b2de37Sjeremylt "\\abs_path\\file.h:function_name 30439b2de37Sjeremylt 30539b2de37Sjeremylt Returns: 30639b2de37Sjeremylt qfunction: Ceed QFunction""" 30739b2de37Sjeremylt 30839b2de37Sjeremylt return QFunction(self, vlength, f, source) 30939b2de37Sjeremylt 31039b2de37Sjeremylt def QFunctionByName(self, name): 31139b2de37Sjeremylt """Ceed QFunction By Name: point-wise operation at quadrature points 31239b2de37Sjeremylt from a given gallery, for evaluating volumetric terms. 31339b2de37Sjeremylt 31439b2de37Sjeremylt Args: 31539b2de37Sjeremylt name: name of QFunction to use from gallery 31639b2de37Sjeremylt 31739b2de37Sjeremylt Returns: 31839b2de37Sjeremylt qfunction: Ceed QFunction By Name""" 31939b2de37Sjeremylt 32039b2de37Sjeremylt return QFunctionByName(self, name) 32139b2de37Sjeremylt 32239b2de37Sjeremylt def IdentityQFunction(self, size, inmode, outmode): 32339b2de37Sjeremylt """Ceed Idenity QFunction: identity qfunction operation. 32439b2de37Sjeremylt 32539b2de37Sjeremylt Args: 32639b2de37Sjeremylt size: size of the qfunction fields 32739b2de37Sjeremylt **inmode: CeedEvalMode for input to Ceed QFunction 32839b2de37Sjeremylt **outmode: CeedEvalMode for output to Ceed QFunction 32939b2de37Sjeremylt 33039b2de37Sjeremylt Returns: 33139b2de37Sjeremylt qfunction: Ceed Identity QFunction""" 33239b2de37Sjeremylt 33339b2de37Sjeremylt return IdentityQFunction(self, size, inmode, outmode) 33439b2de37Sjeremylt 33539b2de37Sjeremylt # CeedOperator 33639b2de37Sjeremylt def Operator(self, qf, dqf=None, qdfT=None): 33739b2de37Sjeremylt """Ceed Operator: composed FE-type operations on vectors. 33839b2de37Sjeremylt 33939b2de37Sjeremylt Args: 34039b2de37Sjeremylt qf: QFunction defining the action of the operator at quadrature points 34139b2de37Sjeremylt **dqf: QFunction defining the action of the Jacobian, default None 34239b2de37Sjeremylt **dqfT: QFunction defining the action of the transpose of the Jacobian, 34339b2de37Sjeremylt default None 34439b2de37Sjeremylt 34539b2de37Sjeremylt Returns: 34639b2de37Sjeremylt operator: Ceed Operator""" 34739b2de37Sjeremylt 34839b2de37Sjeremylt return Operator(self, qf, dqf, qdfT) 34939b2de37Sjeremylt 35039b2de37Sjeremylt def CompositeOperator(self): 35139b2de37Sjeremylt """Composite Ceed Operator: composition of multiple CeedOperators. 35239b2de37Sjeremylt 35339b2de37Sjeremylt Returns: 35439b2de37Sjeremylt operator: Ceed Composite Operator""" 35539b2de37Sjeremylt 35639b2de37Sjeremylt return CompositeOperator(self) 35739b2de37Sjeremylt 35839b2de37Sjeremylt # Destructor 35939b2de37Sjeremylt def __del__(self): 36039b2de37Sjeremylt # libCEED call 36139b2de37Sjeremylt lib.CeedDestroy(self._pointer) 36239b2de37Sjeremylt 36339b2de37Sjeremylt# ------------------------------------------------------------------------------ 364