Utility functions

CALFEM for Python contains several utility functions to make it easier to print out arrays and text in different ways and control verbosity of logging and error messages.

To use these functions in Python the utils module needs to be imported, which can be done with the following statement:

import calfem.utils as cfu

disp_h[1-3]

Purpose:

Display text heading in either markdown or HTML, depending if running in a console or in a Jupyter environment (HTML).

Syntax:
cfu.disp_h1(msg)
cfu.disp_h2(msg)
cfu.disp_h3(msg)
Description:

disp_h1 displays msg as a heading in markdown if running in a console or as a HTML heading if running in a Jupyter environment. The following code

cfu.disp_h1("Element 1 stiffness matrix:")

displays the following output in a console

# Element 1 stiffness matrix:

and

<h1>Element 1 stiffness matrix:</h1>

When running in a Jupyter environment, but will be rendered as a section header.

disp_array

Purpose:

Display an array as a table.

Syntax:
cfu.disp_array(arr, headers=[], fmt=".4e", tablefmt="psql", showindex=False)
Description:

disp_h1 displays arr as a table with optional headers defined as a list in the headers argument. tablefmt is a string determining what kind of table format to use. Example of formats can be:

  • “plain” - No extra formatting except blanks space.

  • “psql” - Default format. Text based table with lines for colums and headers.

  • “grid” - Text based table with a stronger line after the headers.

  • “html” - Displays the text as a HTML table. This is the default if running in a Jupyter environment.

fmt Controls the format of each array cell. showindex is a flag to display indices as the first column.

cfu.disp_array(edof, fmt=".0f", headers=["Element", "DOF1", "DOF2", "DOF3", "DOF4"])

displays the following output in a console

+-----------+--------+--------+--------+
|   Element |   DOF1 |   DOF2 |   DOF3 |
|-----------+--------+--------+--------|
|         1 |      2 |      3 |      4 |
|         3 |      4 |      5 |      6 |
+-----------+--------+--------+--------+

In a Jupyter environment it will render an HTML table.