Physics Problem

class flatiron_tk.physics.physics_problem.PhysicsProblem(mesh, tag='u', q_degree=4, **kwargs)[source]

Bases: ABC

Abstract base class for defining physics problems using the finite element method. This class provides a framework for setting up and solving physics problems on a given mesh. It includes methods for defining the finite element, function space, weak form, boundary conditions, and writing results to output files. Inherited classes must implement the set_weak_form method to define the specific weak form of the problem.

Parameters:
  • mesh (flatiron_tk mesh object) – The mesh object representing the computational domain.

  • tag (str, optional) – Tag for the solution function (default is ‘u’).

  • q_degree (int, optional) – Quadrature degree for the function space (default is 4).

  • **kwargs – Additional keyword arguments.

dirichlet_bcs

List to store Dirichlet boundary conditions.

Type:

list

mesh

The mesh object representing the computational domain.

Type:

flatiron_tk mesh object

number_of_steps_written

Counter for the number of steps written.

Type:

int

tag

Tag for the solution function.

Type:

str

q_degree

Quadrature degree for the function space.

Type:

int

dx

Volume differential (domain measure).

Type:

ufl.Measure

ds

Exterior surface differential.

Type:

ufl.Measure

dS

Interior surface differential.

Type:

ufl.Measure

external_function_dict

Dictionary to hold constant physical properties as external functions.

Type:

dict

add_to_weak_form(form, domain=None)[source]

Add a term to the weak form of the physics problem.

Parameters:
  • form (ufl.Form) – The term to add to the weak form.

  • domain (ufl.Measure, optional) – The domain to which the term applies (default is None, meaning the user has specified the domain in the form.

build_function_space()[source]

Build the function space, test function, trial function, and solution function for the physics problem.

external_function(function_name)[source]

Get an external function by name. :param function_name: The name of the external function to get. :type function_name: str

Returns:

The external function associated with the given name.

Return type:

dolfinx.fem.Constant or ufl expression or dolfinx function/constant

flux(h)[source]

Define the flux term for Neumann boundary conditions. :param h: The flux value. Can be a number, an iterable of numbers, a ufl expression, or a dolfinx function/constant. :type h: number or iterable or ufl expression or dolfinx function/constant

get_function_space(physics_tag=None)[source]

Get the function space for the physics problem. :param physics_tag: The tag of the physics problem (default is None). :type physics_tag: str, optional

Returns:

The function space of the physics problem.

Return type:

dolfinx.fem.FunctionSpace

get_residual()[source]

Get the residual of the weak form.

Returns:

  • ufl.Form – The residual of the weak form.

  • In this base class, this method is not implemented and should be overridden in derived classes.

get_solution_function(physics_tag=None)[source]

Get the solution function for the physics problem. :param physics_tag: The tag of the physics problem (default is None). :type physics_tag: str, optional

Returns:

The solution function of the physics problem.

Return type:

dolfinx.fem.Function

get_test_function(physics_tag=None)[source]

Get the test function for the physics problem. :param physics_tag: The tag of the physics problem (default is None). :type physics_tag: str, optional

Returns:

The test function of the physics problem.

Return type:

ufl.TestFunction

get_trial_function(physics_tag=None)[source]

Get the trial function for the physics problem. :param physics_tag: The tag of the physics problem (default is None). :type physics_tag: str, optional

Returns:

The trial function of the physics problem.

Return type:

ufl.TrialFunction

get_weak_form()[source]

Get the weak form of the physics problem. :returns: The weak form of the physics problem. :rtype: ufl.Form

jacobian()[source]

Get the Jacobian of the weak form. :returns: The Jacobian of the weak form. :rtype: ufl.Form

set_bcs(bcs_dict)[source]

Set the boundary conditions for the physics problem.

Parameters:

bcs_dict (dict) – A dictionary containing the boundary conditions. The keys are the boundary IDs, and the values are dictionaries with ‘type’ and ‘value’ keys.

Raises:

ValueError – If an unsupported boundary condition type is provided.

set_element(element_family, element_degree, element_shape=None)[source]

Set the finite element for the physics problem.

Parameters:
  • element_family (str) – The family of the finite element (e.g., “CG”, “DG”).

  • element_degree (int) – The degree of the finite element.

  • element_shape (str, optional) – The shape of the finite element (e.g., “scalar”, “vector”). Default is None.

set_external_function(function_name, function)[source]

Set an external function for the physics problem.

Parameters:
  • function_name (str) – The name of the external function.

  • function (number or iterable or ufl expression or dolfinx function/constant) – The external function to set. Can be a number, an iterable of numbers, a ufl expression, or a dolfinx function/constant.

set_function_space(V)[source]

Set the function space for the physics problem. :param V: The function space to set. :type V: dolfinx.fem.FunctionSpace

set_quadrature_degree(q_degree)[source]

Set the quadrature degree for the physics problem.

Parameters:

q_degree (int) – The quadrature degree to set.

set_solution_function(solution_function)[source]

Set the solution function for the physics problem. :param solution_function: The solution function to set. :type solution_function: dolfinx.fem.Function

set_tag(tag)[source]
Parameters:

tag (The tag to assign to the solution function.)

set_test_function(test_function)[source]

Set the test function for the physics problem. :param test_function: The test function to set. :type test_function: ufl.TestFunction

set_trial_function(trial_function)[source]

Set the trial function for the physics problem. :param trial_function: The trial function to set. :type trial_function: ufl.TrialFunction

abstractmethod set_weak_form()[source]

Set the weak form of the physics problem.

This is done in the inherited class.

set_writer(output_dir, file_format)[source]

Set up the file writer for the physics problem.

Parameters:
  • output_dir (str) – The directory where the output files will be saved.

  • file_format (str) – The file format for the output files. Supported formats are ‘xdmf’, ‘pvd’, and ‘bp’.

Raises:

ValueError – If an unsupported file format is provided.

to_dolfinx(a)[source]

Convert a to a dolfinx constant if a is a number or an iterable, otherwise return a.

Parameters:

a (number or iterable or ufl expression or dolfinx function/constant) – The input to convert.

Returns:

The converted dolfinx constant or the original input if it was already a ufl expression or dolfinx function/constant.

Return type:

dolfinx.fem.Constant or ufl expression or dolfinx function/constant

Raises:

ValueError – If the input is not a number, iterable, ufl expression, or dolfinx function/constant.

write(function_to_write=None, **kwargs)[source]

Write the solution function to the output file.

Parameters:
  • function_to_write (dolfinx.fem.Function, optional) – The function to write to the output file. If None, the solution function is written (default is None).

  • **kwargs (dict) –

    Additional keyword arguments. Supported keys:

    time_stampfloat, optional

    The time stamp to associate with the output (default is the number of steps written)

PhysicsProblem is a base class for other physics to inherit and define their own physics. This class provides routine functions that are needed by every physics such as function space definition, solution io, etc. This class contains as members, the flatiron_tk.Mesh object the physics is defined on, a tag which is a name of the physics

Defining custom physics

Any physics problem will inherit this class. The developer will need to overload the set_weak_form, flux, and get_residue methods and properly define the physics of the particular problem. For example, see the SteadyScalarTransport physics.

Defining member functions

To streamline the physics parameters, we provide a dictionary mapping a string to a function called external_function_dict as a member function. This dictionary will contain any external functions and variables that will be used to define the physics. For example, if we have a diffusion problem, then the diffusivity of the problem will be encoded in external_function_dict. By doing it this way, we have a centralized location that stores all of the physics definition. Any physics inheriting this class will use the set_external_function method to assign a function value into this dictionary.