4.1. API reference of viennagrid

4.1.1. viennagrid — Fundamental classes for mesh representation

class viennagrid.Point(*args, **kwargs)[source]

Wrapper class that represents a point of any supported coordinate system and dimension.

__init__(*args, **kwargs)[source]

Define a new point specifying its coordinate system, dimension and, optionally, coordinates.

Args:
coordinates of the point (optional)
Kwargs:
  • coord_system: coordinate system tag (if omitted, cartesian is assumed)
  • dim: dimension of the space (optional if coordinates are specified)
coords[source]

Return a list containing the coordinates of the point.

coord_system[source]

Return the coordinate system tag of the point as seen in viennagrid.config.

dim[source]

Return the integer dimension of the space where the point is defined.

__add__(other)[source]

Add two points.

__sub__(other)[source]

Subtract two points.

__mul__(scalar)[source]

Multiply a point by a scalar number (coordinate-wise).

__div__(scalar)[source]

Divide a point by a scalar number (coordinate-wise).

__neg__()[source]

Negate a point (i.e. negate each coordinate of the point).

__getattr__(attr)[source]

If the requested attribute is not present in this class, try to get it from the low-level point class. This serves the purpose of calling the methods for coordinate system conversion and norm computation:

  • to_cartesian()
  • to_cylindrical()
  • to_polar()
  • to_spherical()
  • norm_1()
  • norm_2()
  • norm_inf()

Please, notice that not of all these methods are present in all point classes, but only the applicable ones. Refer to the API reference of viennagrid.wrapper for more information on what methods each low-level class provides.

class viennagrid.Domain(config)[source]

Wrapper class that represents a domain of any supported domain configuration.

__init__(config)[source]

Create a new domain with the given configuration.

Parameters:config (viennagrid.config.Configuration) – Configuration on which the new domain should be based.
config[source]

Return the configuration object of this domain (instance of viennagrid.config.Configuration).

vertices[source]

Return an object that allows you to access the list of all vertices contained in the domain.

This object provides the following methods:

__call__()

This returns a Python list containing all the vertices of the domain, in ascending order of indices:

vertex_list = domain.vertices()
__len__()

This allows you to get the number of vertices in the domain as though it were a Python list:

num_vertices = len(domain.vertices)
__iter__()[source]

This allows you to get an iterator over the vertices of the domain like this:

iterator = iter(domain.vertices)
__getitem__(index)

This allows you to access each vertex by its index using bracket notation:

v0 = domain.vertices[0]
cells[source]

Return an object that allows you to access the list of all cells contained in the domain.

This object provides the following methods:

__call__()

This returns a Python list containing all the cells of the domain, in ascending order of indices:

cell_list = domain.cells()
__len__()

This allows you to get the number of cells in the domain as though it were a Python list:

num_cells = len(domain.cells)
__iter__()[source]

This allows you to get an iterator over the cells of the domain like this:

iterator = iter(domain.cells)
__getitem__(index)

This allows you to access each cell by its index using bracket notation:

s0 = domain.cells[0]
make_vertex(point)[source]

Create a new vertex in the domain with the coordinates of the given point. This method doesn’t return the newly created vertex.

The point and its coordinates will be copied, the point will not be referenced itself.

Parameters:point (viennagrid.Point) – Point on which to base the vertex.
make_cell(*args, **kwargs)[source]

Create a new cell in the domain and return it.

As positional parameters you must pass as many vertices (Vertex objects) as needed to define a cell of the type and dimension of the domain.

Returns:A Cell object — the newly created cell
__iter__()[source]

Return a generator object to iterate over all the vertices contained in the domain.

Returns:A generator over all the vertices of the domain
class viennagrid.Segmentation(domain)[source]

Wrapper class that represents a segmentation of a wrapped domain.

__init__(domain)[source]

Create a new segmentation of the given domain.

Parameters:domain (viennagrid.Domain) – Domain on which to base the segmentation.
domain[source]

Return the domain to which this segmentation corresponds.

segments[source]

Return an object that allows you to access the list of all segments contained in the segmentation.

This object provides the following methods:

__call__()

This returns a Python list containing all the segments of the segmentation, in ascending order of indices:

segment_list = segmentation.segments()
__len__()

This allows you to get the number of segments in the segmentation as though it were a Python list:

num_segments = len(segmentation.segments)
__iter__()[source]

This allows you to get an iterator over the segments of the segmentation like this:

iterator = iter(segmentation.segments)
__getitem__(index)

This allows you to access each segment by its index using bracket notation:

s0 = segmentation.segments[0]
make_segment()[source]

Create a new segment in the segmentation and return it.

Returns:A Segment object — the newly created segment
__iter__()[source]

Return a generator object to iterate over all the segments contained in the segmentation.

Returns:A generator over all the segments of the segmentation
class viennagrid.Segment(segment)[source]

Wrapper class that represents a segment contained in a segmentation.

__init__(segment)[source]

Create a new segment based on the given low-level ViennaGrid segment.

Parameters:segment (Low-level segment type from viennagrid.wrapper) – Low-level ViennaGrid segment to be wrapped in this high-level object.
cells[source]

Return an object that allows you to access the list of all cells contained in the segment.

This object provides the following methods:

__call__()

This returns a Python list containing all the cells of the segment, in ascending order of indices:

cell_list = segment.cells()
__len__()

This allows you to get the number of cells in the segment as though it were a Python list:

num_cells = len(segment.cells)
__iter__()[source]

This allows you to get an iterator over the cells of the segment like this:

iterator = iter(segment.cells)
__getitem__(index)

This allows you to access each cell by its index using bracket notation:

s0 = segment.cells[0]
make_cell(*args, **kwargs)[source]

Create a new cell in the segment and return it.

As positional parameters you must pass as many vertices (Vertex objects) as needed to define a cell of the type and dimension of the domain.

Returns:A Cell object — the newly created cell
__iter__()[source]

Return a generator object to iterate over all the cells contained in the segment.

Returns:A generator over all the cells of the segment
class viennagrid.Cell(cell)[source]

Wrapper class that represents a cell.

__init__(cell)[source]

Create a new cell based on the given low-level ViennaGrid cell.

Parameters:cell (Low-level cell type from viennagrid.wrapper) – Low-level ViennaGrid cell to be wrapped in this high-level object.
vertices[source]

Return an object that allows you to access the list of all vertices that form the cell.

This object provides the following methods:

__call__()

This returns a Python list containing all the vertices of the cell, in ascending order of indices:

vertex_list = cell.vertices()
__len__()

This allows you to get the number of vertices in the cell as though it were a Python list:

num_vertices = len(cell.vertices)
__iter__()[source]

This allows you to get an iterator over the vertices of the cell like this:

iterator = iter(cell.vertices)
__getitem__(index)

This allows you to access each vertex by its index using bracket notation:

s0 = cell.vertices[0]
facets[source]

Return a list containing the facets of the cell.

edges[source]

Return a list containing the edges of the cell.

__iter__()[source]

Return a generator object to iterate over all the vertices that form the cell.

Returns:A generator over all the vertices of the cell
class viennagrid.Vertex(vertex)[source]

Wrapper class that represents a vertex.

__init__(vertex)[source]

Create a new vertex based on the given low-level ViennaGrid vertex.

Parameters:vertex (Low-level vertex type from viennagrid.wrapper) – Low-level ViennaGrid vertex to be wrapped in this high-level object.
to_point()[source]

Convert this vertex to a point.

Returns:viennagrid.Point
class viennagrid.Facet(facet)[source]

Wrapper class that represents a facet of a cell.

__init__(facet)[source]

Create a new facet based on the given low-level ViennaGrid facet.

Parameters:facet (Low-level facet type from viennagrid.wrapper) – Low-level ViennaGrid facet to be wrapped in this high-level object.
class viennagrid.Edge(edge)[source]

Wrapper class that represents an edge of a cell.

__init__(edge)[source]

Create a new edge based on the given low-level ViennaGrid edge.

Parameters:edge (Low-level edge type from viennagrid.wrapper) – Low-level ViennaGrid edge to be wrapped in this high-level object.

4.1.2. viennagrid.config — Configuration classes for domain definition

class viennagrid.config.Configuration(cell_tag, coord_system, dim=None)[source]

This class stores the necessary information to characterize the configuration of a domain:

  • numeric type (data type used for number representation)
  • coordinate system
  • dimension of the space
  • cell tag (string that identifies which type of cell the domain consists of)

and provides useful functions for retrieving the appropriate types for points, domains and segmentations.

cell_tag[source]

Return the cell tag.

coord_system[source]

Return the coordinate system tag.

dim[source]

Return the dimension of the space as an integer.

domain_type[source]

Return appropriate domain type based on this configuration

Returns:Low-level domain type
make_accessor(accessor_type)[source]

Create a new low-level (viennagrid.wrapper) accessor or field based on this configuration.

Parameters:domain (A domain class from viennagrid.wrapper which matches this configuration class) – Domain on which to base the segmentation.
Returns:Low-level accessors or field
make_domain()[source]

Create a new low-level (viennagrid.wrapper) domain based on this configuration

Returns:Low-level domain
make_point(*args, **kwargs)[source]

Create a new low-level (viennagrid.wrapper) point based on this configuration

As arguments you can pass the coordinates of the point to create, or nothing at all. If you specify the coordinates of the point, the number of arguments must match the dimension of the space (i.e. the number of coordinates that the are needed to describe the location of the point in the space).

Returns:Low-level point
make_segmentation(domain)[source]

Create a new low-level (viennagrid.wrapper) segmentation based on this configuration.

Parameters:domain (A domain class from viennagrid.wrapper which matches this configuration class) – Domain on which to base the segmentation.
Returns:Low-level segmentation
numeric_type[source]

Return the data type used for number representation (this is always the string ‘double’).

point_type[source]

Return appropriate point type based on this configuration

Returns:Low-level point type
segmentation_type[source]

Return appropriate segmentation type based on this configuration

Returns:Low-level segmentation type

4.1.2.1. Coordinate system tags and supported dimensions

viennagrid.config.CARTESIAN

String that identifies the cartesian coordinate system. Its value is the string ‘cartesian’.

viennagrid.config.POLAR

String that identifies the polar coordinate system. Its value is the string ‘polar’.

viennagrid.config.SPHERICAL

String that identifies the spherical coordinate system. Its value is the string ‘spherical’.

viennagrid.config.CYLINDRICAL

String that identifies the cylindrical coordinate system. Its value is the string ‘cylindrical’.

viennagrid.config.COORD_SYSTEM_TAGS

Tuple that contains all the supported coordinate system tags. The definition is as follows.

COORD_SYSTEM_TAGS = (CARTESIAN, POLAR, SPHERICAL, CYLINDRICAL)
viennagrid.config.SUPPORTED_DIMENSIONS

Dictionary where the key is a coordinate system tag and the value is a tuple containing all supported dimensions for the given coordinate system. The definition is as follows.

SUPPORTED_DIMENSIONS = {
        CARTESIAN:      (1, 2, 3),
        CYLINDRICAL:    (3,),
        POLAR:          (2,),
        SPHERICAL:      (3,),
}

4.1.2.2. Cell tags

viennagrid.config.LINE_TAG

String that identifies the given cell. Its value is the string ‘linear’.

viennagrid.config.TRIANGLE_TAG

String that identifies the given cell. Its value is the string ‘triangular’.

viennagrid.config.TETRAHEDRON_TAG

String that identifies the given cell. Its value is the string ‘tetrahedral’.

viennagrid.config.QUADRILATERAL_TAG

String that identifies the given cell. Its value is the string ‘quadrilateral’.

viennagrid.config.HEXAHEDRON_TAG

String that identifies the given cell. Its value is the string ‘hexahedral’.

viennagrid.config.CELL_TAGS

Tuple that contains all the supported cell tags. The definition is as follows.

CELL_TAGS = (LINE_TAG, TRIANGLE_TAG, TETRAHEDRON_TAG, QUADRILATERAL_TAG, HEXAHEDRON_TAG)

4.1.2.3. Common configurations

viennagrid.config.linear_1d

Instance of class Configuration that holds the configuration of a linear domain of cartesian 1D points. This is exactly the same as:

Configuration(LINE_TAG, CARTESIAN, 1)
viennagrid.config.linear_2d

Instance of class Configuration that holds the configuration of a linear domain of cartesian 2D points. This is exactly the same as:

Configuration(LINE_TAG, CARTESIAN, 2)
viennagrid.config.linear_3d

Instance of class Configuration that holds the configuration of a linear domain of cartesian 3D points. This is exactly the same as:

Configuration(LINE_TAG, CARTESIAN, 3)
viennagrid.config.triangular_2d

Instance of class Configuration that holds the configuration of a triangular domain of cartesian 2D points. This is exactly the same as:

Configuration(TRIANGLE_TAG, CARTESIAN, 2)
viennagrid.config.triangular_3d

Instance of class Configuration that holds the configuration of a triangular domain of cartesian 3D points. This is exactly the same as:

Configuration(TRIANGLE_TAG, CARTESIAN, 3)
viennagrid.config.tetrahedral_3d

Instance of class Configuration that holds the configuration of a tetrahedral domain of cartesian 3D points. This is exactly the same as:

Configuration(TETRAHEDRON_TAG, CARTESIAN, 3)
viennagrid.config.quadrilateral_2d

Instance of class Configuration that holds the configuration of a quadrilateral domain of cartesian 2D points. This is exactly the same as:

Configuration(QUADRILATERAL_TAG, CARTESIAN, 2)
viennagrid.config.quadrilateral_3d

Instance of class Configuration that holds the configuration of a quadrilateral domain of cartesian 3D points. This is exactly the same as:

Configuration(QUADRILATERAL_TAG, CARTESIAN, 3)
viennagrid.config.hexahedral_3d

Instance of class Configuration that holds the configuration of a hexahedral domain of cartesian 3D points. This is exactly the same as:

Configuration(HEXAHEDRON_TAG, CARTESIAN, 3)

4.1.3. viennagrid.algorithms — Common algorithms on mesh elements

viennagrid.algorithms.apply_voronoi(dom)[source]

Compute Voronoi information of the given domain.

Parameters:dom (viennagrid.Domain) – Domain
Raises :TypeError
viennagrid.algorithms.cell_refine(dom, seg, predicate)[source]

Refine all cells of the given domain and segmentation which match a given predicate.

Parameters:
  • dom (viennagrid.Domain) – Domain to refine
  • seg (viennagrid.Segmentation) – Segmentation of the domain to refine
  • predicate (function object that accepts a cell as parameter and returns a boolean) – Function that tells whether a cell should be refined or not
Returns:

A two-element tuple containing the output domain and segmentation after the refinement.

Raises :

TypeError

viennagrid.algorithms.centroid(cell)[source]

Compute the centroid of the given cell.

Parameters:cell (viennagrid.Cell) – Cell whose centroid should be computed
Returns:viennagrid.Point — the centroid of the cell
Raises :TypeError
viennagrid.algorithms.circumcenter(cell)[source]

Compute the circumcenter of the given cell.

Parameters:cell (viennagrid.Cell) – Cell whose circumcenter should be computed
Returns:viennagrid.Point — the circumcenter of the cell
Raises :TypeError
viennagrid.algorithms.cross_prod(point1, point2)[source]

Compute the cross product of two vectors (represented by points).

Parameters:
Returns:

viennagrid.Point — the result of the cross product

Raises :

TypeError

viennagrid.algorithms.inner_prod(point1, point2)[source]

Compute the inner product of two vectors (represented by points).

Parameters:
Returns:

float — the result of the inner product

Raises :

TypeError

viennagrid.algorithms.is_boundary(domseg, boundary_elem)[source]

Check if the given element is a boundary element of the given domain or segment.

Parameters:
Returns:

bool — True if the given element is a boundary element of the given domain or segment; False otherwise.

Raises :

TypeError

viennagrid.algorithms.is_interface(seg0, seg1, interface_elem)[source]

Check if the given element is an interface element of the two given segments.

Parameters:
Returns:

bool — True if the given element is an interface element of the given segments; False otherwise.

Raises :

TypeError

viennagrid.algorithms.norm(point, norm_type=2)[source]

Compute the norm of a vector (represented by a point).

Parameters:
  • point (viennagrid.Point) – Point
  • norm_type (int or str) – Norm to calculate (at this time only 1, 2 and ‘inf’ are supported).
Returns:

float — the norm of the vector

Raises :

ValueError, TypeError

viennagrid.algorithms.refine(dom, seg, predicate)[source]

Refine all edges of the given domain and segmentation which match a given predicate.

Parameters:
  • dom (viennagrid.Domain) – Domain to refine
  • seg (viennagrid.Segmentation) – Segmentation of the domain to refine
  • predicate (function object that accepts an edge as parameter and returns a boolean) – Function that tells whether an edge should be refined or not
Returns:

A two-element tuple containing the output domain and segmentation after the refinement.

Raises :

TypeError

viennagrid.algorithms.refine_uniformly(dom, seg)[source]

Refine all edges of the given domain and segmentation.

Parameters:
Returns:

A two-element tuple containing the output domain and segmentation after the refinement.

Raises :

TypeError

viennagrid.algorithms.scale(dom, factor)[source]

Scale a domain by a given factor.

Parameters:
Raises :

TypeError

viennagrid.algorithms.spanned_volume(*args)[source]

Calculate the volume spanned by a set of points.

As arguments you have to pass an arbitrary number of points (viennagrid.Point objects).

Returns:float — the volume spanned by the set of points
Raises :TypeError
viennagrid.algorithms.surface(elem)[source]

Calculate the surface of the given element.

Parameters:elem (viennagrid.Cell, viennagrid.Domain or viennagrid.Segment) – Element whose surface should be calculated.
Returns:float — the surface of the cell, domain or segment
Raises :TypeError
viennagrid.algorithms.volume(elem)[source]

Calculate the volume of the given element.

Parameters:elem (viennagrid.Cell, viennagrid.Domain or viennagrid.Segment) – Element whose volume should be calculated.
Returns:float — the volume of the cell, domain or segment
Raises :TypeError

4.1.4. viennagrid.io — Input/output functions

exception viennagrid.io.BadFileFormatError[source]

Exception raised when the mesh file is not well-formatted and hence cannot be read.

viennagrid.io.read_netgen(filepath, domain, segmentation=None)[source]

Read mesh data from a Netgen file and save the read domain data into the given domain. If a segmentation is provided, also save segmentation data into the given segmentation.

Parameters:
Raises :

IOError, BadFileFormatError, TypeError

viennagrid.io.read_vtk(filepath, domain, segmentation=None, accessors={})[source]

Read mesh data from a VTK file and save the read domain data into the given domain. If a segmentation is provided, also save segmentation data into the given segmentation.

Parameters:
Raises :

IOError, BadFileFormatError, TypeError

viennagrid.io.write_opendx(filepath, domain, accessors={})[source]

Write mesh data from the given domain to an OpenDX file.

Parameters:
  • filepath (str) – Path to the mesh file.
  • domain (viennagrid.Domain) – Domain
  • accessors (dict) – Accessors to be used to get quantities that should be written to the mesh file, in the form of a dictionary where keys are the names of the quantities (str) and where the values are accessors (viennagrid.accessors.Accessor) or fields(viennagrid.accessors.Field)
Raises :

IOError, BadFileFormatError, TypeError

viennagrid.io.write_vtk(filepath, domain, segmentation=None, accessors={})[source]

Write mesh data from the given domain to a VTK file. If a segmentation is provided, also write the segmentation data to the file.

Parameters:
Raises :

IOError, BadFileFormatError, TypeError

4.1.5. viennagrid.accessors — Interface for storing scalar quantities in meshes

class viennagrid.accessors.Accessor(accessor_type, domain_config)[source]

Wrapper class that represents an accessor for vertices or cells.

get_value(elem)[source]

Get the value corresponding to the element (vertex or cell) associated with the accessor.

Parameters:elem (viennagrid.Vertex or viennagrid.Cell (or any vertex or cell type from viennagrid.wrapper)) – the vertex or cell whose value should be returned
Returns:float — the value corresponding to the element
set_value(elem, new_value)[source]

Set the value corresponding to the element (vertex or cell) associated with the accessor.

Parameters:
class viennagrid.accessors.Field(accessor_type, domain_config, default_value)[source]

Wrapper class that represents a field accessor for vertices or cells.

get_value(elem)[source]

Get the value corresponding to the element (vertex or cell) associated with the field accessor.

Parameters:elem (viennagrid.Vertex or viennagrid.Cell (or any vertex or cell type from viennagrid.wrapper)) – the vertex or cell whose value should be returned
Returns:float — the value corresponding to the element
set_value(elem, new_value)[source]

Set the value corresponding to the element (vertex or cell) associated with the field accessor.

Parameters: