Initialize a finite element Function. Args: V: The function space that the Function is defined on. x: Function degree-of-freedom vector. Typically required only when reading a saved Function from file. name: Function name. dtyp
(
self,
V: FunctionSpace,
x: la.Vector[Scalar] | None = None,
name: str | None = None,
dtype: npt.DTypeLike | None = None,
)
| 351 | _x: la.Vector[Scalar] |
| 352 | |
| 353 | def __init__( |
| 354 | self, |
| 355 | V: FunctionSpace, |
| 356 | x: la.Vector[Scalar] | None = None, |
| 357 | name: str | None = None, |
| 358 | dtype: npt.DTypeLike | None = None, |
| 359 | ): |
| 360 | """Initialize a finite element Function. |
| 361 | |
| 362 | Args: |
| 363 | V: The function space that the Function is defined on. |
| 364 | x: Function degree-of-freedom vector. Typically required |
| 365 | only when reading a saved Function from file. |
| 366 | name: Function name. |
| 367 | dtype: Scalar type. Is not set, the DOLFINx default scalar |
| 368 | type is used. |
| 369 | """ |
| 370 | if x is not None: |
| 371 | if dtype is None: |
| 372 | dtype = x.array.dtype |
| 373 | else: |
| 374 | assert x.array.dtype == dtype, "Incompatible Vector and dtype." |
| 375 | else: |
| 376 | if dtype is None: |
| 377 | dtype = default_scalar_type |
| 378 | |
| 379 | assert np.issubdtype(V.element.dtype, np.dtype(dtype).type(0).real.dtype), ( |
| 380 | "Incompatible FunctionSpace dtype and requested dtype." |
| 381 | ) |
| 382 | |
| 383 | # Create cpp Function |
| 384 | def functiontype(dtype): |
| 385 | if np.issubdtype(dtype, np.float32): |
| 386 | return _cpp.fem.Function_float32 |
| 387 | elif np.issubdtype(dtype, np.float64): |
| 388 | return _cpp.fem.Function_float64 |
| 389 | elif np.issubdtype(dtype, np.complex64): |
| 390 | return _cpp.fem.Function_complex64 |
| 391 | elif np.issubdtype(dtype, np.complex128): |
| 392 | return _cpp.fem.Function_complex128 |
| 393 | else: |
| 394 | raise NotImplementedError(f"Type {dtype} not supported.") |
| 395 | |
| 396 | if x is not None: |
| 397 | self._cpp_object = functiontype(dtype)(V._cpp_object, x._cpp_object) # type: ignore |
| 398 | else: |
| 399 | self._cpp_object = functiontype(dtype)(V._cpp_object) # type: ignore |
| 400 | |
| 401 | # Initialize the ufl.FunctionSpace |
| 402 | super().__init__(V.ufl_function_space()) |
| 403 | |
| 404 | # Set name |
| 405 | if name is None: |
| 406 | self.name = "f" |
| 407 | else: |
| 408 | self.name = name |
| 409 | |
| 410 | # Store DOLFINx FunctionSpace object |
nothing calls this directly
no test coverage detected