예제 #1
0
def createFaceMeshGroupExteriorOnFace(fieldmodule : Fieldmodule, elementFaceType) -> MeshGroup:
    """
    Returns mesh group for the exterior surface on the face described
    by elementFaceType.
    """
    with ChangeManager(fieldmodule):
        isExterior = fieldmodule.createFieldIsExterior()
        isOnFace = fieldmodule.createFieldIsOnFace(elementFaceType)
        mesh2d = fieldmodule.findMeshByDimension(2)
        faceElementGroup = fieldmodule.createFieldElementGroup(mesh2d)
        faceMeshGroup = faceElementGroup.getMeshGroup()
        faceMeshGroup.addElementsConditional(fieldmodule.createFieldAnd(isExterior, isOnFace))
        del isExterior
        del isOnFace
    return faceMeshGroup
예제 #2
0
def find_or_create_field_finite_element(fieldmodule: Fieldmodule, name: str, components_count: int,
                                        component_names=None, managed=False, type_coordinate=False)\
        -> FieldFiniteElement:
    """
    Finds or creates a finite element field for the specified number of real components.

    :param fieldmodule:  Zinc Fieldmodule to find or create field in.
    :param name:  Name of field to find or create.
    :param components_count: Number of components / dimension of field, from 1 to 3.
    :param component_names: Optional list of component names.
    :param managed: Managed state of field if created here.
    :param type_coordinate: Default value of flag indicating field gives geometric coordinates.
    :return: Zinc FieldFiniteElement, invalid if error.
    """
    assert (components_count > 0), "opencmiss.utils.zinc.field.find_or_create_field_finite_element." \
                                   "  Invalid components_count"
    assert (not component_names) or (len(component_names) >= components_count),\
        "opencmiss.utils.zinc.field.find_or_create_field_finite_element.  Invalid component_names"
    if field_exists(fieldmodule, name, 'FiniteElement', components_count):
        existing_field = fieldmodule.findFieldByName(name)
        return existing_field.castFiniteElement()

    return create_field_finite_element(fieldmodule, name, components_count,
                                       component_names, managed,
                                       type_coordinate)
예제 #3
0
def find_or_create_field_stored_mesh_location(fieldmodule: Fieldmodule, mesh: Mesh, name=None, managed=True)\
        -> FieldStoredMeshLocation:
    """
    Get or create a stored mesh location field for storing locations in the
    supplied mesh, used for storing data projections.
    Note can't currently verify existing field stores locations in the supplied mesh.
    New field is managed by default.

    :param fieldmodule:  Zinc fieldmodule to find or create field in.
    :param mesh:  Mesh to store locations in, from same fieldmodule.
    :param name:  Name of new field. If not defined, defaults to "location_" + mesh.getName().
    :param managed: Managed state of field if created here.
    """
    if not name:
        name = "location_" + mesh.getName()
    field = fieldmodule.findFieldByName(name)
    # StoredMeshLocation field can only have 1 component; its value is an element + xi coordinates
    if field_exists(fieldmodule, name, 'StoredMeshLocation', 1):
        mesh_location_field = field.castStoredMeshLocation()
        return mesh_location_field

    return create_field_stored_mesh_location(fieldmodule,
                                             mesh,
                                             name=name,
                                             managed=managed)
예제 #4
0
def get_unique_field_name(fieldmodule: Fieldmodule, name: str) -> str:
    """
    Return a unique field name in fieldmodule either equal to name or
    appending a number starting at 1 and increasing.

    :param fieldmodule: The fieldmodule to get a unique name in.
    :param name: The name to match or append a number to.
    """
    field = fieldmodule.findFieldByName(name)
    if not field.isValid():
        return name
    number = 1
    while True:
        next_name = name + str(number)
        field = fieldmodule.findFieldByName(next_name)
        if not field.isValid():
            return next_name
        number += 1
예제 #5
0
def get_highest_dimension_mesh(fieldmodule: Fieldmodule) -> Mesh:
    '''
    Get highest dimension non-empty mesh.
    :return: Zinc Mesh or None if all are empty.
    '''
    for dimension in range(3, 0, -1):
        mesh = fieldmodule.findMeshByDimension(dimension)
        if mesh.getSize() > 0:
            return mesh
    return None
예제 #6
0
def orphan_field_by_name(fieldmodule: Fieldmodule, name: str):
    """
    Find existing field with the name in fieldmodule.
    If it exists, uniquely rename it (prefix with ".destroy_" and append unique number)
    and unmanage it so destroyed when no longer in use.
    """
    field = fieldmodule.findFieldByName(name)
    if field.isValid():
        field.setName(get_unique_field_name(fieldmodule, ".destroy_" + name))
        field.setManaged(False)
예제 #7
0
def create_field_finite_element(fieldmodule: Fieldmodule, name: str, components_count: int,
                                component_names=None, managed=False, type_coordinate=False) -> FieldFiniteElement:
    with ChangeManager(fieldmodule):
        field = fieldmodule.createFieldFiniteElement(components_count)
        field.setName(name)
        field.setManaged(managed)
        field.setTypeCoordinate(type_coordinate)
        if component_names is not None:
            for index, component_name in enumerate(component_names[:components_count]):
                field.setComponentName(index + 1, component_name)

    return field
예제 #8
0
def create_field_stored_string(fieldmodule: Fieldmodule, name="name", managed=False) -> Field:
    """
    Creates a stored string field for defining names on nodes or datapoints.
    New field is not managed by default.

    :param fieldmodule:  Zinc fieldmodule to find or create field in.
    :param name:  Name of field to find or create.
    :param managed: Managed state of field if created here.
    :return: Zinc Field.
    """
    with ChangeManager(fieldmodule):
        stored_string_field = fieldmodule.createFieldStoredString()
        stored_string_field.setName(name)
        stored_string_field.setManaged(managed)
    return stored_string_field
예제 #9
0
def create_field_group(fieldmodule: Fieldmodule, name: str, managed=False) -> FieldGroup:
    """
    Finds or creates a Group field of the supplied name.
    New field is not managed by default.

    :param fieldmodule:  Zinc fieldmodule to find or create field in.
    :param name:  Name of field to find or create.
    :param managed: Managed state of field if created here.
    :return: Zinc FieldGroup.
    """
    with ChangeManager(fieldmodule):
        group = fieldmodule.createFieldGroup()
        group.setName(name)
        group.setManaged(managed)
    return group
예제 #10
0
def find_or_create_field_stored_string(fieldmodule: Fieldmodule, name="name", managed=True) -> Field:
    """
    Finds or creates a stored string field for defining names on nodes or
    datapoints. Note can't use Field.castStoredString API as not released.
    New field is managed by default.

    :param fieldmodule:  Zinc fieldmodule to find or create field in.
    :param name:  Name of field to find or create.
    :param managed: Managed state of field if created here.
    :return: Zinc Field.
    """
    field = fieldmodule.findFieldByName(name)
    if field.isValid():
        if field.getValueType() == Field.VALUE_TYPE_STRING:
            return field
    return create_field_stored_string(fieldmodule, name, managed=managed)
예제 #11
0
def find_or_create_field_group(fieldmodule: Fieldmodule, name: str, managed=True) -> FieldGroup:
    """
    Finds or creates a Group field of the supplied name.
    New field is managed by default.

    :param fieldmodule:  Zinc fieldmodule to find or create field in.
    :param name:  Name of field to find or create.
    :param managed: Managed state of field if created here.
    :return: Zinc FieldGroup.
    """
    field = fieldmodule.findFieldByName(name)
    if field.isValid():
        group = field.castGroup()
        if group:
            return group
    return create_field_group(fieldmodule, name, managed=managed)
예제 #12
0
def find_or_create_field_fibres(fieldmodule: Fieldmodule, name="fibres", components_count=3, managed=True) \
        -> FieldFiniteElement:
    """
    Finds or creates a finite element fibre field.
    New field has component names: "fibre angle", "imbrication angle", "sheet angle".
    New field is managed by default.

    :param fieldmodule:  Zinc fieldmodule to find or create field in.
    :param name:  Name of field to find or create.
    :param components_count: Number of components of field, from 1 to 3.
    :param managed: Managed state of field if created here.
    :return: Zinc FieldFiniteElement
    """
    assert 1 <= components_count <= 3
    if field_exists(fieldmodule, name, 'FiniteElement', components_count):
        fibres = fieldmodule.findFieldByName(name).castFiniteElement()
        if fibres.getCoordinateSystemType() == Field.COORDINATE_SYSTEM_TYPE_FIBRE:
            return fibres
    return create_field_fibres(fieldmodule, name, components_count, managed=managed)
예제 #13
0
def create_field_stored_mesh_location(fieldmodule: Fieldmodule, mesh: Mesh, name=None, managed=False)\
        -> FieldStoredMeshLocation:
    """
    Create a stored mesh location field for storing locations in the
    supplied mesh, used for storing data projections.
    New field is not managed by default.

    :param fieldmodule:  Zinc fieldmodule to find or create field in.
    :param mesh:  Mesh to store locations in, from same fieldmodule.
    :param name:  Name of new field. If not defined, defaults to "location_" + mesh.getName().
    :param managed: Managed state of field.
    :return: Zinc FieldStoredMeshLocation
    """
    if not name:
        name = "location_" + mesh.getName()
    with ChangeManager(fieldmodule):
        mesh_location_field = fieldmodule.createFieldStoredMeshLocation(mesh)
        mesh_location_field.setName(name)
        mesh_location_field.setManaged(managed)
    return mesh_location_field
예제 #14
0
def field_exists(fieldmodule: Fieldmodule, name: str, field_type, components_count) -> bool:
    """
    Tests to determine if the field with the given name exists in the given field module.

    :param fieldmodule: Zinc field module to search.
    :param name: Name of field to find.
    :param field_type: Type of field if derived type. Default: finiteelement.
    :param components_count: Number of components in the field. Default: 3.
    :return: True if the field is found in the module with the given name and number of components,
    false otherwise.
    """
    field = fieldmodule.findFieldByName(name)
    if field.isValid():
        if hasattr(field, 'cast' + field_type):
            field = getattr(field, 'cast' + field_type)()
            return field.isValid() and field.getNumberOfComponents() == components_count

        return field.getNumberOfComponents() == components_count

    return False
예제 #15
0
def create_field_euler_angles_rotation_matrix(fieldmodule: Fieldmodule,
                                              euler_angles: Field) -> Field:
    """
    From OpenCMISS-Zinc graphics_library.cpp, matrix transposed to row major.
    Matrix is product RzRyRx, giving rotation about x, then y, then z with
    positive angles rotating by right hand rule about axis.
    :param fieldmodule: The fieldmodule to create the field in.
    :param euler_angles: 3-component field of angles in radians, components:
    0 = azimuth (about z)
    1 = elevation (about y)
    2 = roll (about x)
    :return: 3x3 rotation matrix field suitable for pre-multiplying vector v
    i.e. v' = Mv
    """
    assert euler_angles.getNumberOfComponents() == 3
    with ChangeManager(fieldmodule):
        azimuth = fieldmodule.createFieldComponent(euler_angles, 1)
        cos_azimuth = fieldmodule.createFieldCos(azimuth)
        sin_azimuth = fieldmodule.createFieldSin(azimuth)
        elevation = fieldmodule.createFieldComponent(euler_angles, 2)
        cos_elevation = fieldmodule.createFieldCos(elevation)
        sin_elevation = fieldmodule.createFieldSin(elevation)
        roll = fieldmodule.createFieldComponent(euler_angles, 3)
        cos_roll = fieldmodule.createFieldCos(roll)
        sin_roll = fieldmodule.createFieldSin(roll)
        minus_one = fieldmodule.createFieldConstant([-1.0])
        cos_azimuth_sin_elevation = cos_azimuth * sin_elevation
        sin_azimuth_sin_elevation = sin_azimuth * sin_elevation
        matrix_components = [
            cos_azimuth * cos_elevation,
            cos_azimuth_sin_elevation * sin_roll - sin_azimuth * cos_roll,
            cos_azimuth_sin_elevation * cos_roll + sin_azimuth * sin_roll,
            sin_azimuth * cos_elevation,
            sin_azimuth_sin_elevation * sin_roll + cos_azimuth * cos_roll,
            sin_azimuth_sin_elevation * cos_roll - cos_azimuth * sin_roll,
            minus_one * sin_elevation, cos_elevation * sin_roll,
            cos_elevation * cos_roll
        ]
        rotation_matrix = fieldmodule.createFieldConcatenate(matrix_components)
    return rotation_matrix
예제 #16
0
def create_field_euler_angles_rotation_matrix(fieldmodule: Fieldmodule, euler_angles: Field) -> Field:
    """
    From OpenCMISS-Zinc graphics_library.cpp, transposed.
    :param fieldmodule:
    :param euler_angles: 3-component field of angles in radians, components:
    1 = azimuth (about z)
    2 = elevation (about rotated y)
    3 = roll (about rotated x)
    :return: 3x3 rotation matrix field suitable for pre-multiplying [x, y, z].
    """
    assert euler_angles.getNumberOfComponents() == 3
    with ChangeManager(fieldmodule):
        azimuth = fieldmodule.createFieldComponent(euler_angles, 1)
        cos_azimuth = fieldmodule.createFieldCos(azimuth)
        sin_azimuth = fieldmodule.createFieldSin(azimuth)
        elevation = fieldmodule.createFieldComponent(euler_angles, 2)
        cos_elevation = fieldmodule.createFieldCos(elevation)
        sin_elevation = fieldmodule.createFieldSin(elevation)
        roll = fieldmodule.createFieldComponent(euler_angles, 3)
        cos_roll = fieldmodule.createFieldCos(roll)
        sin_roll = fieldmodule.createFieldSin(roll)
        minus_one = fieldmodule.createFieldConstant([-1.0])
        cos_azimuth_sin_elevation = cos_azimuth*sin_elevation
        sin_azimuth_sin_elevation = sin_azimuth*sin_elevation
        matrix_components = [
            cos_azimuth*cos_elevation,
            cos_azimuth_sin_elevation*sin_roll - sin_azimuth*cos_roll,
            cos_azimuth_sin_elevation*cos_roll + sin_azimuth*sin_roll,
            sin_azimuth*cos_elevation,
            sin_azimuth_sin_elevation*sin_roll + cos_azimuth*cos_roll,
            sin_azimuth_sin_elevation*cos_roll - cos_azimuth*sin_roll,
            minus_one*sin_elevation,
            cos_elevation*sin_roll,
            cos_elevation*cos_roll]
        rotation_matrix = fieldmodule.createFieldConcatenate(matrix_components)
    return rotation_matrix