def setModelCoordinatesField(self, modelCoordinatesField: Field):
     finiteElementField = modelCoordinatesField.castFiniteElement()
     assert finiteElementField.isValid() and (
         finiteElementField.getNumberOfComponents() == 3)
     self._modelCoordinatesField = finiteElementField
     self._modelCoordinatesFieldName = modelCoordinatesField.getName()
     modelReferenceCoordinatesFieldName = "reference_" + self._modelCoordinatesField.getName(
     )
     orphanFieldByName(self._fieldmodule,
                       modelReferenceCoordinatesFieldName)
     self._modelReferenceCoordinatesField = createFieldFiniteElementClone(
         self._modelCoordinatesField, modelReferenceCoordinatesFieldName)
     self._updateMarkerCoordinatesField()
     self._updateMarkerDataLocationCoordinatesField()
Exemple #2
0
def create_field_finite_element_clone(source_field: Field,
                                      name: str,
                                      managed=False) -> FieldFiniteElement:
    """
    Copy an existing Finite Element Field to a new field of supplied name.
    Note: does not handle time-varying parameters.
    New field is not managed by default.
    :param source_field: Zinc finite element field to copy.
    :param name: The name of the new field, asserts that no field of that name exists.
    :param managed: Managed state of field created here.
    :return: New identically defined field with supplied name.
    """
    assert source_field.castFiniteElement().isValid(), \
        "opencmiss.utils.zinc.field.createFieldFiniteElementClone.  Not a Zinc finite element field"
    fieldmodule = source_field.getFieldmodule()
    field = fieldmodule.findFieldByName(name)
    assert not field.isValid(
    ), "opencmiss.utils.zinc.field.createFieldFiniteElementClone.  Target field name is in use"
    with ChangeManager(fieldmodule):
        # Zinc needs a function to do this efficiently; currently serialise to string, replace field name and reload!
        source_name = source_field.getName()
        region = fieldmodule.getRegion()
        sir = region.createStreaminformationRegion()
        srm = sir.createStreamresourceMemory()
        sir.setFieldNames([source_name])
        region.write(sir)
        result, buffer = srm.getBuffer()
        # small risk of modifying other text here:
        source_bytes = bytes(") " + source_name + ",", "utf-8")
        target_bytes = bytes(") " + name + ",", "utf-8")
        buffer = buffer.replace(source_bytes, target_bytes)
        sir = region.createStreaminformationRegion()
        sir.createStreamresourceMemoryBuffer(buffer)
        result = region.read(sir)
        assert result == RESULT_OK
    # note currently must have called endChange before field can be found
    field = fieldmodule.findFieldByName(name).castFiniteElement()
    field.setManaged(managed)
    assert field.isValid()
    return field
 def setMarkerGroup(self, markerGroup: Field):
     self._markerGroup = None
     self._markerGroupName = None
     self._markerNodeGroup = None
     self._markerLocationField = None
     self._markerCoordinatesField = None
     self._markerNameField = None
     self._markerDataGroup = None
     self._markerDataCoordinatesField = None
     self._markerDataNameField = None
     self._markerDataLocationField = None
     self._markerDataLocationCoordinatesField = None
     self._markerDataDeltaField = None
     self._markerDataLocationGroupField = None
     self._markerDataLocationGroup = None
     if not markerGroup:
         return
     fieldGroup = markerGroup.castGroup()
     assert fieldGroup.isValid()
     self._markerGroup = fieldGroup
     self._markerGroupName = markerGroup.getName()
     nodes = self._fieldmodule.findNodesetByFieldDomainType(
         Field.DOMAIN_TYPE_NODES)
     self._markerNodeGroup = self._markerGroup.getFieldNodeGroup(
         nodes).getNodesetGroup()
     if self._markerNodeGroup.isValid():
         node = self._markerNodeGroup.createNodeiterator().next()
         if node.isValid():
             fieldcache = self._fieldmodule.createFieldcache()
             fieldcache.setNode(node)
             fielditer = self._fieldmodule.createFielditerator()
             field = fielditer.next()
             while field.isValid():
                 if field.isDefinedAtLocation(fieldcache):
                     if (not self._markerLocationField
                         ) and field.castStoredMeshLocation().isValid():
                         self._markerLocationField = field
                     elif (not self._markerNameField) and (
                             field.getValueType()
                             == Field.VALUE_TYPE_STRING):
                         self._markerNameField = field
                 field = fielditer.next()
             self._updateMarkerCoordinatesField()
     else:
         self._markerNodeGroup = None
     datapoints = self._fieldmodule.findNodesetByFieldDomainType(
         Field.DOMAIN_TYPE_DATAPOINTS)
     self._markerDataGroup = self._markerGroup.getFieldNodeGroup(
         datapoints).getNodesetGroup()
     if self._markerDataGroup.isValid():
         datapoint = self._markerDataGroup.createNodeiterator().next()
         if datapoint.isValid():
             fieldcache = self._fieldmodule.createFieldcache()
             fieldcache.setNode(datapoint)
             fielditer = self._fieldmodule.createFielditerator()
             field = fielditer.next()
             while field.isValid():
                 if field.isDefinedAtLocation(fieldcache):
                     if (not self._markerDataCoordinatesField) and field.isTypeCoordinate() and \
                             (field.getNumberOfComponents() == 3) and (field.castFiniteElement().isValid()):
                         self._markerDataCoordinatesField = field
                     elif (not self._markerDataNameField) and (
                             field.getValueType()
                             == Field.VALUE_TYPE_STRING):
                         self._markerDataNameField = field
                 field = fielditer.next()
     else:
         self._markerDataGroup = None
     self._calculateMarkerDataLocations()
 def setDataCoordinatesField(self, dataCoordinatesField: Field):
     finiteElementField = dataCoordinatesField.castFiniteElement()
     assert finiteElementField.isValid() and (
         finiteElementField.getNumberOfComponents() == 3)
     self._dataCoordinatesFieldName = dataCoordinatesField.getName()
     self._dataCoordinatesField = finiteElementField