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)
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)
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
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)
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)
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)
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)
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