def reverseMirrorVector(self, v): """ Find the reverse mirror image of vector v on plane p :param v: Vector. :return: image vector. """ n = self._planeUnitNormalVector return [-v[c] + 2 * vector.dotproduct(v, n) * n[c] for c in range(3)]
def getPointDistanceFromPlane(self, x): """ Get distance from point x to plane p. Returns negative distance, if point x is on the negative side of the plane. :param x: Point coordinates. :return: Distance. """ n = self._planeUnitNormalVector ma = self._magNormal d = self._planeDParam return vector.dotproduct(x, n) - d / ma
def calculate_surface_delta_xi(d1, d2, direction): ''' Calculate dxi1, dxi2 in 3-D vector direction. :param d1, d2: Derivatives of coordinate w.r.t. xi1, xi2. :param direction: 3-D vector. :return: delta_xi1, delta_xi2 ''' # overdetermined system (3-D vector, 2-D surface), solve least squares # A transpose A x = A transpose b a = [[0.0, 0.0], [0.0, 0.0]] b = [0.0, 0.0] dx_dxi = [d1, d2] for i in range(2): for j in range(2): for k in range(3): a[i][j] += dx_dxi[i][k] * dx_dxi[j][k] for k in range(3): b[i] += dx_dxi[i][k] * direction[k] # 2x2 matrix inverse deta = a[0][0] * a[1][1] - a[0][1] * a[1][0] if deta > 0.0: inva = [[a[1][1] / deta, -a[0][1] / deta], [-a[1][0] / deta, a[0][0] / deta]] delta_xi1 = inva[0][0] * b[0] + inva[0][1] * b[1] delta_xi2 = inva[1][0] * b[0] + inva[1][1] * b[1] else: # at pole: assume direction is inline with d1 or d2 and other is zero delta_xi2 = vector.dotproduct(d2, direction) if math.fabs(delta_xi2) > 0.0: delta_xi1 = 0.0 delta_xi2 = (1.0 if (delta_xi2 > 0.0) else -1.0 ) * vector.magnitude(direction) / vector.magnitude(d2) else: delta_xi1 = vector.dotproduct(d1, direction) if math.fabs(delta_xi1) > 0.0: delta_xi1 = (1.0 if (delta_xi1 > 0.0) else -1.0) * vector.magnitude( direction) / vector.magnitude(d1) delta_xi2 = 0.0 #delx = [ (delta_xi1*d1[c] + delta_xi2*d2[c]) for c in range(3) ] #print('delx', delx, 'dir', direction, 'diff', vector.magnitude([ (delx[c] - direction[c]) for c in range(3) ])) return delta_xi1, delta_xi2
def getCubicHermiteCurvature(v1, d1, v2, d2, radialVector, xi): """ :param radialVector: Radial direction, assumed unit normal to curve tangent at point. :return: Scalar curvature (1/R) of the 1-D cubic Hermite curve. """ tangent = interpolateCubicHermiteDerivative(v1, d1, v2, d2, xi) dTangent = interpolateCubicHermiteSecondDerivative(v1, d1, v2, d2, xi) #tangentVector = vector.normalise(tangent) #tangentCurvature = vector.dotproduct(dTangent, tangentVector) radialCurvature = vector.dotproduct(dTangent, radialVector) magTangent = vector.magnitude(tangent) curvature = radialCurvature / (magTangent * magTangent) return curvature
def getCubicHermiteCurvature(v1, d1, v2, d2, radialVector, xi): """ :param v1, v2: Values at xi = 0.0 and xi = 1.0, respectively. :param d1, d2: Derivatives w.r.t. xi at xi = 0.0 and xi = 1.0, respectively. :param radialVector: Radial direction, assumed unit normal to curve tangent at point. :param xi: Position in curve, nominally in [0.0, 1.0]. :return: Scalar curvature (1/R) of the 1-D cubic Hermite curve. """ tangent = interpolateCubicHermiteDerivative(v1, d1, v2, d2, xi) dTangent = interpolateCubicHermiteSecondDerivative(v1, d1, v2, d2, xi) #tangentVector = vector.normalise(tangent) #tangentCurvature = vector.dotproduct(dTangent, tangentVector) radialCurvature = vector.dotproduct(dTangent, radialVector) magTangent = vector.magnitude(tangent) curvature = radialCurvature / (magTangent * magTangent) return curvature
def generateNodesForUpperHalf(self): """ Generates coordinates and derivatives for the upper half by mirroring the lower half nodes and derivatives. It keeps the d1 direction. It uses mirrorPlane: plane ax+by+cz=d in form of [a,b,c,d] """ mirrorPlane = [-d for d in self.majorAxis ] + [-vector.dotproduct(self.majorAxis, self.centre)] mirror = Mirror(mirrorPlane) for n2 in range(self.elementsCountUp): for n1 in range(self.elementsCountAcrossMinor + 1): if self.px[n2][n1]: self.px[2 * self.elementsCountUp - n2][n1] = mirror.mirrorImageOfPoint( self.px[n2][n1]) self.pd1[2 * self.elementsCountUp - n2][n1] = mirror.reverseMirrorVector( self.pd1[n2][n1]) self.pd3[2 * self.elementsCountUp - n2][n1] = mirror.mirrorVector(self.pd3[n2][n1])
def setRimNodes(self, nx, nd1, nd2, nd3): """ Set nodes around the ellipse perimeter in order needed for creating a shield mesh. """ btx = self.px btd1 = self.pd1 btd2 = self.pd2 btd3 = self.pd3 elementsCountRim = 0 for n in range(self.elementsCountAround + 1): n1, n2 = self.__shield.convertRimIndex(n) btx[n2][n1] = nx[n] if n2 > elementsCountRim: # regular rows btd1[n2][n1] = nd1[n] btd3[n2][n1] = nd3[n] if n2 >= 2: btd3[n2][n1] = vector.setMagnitude( self.minorAxis, vector.dotproduct(nd3[n], self.minorAxis)) else: # around rim btd1[n2][n1] = nd1[n] btd3[n2][n1] = nd3[n] btd2[n2][n1] = nd2[n]
def createAnnulusMesh3d(nodes, mesh, nextNodeIdentifier, nextElementIdentifier, startPointsx, startPointsd1, startPointsd2, startPointsd3, startNodeId, startDerivativesMap, endPointsx, endPointsd1, endPointsd2, endPointsd3, endNodeId, endDerivativesMap, forceStartLinearXi3=False, forceMidLinearXi3=False, forceEndLinearXi3=False, maxStartThickness=None, maxEndThickness=None, useCrossDerivatives=False, elementsCountRadial=1, meshGroups=None, wallAnnotationGroups=None, tracksurface=None, startProportions=None, endProportions=None, rescaleStartDerivatives=False, rescaleEndDerivatives=False, sampleBlend=0.0): """ Create an annulus mesh from a loop of start points/nodes with specified derivative mappings to a loop of end points/nodes with specified derivative mappings. Derivative d3 is through the wall. Currently limited to single element layer through wall. Points/nodes order cycles fastest around the annulus, then through the wall. Note doesn't support cross derivatives. Arrays are indexed by n3 (node through wall, size 2), n2 (node along/radial), n1 (node around, variable size) and coordinate component c. :param nodes: The nodeset to create nodes in. :param mesh: The mesh to create elements in. :param nextNodeIdentifier, nextElementIdentifier: Next identifiers to use and increment. :param startPointsx, startPointsd1, startPointsd2, startPointsd3, endPointsx, endPointsd1, endPointsd2, endPointsd3: List array[n3][n1][c] or start/point coordinates and derivatives. To linearise through the wall, pass None to d3. If both ends are linear through the wall, interior points are linear through the wall. :param startNodeId, endNodeId: List array [n3][n1] of existing node identifiers to use at start/end. Pass None for argument if no nodes are specified at end. These arguments are 'all or nothing'. :param startDerivativesMap, endDerivativesMap: List array[n3][n1] of mappings for d/dxi1, d/dxi2, d/dxi3 at start/end of form: ( (1, -1, 0), (1, 0, 0), None ) where the first tuple means d/dxi1 = d/ds1 - d/ds2. Only 0, 1 and -1 may be used. None means use default e.g. d/dxi2 = d/ds2. Pass None for the entire argument to use the defaults d/dxi1 = d/ds1, d/dxi2 = d/ds2, d/dxi3 = d/ds3. Pass a 4th mapping to apply to d/dxi1 on other side of node; if not supplied first mapping applies both sides. :param forceStartLinearXi3, forceMidLinearXi3, forceEndLinearXi3: Force start, middle or end elements to be linear through the wall, even if d3 is supplied at either end. Can only use forceMidLinearXi3 only if at least one end is linear in d3. :param maxStartThickness, maxEndThickness: Optional maximum override on start/end thicknesses. :param useCrossDerivatives: May only be True if no derivatives maps are in use. :param elementsCountRadial: Optional number of elements in radial direction between start and end. :param meshGroups: Optional sequence of Zinc MeshGroup for adding all new elements to, or a sequence of length elementsCountRadial containing sequences of mesh groups to add rows of radial elements to from start to end. :param wallAnnotationGroups: Annotation groups for adding all new elements to a sequence of groups to add to elements through wall. :param tracksurface: Description for outer surface representation used for creating annulus mesh. Provides information for creating radial nodes on annulus that sit on tracksurface. Need startProportions and endProportions to work. :param startProportions: Proportion around and along of startPoints on tracksurface. These vary with nodes around as for startPoints. Values only given for tracksurface for outer layer (xi3 == 1). :param endProportions: Proportion around and along of endPoints on track surface. These vary with nodes around as for endPoints. Values only given for tracksurface for outer layer (xi3 == 1). :param rescaleStartDerivatives, rescaleEndDerivatives: Optional flags to compute and multiply additional scale factors on start, end or both radial derivatives to fit arc length, needed if derivatives are of the wrong scale for the radial distances and the chosen elementsCountRadial. If either is True, derivatives and sampled radial nodes are spaced for a gradual change of derivative from that at the other end. If both are True, scaling is set to give even sampling and arclength derivatives. :param sampleBlend: Real value varying from 0.0 to 1.0 controlling weighting of start and end derivatives when interpolating extra points in-between, where 0.0 = sample with equal end derivatives, and 1.0 = proportional to current magnitudes, interpolated in between. :return: Final values of nextNodeIdentifier, nextElementIdentifier """ assert (elementsCountRadial >= 1), 'createAnnulusMesh3d: Invalid number of radial elements' startLinearXi3 = (not startPointsd3) or forceStartLinearXi3 endLinearXi3 = (not endPointsd3) or forceEndLinearXi3 midLinearXi3 = (startLinearXi3 and endLinearXi3) or ( (startLinearXi3 or endLinearXi3) and forceMidLinearXi3) # get list whether each row of nodes in elements is linear in Xi3 # this is for element use; start/end nodes may have d3 even if element is linear rowLinearXi3 = [ startLinearXi3 ] + [midLinearXi3] * (elementsCountRadial - 1) + [endLinearXi3] assert (not useCrossDerivatives) or ((not startDerivativesMap) and (not endDerivativesMap)), \ 'createAnnulusMesh3d: Cannot use cross derivatives with derivatives map' nodesCountWall = len(startPointsx) assert (len(startPointsd1) == nodesCountWall) and (len(startPointsd2) == nodesCountWall) and \ (startLinearXi3 or (len(startPointsd3) == nodesCountWall)) and \ (len(endPointsx) == nodesCountWall) and (len(endPointsd1) == nodesCountWall) and \ (len(endPointsd2) == nodesCountWall) and (endLinearXi3 or (len(endPointsd3) == nodesCountWall)) and \ ((startNodeId is None) or (len(startNodeId) == nodesCountWall)) and \ ((endNodeId is None) or (len(endNodeId) == nodesCountWall)) and \ ((startDerivativesMap is None) or (len(startDerivativesMap) == nodesCountWall)) and \ ((endDerivativesMap is None) or (len(endDerivativesMap) == nodesCountWall)),\ 'createAnnulusMesh3d: Mismatch in number of layers through wall' elementsCountAround = nodesCountAround = len(startPointsx[0]) assert ( nodesCountAround > 1 ), 'createAnnulusMesh3d: Invalid number of points/nodes around annulus' for n3 in range(nodesCountWall): assert (len(startPointsx[n3]) == nodesCountAround) and (len(startPointsd1[n3]) == nodesCountAround) and \ (len(startPointsd2[n3]) == nodesCountAround) and \ (startLinearXi3 or (len(startPointsd3[n3]) == nodesCountAround)) and\ (len(endPointsx[n3]) == nodesCountAround) and (len(endPointsd1[n3]) == nodesCountAround) and \ (len(endPointsd2[n3]) == nodesCountAround) and \ (endLinearXi3 or (len(endPointsd3[n3]) == nodesCountAround)) and \ ((startNodeId is None) or (len(startNodeId[n3]) == nodesCountAround)) and\ ((endNodeId is None) or (len(endNodeId[n3]) == nodesCountAround)) and \ ((startDerivativesMap is None) or (len(startDerivativesMap[n3]) == nodesCountAround)) and \ ((endDerivativesMap is None) or (len(endDerivativesMap[n3]) == nodesCountAround)), \ 'createAnnulusMesh3d: Mismatch in number of points/nodes in layers through wall' rowMeshGroups = meshGroups if meshGroups: assert isinstance( meshGroups, Sequence), 'createAnnulusMesh3d: Mesh groups is not a sequence' if (len(meshGroups) == 0) or (not isinstance(meshGroups[0], Sequence)): rowMeshGroups = [meshGroups] * elementsCountRadial else: assert len(meshGroups) == elementsCountRadial, \ 'createAnnulusMesh3d: Length of meshGroups sequence does not equal elementsCountRadial' if wallAnnotationGroups: assert len(wallAnnotationGroups) == nodesCountWall - 1, \ 'createAnnulusMesh3d: Length of wallAnnotationGroups sequence does not equal elementsCountThroughWall' if tracksurface: assert startProportions and endProportions, \ 'createAnnulusMesh3d: Missing start and/or end proportions for use with tracksurface' assert len(startProportions) == nodesCountAround, \ 'createAnnulusMesh3d: Length of startProportions does not equal nodesCountAround' assert len(endProportions) == nodesCountAround, \ 'createAnnulusMesh3d: Length of endProportions does not equal nodesCountAround' fm = mesh.getFieldmodule() fm.beginChange() cache = fm.createFieldcache() coordinates = findOrCreateFieldCoordinates(fm) # Build arrays of points from start to end px = [[] for n3 in range(nodesCountWall)] pd1 = [[] for n3 in range(nodesCountWall)] pd2 = [[] for n3 in range(nodesCountWall)] pd3 = [[] for n3 in range(nodesCountWall)] # Find total wall thickness thicknessProportions = [] thicknesses = [] thicknesses.append([ vector.magnitude([ (startPointsx[nodesCountWall - 1][n1][c] - startPointsx[0][n1][c]) for c in range(3) ]) for n1 in range(nodesCountAround) ]) for n2 in range(1, elementsCountRadial): thicknesses.append([None] * nodesCountAround) thicknesses.append([ vector.magnitude([ (endPointsx[nodesCountWall - 1][n1][c] - endPointsx[0][n1][c]) for c in range(3) ]) for n1 in range(nodesCountAround) ]) for n3 in range(nodesCountWall): px[n3] = [startPointsx[n3], endPointsx[n3]] pd1[n3] = [startPointsd1[n3], endPointsd1[n3]] pd2[n3] = [startPointsd2[n3], endPointsd2[n3]] pd3[n3] = [ startPointsd3[n3] if (startPointsd3 is not None) else None, endPointsd3[n3] if (endPointsd3 is not None) else None ] startThicknessList = \ [vector.magnitude([(startPointsx[n3][n1][c] - startPointsx[n3 - (1 if n3 > 0 else 0)][n1][c]) for c in range(3)]) for n1 in range(len(startPointsx[n3]))] endThicknessList = \ [vector.magnitude([(endPointsx[n3][n1][c] - endPointsx[n3 - (1 if n3 > 0 else 0)][n1][c]) for c in range(3)]) for n1 in range(len(endPointsx[n3]))] thicknessList = [startThicknessList, endThicknessList] # thickness of each layer startThicknessProportions = [ thicknessList[0][c] / thicknesses[0][c] for c in range(nodesCountAround) ] endThicknessProportions = [ thicknessList[1][c] / thicknesses[-1][c] for c in range(nodesCountAround) ] thicknessProportions.append( [startThicknessProportions, endThicknessProportions]) if rescaleStartDerivatives: scaleFactorMapStart = [[] for n3 in range(nodesCountWall)] if rescaleEndDerivatives: scaleFactorMapEnd = [[] for n3 in range(nodesCountWall)] # following code adds in-between points, but also handles rescaling for 1 radial element for n3 in range(nodesCountWall): for n2 in range(1, elementsCountRadial): px[n3].insert(n2, [None] * nodesCountAround) pd1[n3].insert(n2, [None] * nodesCountAround) pd2[n3].insert(n2, [None] * nodesCountAround) pd3[n3].insert(n2, None if midLinearXi3 else [None] * nodesCountAround) thicknessProportions[n3].insert(n2, [None] * nodesCountAround) if maxStartThickness: for n1 in range(nodesCountAround): thicknesses[0][n1] = min(thicknesses[0][n1], maxStartThickness) if maxEndThickness: for n1 in range(nodesCountAround): thicknesses[-1][n1] = min(thicknesses[-1][n1], maxEndThickness) n3 = nodesCountWall - 1 for n1 in range(nodesCountAround): ax = startPointsx[n3][n1] ad1, ad2 = getMappedD1D2( [startPointsd1[n3][n1], startPointsd2[n3][n1]] + ([startPointsd3[n3][n1]] if startPointsd3 else []), startDerivativesMap[n3][n1] if startDerivativesMap else None) bx = endPointsx[n3][n1] bd1, bd2 = getMappedD1D2( [endPointsd1[n3][n1], endPointsd2[n3][n1]] + ([endPointsd3[n3][n1]] if endPointsd3 else []), endDerivativesMap[n3][n1] if endDerivativesMap else None) # sample between start and end points and derivatives # scaling end derivatives to arc length gives even curvature along the curve aMag = vector.magnitude(ad2) bMag = vector.magnitude(bd2) ad2Scaled = vector.setMagnitude( ad2, 0.5 * ((1.0 + sampleBlend) * aMag + (1.0 - sampleBlend) * bMag)) bd2Scaled = vector.setMagnitude( bd2, 0.5 * ((1.0 + sampleBlend) * bMag + (1.0 - sampleBlend) * aMag)) scaling = interp.computeCubicHermiteDerivativeScaling( ax, ad2Scaled, bx, bd2Scaled) ad2Scaled = [d * scaling for d in ad2Scaled] bd2Scaled = [d * scaling for d in bd2Scaled] derivativeMagnitudeStart = None if rescaleStartDerivatives else vector.magnitude( ad2) derivativeMagnitudeEnd = None if rescaleEndDerivatives else vector.magnitude( bd2) if tracksurface: mx, md2, md1, md3, mProportions = \ tracksurface.createHermiteCurvePoints(startProportions[n1][0], startProportions[n1][1], endProportions[n1][0], endProportions[n1][1], elementsCountRadial, derivativeStart=[d / elementsCountRadial for d in ad2Scaled], derivativeEnd=[d / elementsCountRadial for d in bd2Scaled]) mx, md2, md1 = \ tracksurface.resampleHermiteCurvePointsSmooth(mx, md2, md1, md3, mProportions, derivativeMagnitudeStart, derivativeMagnitudeEnd)[0:3] # interpolate thicknesses using xi calculated from radial arclength distances to points arcLengthInsideToRadialPoint = \ [0.0] + [interp.getCubicHermiteArcLength(mx[n2], md2[n2], mx[n2 + 1], md2[n2 + 1]) for n2 in range(elementsCountRadial)] arclengthInsideToOutside = sum(arcLengthInsideToRadialPoint) thi = [] for n2 in range(elementsCountRadial + 1): xi2 = arcLengthInsideToRadialPoint[ n2 - 1] / arclengthInsideToOutside thi.append(thicknesses[-1][n1] * xi2 + thicknesses[0][n1] * (1.0 - xi2)) thiProportion = [] for m3 in range(nodesCountWall): thiProportionRadial = [] for n2 in range(elementsCountRadial + 1): xi2 = arcLengthInsideToRadialPoint[ n2 - 1] / arclengthInsideToOutside thiProportionRadial.append( thicknessProportions[m3][-1][n1] * xi2 + thicknessProportions[m3][0][n1] * (1.0 - xi2)) thiProportion.append(thiProportionRadial) else: mx, md2, me, mxi = interp.sampleCubicHermiteCurvesSmooth( [ax, bx], [ad2Scaled, bd2Scaled], elementsCountRadial, derivativeMagnitudeStart, derivativeMagnitudeEnd)[0:4] md1 = interp.interpolateSampleLinear([ad1, bd1], me, mxi) thi = interp.interpolateSampleLinear( [thicknesses[0][n1], thicknesses[-1][n1]], me, mxi) thiProportion = [] for m3 in range(nodesCountWall): thiProportion.append( interp.interpolateSampleLinear([ thicknessProportions[m3][0][n1], thicknessProportions[m3][-1][n1] ], me, mxi)) # set scalefactors if rescaling, make same on inside for now if rescaleStartDerivatives: scaleFactor = vector.magnitude(md2[0]) / vector.magnitude(ad2) scaleFactorMapStart[n3].append(scaleFactor) if rescaleEndDerivatives: scaleFactor = vector.magnitude(md2[-1]) / vector.magnitude(bd2) scaleFactorMapEnd[n3].append(scaleFactor) for n2 in range(1, elementsCountRadial): px[n3][n2][n1] = mx[n2] pd1[n3][n2][n1] = md1[n2] pd2[n3][n2][n1] = md2[n2] thicknesses[n2][n1] = thi[n2] for m3 in range(nodesCountWall): thicknessProportions[m3][n2][n1] = thiProportion[m3][n2] xi3List = [[[[] for n1 in range(nodesCountAround)] for n2 in range(elementsCountRadial + 1)] for n3 in range(nodesCountWall)] for n1 in range(nodesCountAround): for n2 in range(elementsCountRadial + 1): xi3 = 0.0 for n3 in range(nodesCountWall): xi3 += thicknessProportions[n3][n2][n1] xi3List[n3][n2][n1] = xi3 # now get inner positions from normal and thickness, derivatives from curvature for n2 in range(1, elementsCountRadial): # first smooth derivative 1 around outer loop pd1[-1][n2] = \ interp.smoothCubicHermiteDerivativesLoop(px[-1][n2], pd1[-1][n2], magnitudeScalingMode=interp.DerivativeScalingMode.HARMONIC_MEAN) for n3 in range(0, nodesCountWall - 1): for n1 in range(nodesCountAround): xi3 = 1 - xi3List[n3][n2][n1] normal = vector.normalise( vector.crossproduct3(pd1[-1][n2][n1], pd2[-1][n2][n1])) thickness = thicknesses[n2][n1] * xi3 d3 = [d * thickness for d in normal] px[n3][n2][n1] = [(px[-1][n2][n1][c] - d3[c]) for c in range(3)] # calculate inner d1 from curvature around n1m = n1 - 1 n1p = (n1 + 1) % nodesCountAround curvature = 0.5 * (interp.getCubicHermiteCurvature( px[-1][n2][n1m], pd1[-1][n2][n1m], px[-1][n2][n1], pd1[-1] [n2][n1], normal, 1.0) + interp.getCubicHermiteCurvature( px[-1][n2][n1], pd1[-1][n2][n1], px[-1][n2][n1p], pd1[-1][n2][n1p], normal, 0.0)) factor = 1.0 + curvature * thickness pd1[n3][n2][n1] = [factor * d for d in pd1[-1][n2][n1]] # calculate inner d2 from curvature radially n2m = n2 - 1 n2p = n2 + 1 curvature = 0.5 * (interp.getCubicHermiteCurvature( px[-1][n2m][n1], pd2[-1][n2m][n1], px[-1][n2][n1], pd2[-1] [n2][n1], normal, 1.0) + interp.getCubicHermiteCurvature( px[-1][n2][n1], pd2[-1][n2][n1], px[-1][n2p][n1], pd2[-1][n2p][n1], normal, 0.0)) factor = 1.0 + curvature * thickness pd2[n3][n2][n1] = [factor * d for d in pd2[-1][n2][n1]] d2Scaled = [factor * d for d in pd2[-1][n2][n1]] if vector.dotproduct(vector.normalise(pd2[-1][n2][n1]), vector.normalise(d2Scaled)) == -1: pd2[n3][n2][n1] = [-factor * d for d in pd2[-1][n2][n1]] if not midLinearXi3: pd3[n3][n2][n1] = pd3[-1][n2][n1] = \ [d * thicknesses[n2][n1] * thicknessProportions[n3 + 1][n2][n1] for d in normal] # smooth derivative 1 around inner loop pd1[n3][n2] = interp.smoothCubicHermiteDerivativesLoop( px[n3][n2], pd1[n3][n2], magnitudeScalingMode=interp.DerivativeScalingMode.HARMONIC_MEAN ) for n3 in range(0, nodesCountWall): # smooth derivative 2 radially/along annulus for n1 in range(nodesCountAround): mx = [px[n3][n2][n1] for n2 in range(elementsCountRadial + 1)] md2 = [pd2[n3][n2][n1] for n2 in range(elementsCountRadial + 1)] # replace mapped start/end d2 md2[0] = getMappedD1D2( [startPointsd1[n3][n1], startPointsd2[n3][n1]] + ([startPointsd3[n3][n1]] if startPointsd3 else []), startDerivativesMap[n3][n1] if startDerivativesMap else None)[1] md2[-1] = getMappedD1D2( [endPointsd1[n3][n1], endPointsd2[n3][n1]] + ([endPointsd3[n3][n1]] if endPointsd3 else []), endDerivativesMap[n3][n1] if endDerivativesMap else None)[1] sd2 = interp.smoothCubicHermiteDerivativesLine( mx, md2, fixAllDirections=True, fixStartDerivative=not rescaleStartDerivatives, fixStartDirection=rescaleStartDerivatives, fixEndDerivative=not rescaleEndDerivatives, fixEndDirection=rescaleEndDerivatives, magnitudeScalingMode=interp.DerivativeScalingMode.HARMONIC_MEAN ) if rescaleStartDerivatives: scaleFactor = vector.magnitude(sd2[0]) / vector.magnitude( md2[0]) scaleFactorMapStart[n3].append(scaleFactor) if rescaleEndDerivatives: scaleFactor = vector.magnitude(sd2[-1]) / vector.magnitude( md2[-1]) scaleFactorMapEnd[n3].append(scaleFactor) for n2 in range(1, elementsCountRadial): pd2[n3][n2][n1] = sd2[n2] ############## # Create nodes ############## nodetemplate = nodes.createNodetemplate() nodetemplate.defineField(coordinates) nodetemplate.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_VALUE, 1) nodetemplate.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_D_DS1, 1) nodetemplate.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_D_DS2, 1) if useCrossDerivatives: nodetemplate.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_D2_DS1DS2, 1) nodetemplate.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_D_DS3, 1) if useCrossDerivatives: nodetemplate.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_D2_DS1DS3, 1) nodetemplate.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_D2_DS2DS3, 1) nodetemplate.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_D3_DS1DS2DS3, 1) nodetemplateLinearS3 = nodes.createNodetemplate() nodetemplateLinearS3.defineField(coordinates) nodetemplateLinearS3.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_VALUE, 1) nodetemplateLinearS3.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_D_DS1, 1) nodetemplateLinearS3.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_D_DS2, 1) if useCrossDerivatives: nodetemplateLinearS3.setValueNumberOfVersions( coordinates, -1, Node.VALUE_LABEL_D2_DS1DS2, 1) nodeIdentifier = nextNodeIdentifier nodeId = [[] for n3 in range(nodesCountWall)] for n2 in range(elementsCountRadial + 1): for n3 in range(nodesCountWall): if (n2 == 0) and (startNodeId is not None): rowNodeId = copy.deepcopy(startNodeId[n3]) elif (n2 == elementsCountRadial) and (endNodeId is not None): rowNodeId = copy.deepcopy(endNodeId[n3]) else: rowNodeId = [] nodetemplate1 = nodetemplate if pd3[n3][ n2] else nodetemplateLinearS3 for n1 in range(nodesCountAround): node = nodes.createNode(nodeIdentifier, nodetemplate1) rowNodeId.append(nodeIdentifier) cache.setNode(node) coordinates.setNodeParameters(cache, -1, Node.VALUE_LABEL_VALUE, 1, px[n3][n2][n1]) coordinates.setNodeParameters(cache, -1, Node.VALUE_LABEL_D_DS1, 1, pd1[n3][n2][n1]) coordinates.setNodeParameters(cache, -1, Node.VALUE_LABEL_D_DS2, 1, pd2[n3][n2][n1]) if pd3[n3][n2]: coordinates.setNodeParameters(cache, -1, Node.VALUE_LABEL_D_DS3, 1, pd3[n3][n2][n1]) nodeIdentifier = nodeIdentifier + 1 nodeId[n3].append(rowNodeId) ################# # Create elements ################# tricubichermite = eftfactory_tricubichermite(mesh, useCrossDerivatives) bicubichermitelinear = eftfactory_bicubichermitelinear( mesh, useCrossDerivatives) elementIdentifier = nextElementIdentifier elementtemplateStandard = mesh.createElementtemplate() elementtemplateStandard.setElementShapeType(Element.SHAPE_TYPE_CUBE) elementtemplateX = mesh.createElementtemplate() elementtemplateX.setElementShapeType(Element.SHAPE_TYPE_CUBE) elementsCountWall = nodesCountWall - 1 for e2 in range(elementsCountRadial): nonlinearXi3 = (not rowLinearXi3[e2]) or (not rowLinearXi3[e2 + 1]) eftFactory = tricubichermite if nonlinearXi3 else bicubichermitelinear eftStandard = eftFactory.createEftBasic() elementtemplateStandard.defineField(coordinates, -1, eftStandard) mapStartDerivatives = (e2 == 0) and (startDerivativesMap or rescaleStartDerivatives) mapStartLinearDerivativeXi3 = nonlinearXi3 and rowLinearXi3[e2] mapEndDerivatives = (e2 == (elementsCountRadial - 1)) and ( endDerivativesMap or rescaleEndDerivatives) mapEndLinearDerivativeXi3 = nonlinearXi3 and rowLinearXi3[e2 + 1] mapDerivatives = mapStartDerivatives or mapStartLinearDerivativeXi3 or \ mapEndDerivatives or mapEndLinearDerivativeXi3 for e3 in range(elementsCountWall): for e1 in range(elementsCountAround): en = (e1 + 1) % elementsCountAround nids = [ nodeId[e3][e2][e1], nodeId[e3][e2][en], nodeId[e3][e2 + 1][e1], nodeId[e3][e2 + 1][en], nodeId[e3 + 1][e2][e1], nodeId[e3 + 1][e2][en], nodeId[e3 + 1][e2 + 1][e1], nodeId[e3 + 1][e2 + 1][en] ] scaleFactors = [] if mapDerivatives: eft1 = eftFactory.createEftNoCrossDerivatives() # work out if scaling by global -1 scaleMinus1 = mapStartLinearDerivativeXi3 or mapEndLinearDerivativeXi3 if (not scaleMinus1 ) and mapStartDerivatives and startDerivativesMap: for n3 in range(2): n3Idx = n3 + e3 # need to handle 3 or 4 maps (e1 uses last 3, en uses first 3) for map in startDerivativesMap[n3Idx][e1][-3:]: if map and (-1 in map): scaleMinus1 = True break for map in startDerivativesMap[n3Idx][en][:3]: if map and (-1 in map): scaleMinus1 = True break if (not scaleMinus1 ) and mapEndDerivatives and endDerivativesMap: for n3 in range(2): n3Idx = n3 + e3 # need to handle 3 or 4 maps (e1 uses last 3, en uses first 3) for map in endDerivativesMap[n3Idx][e1][-3:]: if map and (-1 in map): scaleMinus1 = True break for map in endDerivativesMap[n3Idx][en][:3]: if map and (-1 in map): scaleMinus1 = True break # make node scale factors vary fastest by local node varying across lower xi nodeScaleFactorIds = [] for n3 in range(2): n3Idx = n3 + e3 if mapStartDerivatives and rescaleStartDerivatives: for i in range(2): derivativesMap = ( startDerivativesMap[n3Idx][e1][1] if (i == 0) else startDerivativesMap[n3Idx] [en][1]) if startDerivativesMap else None nodeScaleFactorIds.append( getQuadrantID(derivativesMap if derivativesMap else (0, 1, 0))) if mapEndDerivatives and rescaleEndDerivatives: for i in range(2): derivativesMap = ( endDerivativesMap[n3Idx][e1][1] if (i == 0) else endDerivativesMap[n3Idx][en] [1]) if endDerivativesMap else None nodeScaleFactorIds.append( getQuadrantID(derivativesMap if derivativesMap else (0, 1, 0))) setEftScaleFactorIds(eft1, [1] if scaleMinus1 else [], nodeScaleFactorIds) firstNodeScaleFactorIndex = 2 if scaleMinus1 else 1 firstStartNodeScaleFactorIndex = \ firstNodeScaleFactorIndex if (mapStartDerivatives and rescaleStartDerivatives) else None firstEndNodeScaleFactorIndex = \ (firstNodeScaleFactorIndex + (2 if firstStartNodeScaleFactorIndex else 0)) \ if (mapEndDerivatives and rescaleEndDerivatives) else None layerNodeScaleFactorIndexOffset = \ 4 if (firstStartNodeScaleFactorIndex and firstEndNodeScaleFactorIndex) else 2 if scaleMinus1: scaleFactors.append(-1.0) for n3 in range(2): n3Idx = n3 + e3 if firstStartNodeScaleFactorIndex: scaleFactors.append(scaleFactorMapStart[n3Idx][e1]) scaleFactors.append(scaleFactorMapStart[n3Idx][en]) if firstEndNodeScaleFactorIndex: scaleFactors.append(scaleFactorMapEnd[n3Idx][e1]) scaleFactors.append(scaleFactorMapEnd[n3Idx][en]) if mapStartLinearDerivativeXi3: eftFactory.setEftLinearDerivative2( eft1, [1, 5, 2, 6], Node.VALUE_LABEL_D_DS3, [Node.VALUE_LABEL_D2_DS1DS3]) if mapStartDerivatives: for i in range(2): lns = [1, 5] if (i == 0) else [2, 6] for n3 in range(2): n3Idx = n3 + e3 derivativesMap = \ (startDerivativesMap[n3Idx][e1] if (i == 0) else startDerivativesMap[n3Idx][en]) \ if startDerivativesMap else (None, None, None) # handle different d1 on each side of node d1Map = \ derivativesMap[0] if ((i == 1) or (len(derivativesMap) < 4)) else derivativesMap[3] d2Map = derivativesMap[1] if derivativesMap[ 1] else (0, 1, 0) d3Map = derivativesMap[2] # use temporary to safely swap DS1 and DS2: ln = [lns[n3]] if d1Map: remapEftNodeValueLabel( eft1, ln, Node.VALUE_LABEL_D_DS1, [(Node.VALUE_LABEL_D2_DS1DS2, [])]) if d3Map: remapEftNodeValueLabel( eft1, ln, Node.VALUE_LABEL_D_DS3, [(Node.VALUE_LABEL_D2_DS2DS3, [])]) if d2Map: remapEftNodeValueLabel( eft1, ln, Node.VALUE_LABEL_D_DS2, derivativeSignsToExpressionTerms( (Node.VALUE_LABEL_D_DS1, Node.VALUE_LABEL_D_DS2, Node.VALUE_LABEL_D_DS3), d2Map, (firstStartNodeScaleFactorIndex + i + n3 * layerNodeScaleFactorIndexOffset) if rescaleStartDerivatives else None)) if d1Map: remapEftNodeValueLabel( eft1, ln, Node.VALUE_LABEL_D2_DS1DS2, derivativeSignsToExpressionTerms( (Node.VALUE_LABEL_D_DS1, Node.VALUE_LABEL_D_DS2, Node.VALUE_LABEL_D_DS3), d1Map)) if d3Map: remapEftNodeValueLabel( eft1, ln, Node.VALUE_LABEL_D2_DS2DS3, derivativeSignsToExpressionTerms( (Node.VALUE_LABEL_D_DS1, Node.VALUE_LABEL_D_DS2, Node.VALUE_LABEL_D_DS3), d3Map)) if mapEndLinearDerivativeXi3: eftFactory.setEftLinearDerivative2( eft1, [3, 7, 4, 8], Node.VALUE_LABEL_D_DS3, [Node.VALUE_LABEL_D2_DS1DS3]) if mapEndDerivatives: for i in range(2): lns = [3, 7] if (i == 0) else [4, 8] for n3 in range(2): n3Idx = n3 + e3 derivativesMap = \ (endDerivativesMap[n3Idx][e1] if (i == 0) else endDerivativesMap[n3Idx][en]) \ if endDerivativesMap else (None, None, None) # handle different d1 on each side of node d1Map = derivativesMap[0] if ((i == 1) or (len(derivativesMap) < 4)) else \ derivativesMap[3] d2Map = derivativesMap[1] if derivativesMap[ 1] else (0, 1, 0) d3Map = derivativesMap[2] # use temporary to safely swap DS1 and DS2: ln = [lns[n3]] if d1Map: remapEftNodeValueLabel( eft1, ln, Node.VALUE_LABEL_D_DS1, [(Node.VALUE_LABEL_D2_DS1DS2, [])]) if d3Map: remapEftNodeValueLabel( eft1, ln, Node.VALUE_LABEL_D_DS3, [(Node.VALUE_LABEL_D2_DS2DS3, [])]) if d2Map: remapEftNodeValueLabel( eft1, ln, Node.VALUE_LABEL_D_DS2, derivativeSignsToExpressionTerms( (Node.VALUE_LABEL_D_DS1, Node.VALUE_LABEL_D_DS2, Node.VALUE_LABEL_D_DS3), d2Map, (firstEndNodeScaleFactorIndex + i + n3 * layerNodeScaleFactorIndexOffset) if rescaleEndDerivatives else None)) if d1Map: remapEftNodeValueLabel( eft1, ln, Node.VALUE_LABEL_D2_DS1DS2, derivativeSignsToExpressionTerms( (Node.VALUE_LABEL_D_DS1, Node.VALUE_LABEL_D_DS2, Node.VALUE_LABEL_D_DS3), d1Map)) if d3Map: remapEftNodeValueLabel( eft1, ln, Node.VALUE_LABEL_D2_DS2DS3, derivativeSignsToExpressionTerms( (Node.VALUE_LABEL_D_DS1, Node.VALUE_LABEL_D_DS2, Node.VALUE_LABEL_D_DS3), d3Map)) elementtemplateX.defineField(coordinates, -1, eft1) elementtemplate1 = elementtemplateX else: eft1 = eftStandard elementtemplate1 = elementtemplateStandard element = mesh.createElement(elementIdentifier, elementtemplate1) result2 = element.setNodesByIdentifier(eft1, nids) if scaleFactors: result3 = element.setScaleFactors(eft1, scaleFactors) # print('create element annulus', element.isValid(), elementIdentifier, eft1.validate(), # result2, result3 if scaleFactors else None, nids) elementIdentifier += 1 if rowMeshGroups: for meshGroup in rowMeshGroups[e2]: meshGroup.addElement(element) if wallAnnotationGroups: for annotationGroup in wallAnnotationGroups[e3]: meshGroup = annotationGroup.getMeshGroup(mesh) meshGroup.addElement(element) fm.endChange() return nodeIdentifier, elementIdentifier
def warpSegmentPoints(xList, d1List, d2List, segmentAxis, sx, sd1, sd2, elementsCountAround, elementsCountAlongSegment, refPointZ, innerRadiusAlong, closedProximalEnd): """ Warps points in segment to account for bending and twisting along central path defined by nodes sx and derivatives sd1 and sd2. :param xList: coordinates of segment points. :param d1List: derivatives around axis of segment. :param d2List: derivatives along axis of segment. :param segmentAxis: axis perpendicular to segment plane. :param sx: coordinates of points on central path. :param sd1: derivatives of points along central path. :param sd2: derivatives representing cross axes. :param elementsCountAround: Number of elements around segment. :param elementsCountAlongSegment: Number of elements along segment. :param refPointZ: z-coordinate of reference point for each element groups along the segment to be used for transformation. :param innerRadiusAlong: radius of segment along length. :param closedProximalEnd: True if proximal end of segment is a closed end. :return coordinates and derivatives of warped points. """ xWarpedList = [] d1WarpedList = [] d2WarpedList = [] d2WarpedListFinal = [] d3WarpedUnitList = [] for nAlongSegment in range(elementsCountAlongSegment + 1): xElementAlongSegment = xList[elementsCountAround * nAlongSegment:elementsCountAround * (nAlongSegment + 1)] d1ElementAlongSegment = d1List[elementsCountAround * nAlongSegment:elementsCountAround * (nAlongSegment + 1)] d2ElementAlongSegment = d2List[elementsCountAround * nAlongSegment:elementsCountAround * (nAlongSegment + 1)] centroid = [0.0, 0.0, refPointZ[nAlongSegment]] # Rotate to align segment axis with tangent of central line unitTangent = vector.normalise(sd1[nAlongSegment]) cp = vector.crossproduct3(segmentAxis, unitTangent) dp = vector.dotproduct(segmentAxis, unitTangent) if vector.magnitude( cp) > 0.0: # path tangent not parallel to segment axis axisRot = vector.normalise(cp) thetaRot = math.acos(vector.dotproduct(segmentAxis, unitTangent)) rotFrame = matrix.getRotationMatrixFromAxisAngle(axisRot, thetaRot) centroidRot = [ rotFrame[j][0] * centroid[0] + rotFrame[j][1] * centroid[1] + rotFrame[j][2] * centroid[2] for j in range(3) ] else: # path tangent parallel to segment axis (z-axis) if dp == -1.0: # path tangent opposite direction to segment axis thetaRot = math.pi axisRot = [1.0, 0, 0] rotFrame = matrix.getRotationMatrixFromAxisAngle( axisRot, thetaRot) centroidRot = [ rotFrame[j][0] * centroid[0] + rotFrame[j][1] * centroid[1] + rotFrame[j][2] * centroid[2] for j in range(3) ] else: # segment axis in same direction as unit tangent rotFrame = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] centroidRot = centroid translateMatrix = [ sx[nAlongSegment][j] - centroidRot[j] for j in range(3) ] for n1 in range(elementsCountAround): x = xElementAlongSegment[n1] d1 = d1ElementAlongSegment[n1] d2 = d2ElementAlongSegment[n1] if vector.magnitude( cp) > 0.0: # path tangent not parallel to segment axis xRot1 = [ rotFrame[j][0] * x[0] + rotFrame[j][1] * x[1] + rotFrame[j][2] * x[2] for j in range(3) ] d1Rot1 = [ rotFrame[j][0] * d1[0] + rotFrame[j][1] * d1[1] + rotFrame[j][2] * d1[2] for j in range(3) ] d2Rot1 = [ rotFrame[j][0] * d2[0] + rotFrame[j][1] * d2[1] + rotFrame[j][2] * d2[2] for j in range(3) ] # xTranslate = [xRot1[j] + translateMatrix[j] for j in range(3)] else: # path tangent parallel to segment axis xRot1 = [ rotFrame[j][0] * x[0] + rotFrame[j][1] * x[1] + rotFrame[j][2] * x[2] for j in range(3) ] if dp == -1.0 else x d1Rot1 = [ rotFrame[j][0] * d1[0] + rotFrame[j][1] * d1[1] + rotFrame[j][2] * d1[2] for j in range(3) ] if dp == -1.0 else d1 d2Rot1 = [ rotFrame[j][0] * d2[0] + rotFrame[j][1] * d2[1] + rotFrame[j][2] * d2[2] for j in range(3) ] if dp == -1.0 else d2 # xTranslate = [xRot1[j] + translateMatrix[j] for j in range(3)] if n1 == 0: # Find angle between xCentroidRot and first node in the face vectorToFirstNode = [ xRot1[c] - centroidRot[c] for c in range(3) ] if vector.magnitude(vectorToFirstNode) > 0.0: cp = vector.crossproduct3( vector.normalise(vectorToFirstNode), vector.normalise(sd2[nAlongSegment])) if vector.magnitude(cp) > 1e-7: cp = vector.normalise(cp) signThetaRot2 = vector.dotproduct(unitTangent, cp) thetaRot2 = math.acos( vector.dotproduct( vector.normalise(vectorToFirstNode), sd2[nAlongSegment])) axisRot2 = unitTangent rotFrame2 = matrix.getRotationMatrixFromAxisAngle( axisRot2, signThetaRot2 * thetaRot2) else: rotFrame2 = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] else: rotFrame2 = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] xRot2 = [ rotFrame2[j][0] * xRot1[0] + rotFrame2[j][1] * xRot1[1] + rotFrame2[j][2] * xRot1[2] for j in range(3) ] d1Rot2 = [ rotFrame2[j][0] * d1Rot1[0] + rotFrame2[j][1] * d1Rot1[1] + rotFrame2[j][2] * d1Rot1[2] for j in range(3) ] d2Rot2 = [ rotFrame2[j][0] * d2Rot1[0] + rotFrame2[j][1] * d2Rot1[1] + rotFrame2[j][2] * d2Rot1[2] for j in range(3) ] xTranslate = [xRot2[j] + translateMatrix[j] for j in range(3)] xWarpedList.append(xTranslate) d1WarpedList.append(d1Rot2) d2WarpedList.append(d2Rot2) # Scale d2 with curvature of central path d2WarpedListScaled = [] vProjectedList = [] for nAlongSegment in range(elementsCountAlongSegment + 1): for n1 in range(elementsCountAround): n = nAlongSegment * elementsCountAround + n1 # Calculate norm sd1Normalised = vector.normalise(sd1[nAlongSegment]) v = [xWarpedList[n][c] - sx[nAlongSegment][c] for c in range(3)] dp = vector.dotproduct(v, sd1Normalised) dpScaled = [dp * c for c in sd1Normalised] vProjected = [v[c] - dpScaled[c] for c in range(3)] vProjectedList.append(vProjected) if vector.magnitude(vProjected) > 0.0: vProjectedNormlised = vector.normalise(vProjected) else: vProjectedNormlised = [0.0, 0.0, 0.0] # Calculate curvature along at each node if nAlongSegment == 0: curvature = interp.getCubicHermiteCurvature( sx[0], sd1[0], sx[1], sd1[1], vProjectedNormlised, 0.0) elif nAlongSegment == elementsCountAlongSegment: curvature = interp.getCubicHermiteCurvature( sx[-2], sd1[-2], sx[-1], sd1[-1], vProjectedNormlised, 1.0) else: curvature = 0.5 * (interp.getCubicHermiteCurvature( sx[nAlongSegment - 1], sd1[nAlongSegment - 1], sx[nAlongSegment], sd1[nAlongSegment], vProjectedNormlised, 1.0) + interp.getCubicHermiteCurvature( sx[nAlongSegment], sd1[nAlongSegment], sx[nAlongSegment + 1], sd1[nAlongSegment + 1], vProjectedNormlised, 0.0)) # Scale factor = 1.0 - curvature * innerRadiusAlong[nAlongSegment] d2 = [factor * c for c in d2WarpedList[n]] d2WarpedListScaled.append(d2) # Smooth d2 for segment smoothd2Raw = [] for n1 in range(elementsCountAround): nx = [] nd2 = [] for n2 in range(elementsCountAlongSegment + 1): n = n2 * elementsCountAround + n1 nx.append(xWarpedList[n]) nd2.append(d2WarpedListScaled[n]) smoothd2 = interp.smoothCubicHermiteDerivativesLine( nx, nd2, fixStartDerivative=True, fixEndDerivative=True) smoothd2Raw.append(smoothd2) # Re-arrange smoothd2 for n2 in range(elementsCountAlongSegment + 1): for n1 in range(elementsCountAround): d2WarpedListFinal.append(smoothd2Raw[n1][n2]) # Calculate unit d3 for n in range(len(xWarpedList)): d3Unit = vector.normalise( vector.crossproduct3(vector.normalise(d1WarpedList[n]), vector.normalise(d2WarpedListFinal[n]))) d3WarpedUnitList.append(d3Unit) return xWarpedList, d1WarpedList, d2WarpedListFinal, d3WarpedUnitList
def getPlaneProjectionOnCentralPath(x, elementsCountAround, elementsCountAlong, segmentLength, sx, sd1, sd2, sd12): """ Projects reference point used for warping onto the central path and find coordinates and derivatives at projected location. :param x: coordinates of nodes. :param elementsCountAround: number of elements around. :param elementsCountAlong: number of elements along. :param segmentLength: Length of segment. :param sx: coordinates of equally spaced points on central path. :param sd1: tangent of equally spaced points on central path. :param sd2: derivative representing cross axis at equally spaced points on central path. :param sd12: rate of change of cross axis at equally spaced points on central path. :return: coordinates and derivatives on project points and z-coordinates of reference points. """ # Use first node in each group of elements along as reference for warping later zRefList = [] for n2 in range(elementsCountAlong + 1): zFirstNodeAlong = x[n2 * elementsCountAround][2] zRefList.append(zFirstNodeAlong) # Find sx, sd1, sd2 at projection of reference points on central path lengthElementAlong = segmentLength / elementsCountAlong # Append values from first node on central path sxRefList = [] sd1RefList = [] sd2RefList = [] sxRefList.append(sx[0]) sd1RefList.append(sd1[0]) sd2RefList.append(sd2[0]) # Interpolate the ones in between for n2 in range(1, elementsCountAlong): ei = int(zRefList[n2] // lengthElementAlong + 1) xi = (zRefList[n2] - lengthElementAlong * (ei - 1)) / lengthElementAlong sxRef = interp.interpolateCubicHermite(sx[ei - 1], sd1[ei - 1], sx[ei], sd1[ei], xi) sd1Ref = interp.interpolateCubicHermiteDerivative( sx[ei - 1], sd1[ei - 1], sx[ei], sd1[ei], xi) sd2Ref = interp.interpolateCubicHermite(sd2[ei - 1], sd12[ei - 1], sd2[ei], sd12[ei], xi) sxRefList.append(sxRef) sd1RefList.append(sd1Ref) sd2RefList.append(sd2Ref) # Append values from last node on central path sxRefList.append(sx[-1]) sd1RefList.append(sd1[-1]) sd2RefList.append(sd2[-1]) # Project sd2 to plane orthogonal to sd1 sd2ProjectedListRef = [] for n in range(len(sd2RefList)): sd1Normalised = vector.normalise(sd1RefList[n]) dp = vector.dotproduct(sd2RefList[n], sd1Normalised) dpScaled = [dp * c for c in sd1Normalised] sd2Projected = vector.normalise( [sd2RefList[n][c] - dpScaled[c] for c in range(3)]) sd2ProjectedListRef.append(sd2Projected) return sxRefList, sd1RefList, sd2ProjectedListRef, zRefList
def warpSegmentPoints(xList, d1List, d2List, segmentAxis, segmentLength, sx, sd1, sd2, elementsCountAround, elementsCountAlongSegment, nSegment, faceMidPointZ): """ Warps points in segment to account for bending and twisting along central path defined by nodes sx and derivatives sd1 and sd2. :param xList: coordinates of segment points. :param d1List: derivatives around axis of segment. :param d2List: derivatives along axis of segment. :param segmentAxis: axis perpendicular to segment plane. :param sx: coordinates of points on central path. :param sd1: derivatives of points along central path. :param sd2: derivatives representing cross axes. :param elementsCountAround: Number of elements around segment. :param elementsCountAlongSegment: Number of elements along segment. :param nSegment: Segment index along central path. :param faceMidPointZ: z-coordinate of midpoint for each element groups along the segment. :return coordinates and derivatives of warped points. """ xWarpedList = [] d1WarpedList = [] d2WarpedList = [] smoothd2WarpedList = [] d3WarpedUnitList = [] for nAlongSegment in range(elementsCountAlongSegment + 1): n2 = elementsCountAlongSegment * nSegment + nAlongSegment xElementAlongSegment = xList[elementsCountAround * nAlongSegment:elementsCountAround * (nAlongSegment + 1)] d1ElementAlongSegment = d1List[elementsCountAround * nAlongSegment:elementsCountAround * (nAlongSegment + 1)] d2ElementAlongSegment = d2List[elementsCountAround * nAlongSegment:elementsCountAround * (nAlongSegment + 1)] xMid = [0.0, 0.0, faceMidPointZ[nAlongSegment]] # Rotate to align segment axis with tangent of central line unitTangent = vector.normalise(sd1[n2]) cp = vector.crossproduct3(segmentAxis, unitTangent) dp = vector.dotproduct(segmentAxis, unitTangent) if vector.magnitude( cp) > 0.0: # path tangent not parallel to segment axis axisRot = vector.normalise(cp) thetaRot = math.acos(vector.dotproduct(segmentAxis, unitTangent)) rotFrame = matrix.getRotationMatrixFromAxisAngle(axisRot, thetaRot) midRot = [ rotFrame[j][0] * xMid[0] + rotFrame[j][1] * xMid[1] + rotFrame[j][2] * xMid[2] for j in range(3) ] else: # path tangent parallel to segment axis (z-axis) if dp == -1.0: # path tangent opposite direction to segment axis thetaRot = math.pi axisRot = [1.0, 0, 0] rotFrame = matrix.getRotationMatrixFromAxisAngle( axisRot, thetaRot) midRot = [ rotFrame[j][0] * xMid[0] + rotFrame[j][1] * xMid[1] + rotFrame[j][2] * xMid[2] for j in range(3) ] else: # segment axis in same direction as unit tangent midRot = xMid translateMatrix = [sx[n2][j] - midRot[j] for j in range(3)] for n1 in range(elementsCountAround): x = xElementAlongSegment[n1] d1 = d1ElementAlongSegment[n1] d2 = d2ElementAlongSegment[n1] if vector.magnitude( cp) > 0.0: # path tangent not parallel to segment axis xRot1 = [ rotFrame[j][0] * x[0] + rotFrame[j][1] * x[1] + rotFrame[j][2] * x[2] for j in range(3) ] d1Rot1 = [ rotFrame[j][0] * d1[0] + rotFrame[j][1] * d1[1] + rotFrame[j][2] * d1[2] for j in range(3) ] d2Rot1 = [ rotFrame[j][0] * d2[0] + rotFrame[j][1] * d2[1] + rotFrame[j][2] * d2[2] for j in range(3) ] if n1 == 0: # Project sd2 onto plane normal to sd1 v = sd2[n2] pt = [midRot[j] + sd2[n2][j] for j in range(3)] dist = vector.dotproduct(v, unitTangent) ptOnPlane = [ pt[j] - dist * unitTangent[j] for j in range(3) ] newVector = [ptOnPlane[j] - midRot[j] for j in range(3)] # Rotate first point to align with planar projection of sd2 firstVector = vector.normalise( [xRot1[j] - midRot[j] for j in range(3)]) thetaRot2 = math.acos( vector.dotproduct(vector.normalise(newVector), firstVector)) cp2 = vector.crossproduct3(vector.normalise(newVector), firstVector) if vector.magnitude(cp2) > 0.0: cp2 = vector.normalise(cp2) signThetaRot2 = vector.dotproduct(unitTangent, cp2) axisRot2 = unitTangent rotFrame2 = matrix.getRotationMatrixFromAxisAngle( axisRot2, -signThetaRot2 * thetaRot2) else: rotFrame2 = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] else: # path tangent parallel to segment axis xRot1 = [ rotFrame[j][0] * x[0] + rotFrame[j][1] * x[1] + rotFrame[j][2] * x[2] for j in range(3) ] if dp == -1.0 else x d1Rot1 = [ rotFrame[j][0] * d1[0] + rotFrame[j][1] * d1[1] + rotFrame[j][2] * d1[2] for j in range(3) ] if dp == -1.0 else d1 d2Rot1 = [ rotFrame[j][0] * d2[0] + rotFrame[j][1] * d2[1] + rotFrame[j][2] * d2[2] for j in range(3) ] if dp == -1.0 else d2 # Rotate to align start of elementsAround with sd2 if n1 == 0: v = vector.normalise(sd2[n2]) startVector = vector.normalise( [xRot1[j] - midRot[j] for j in range(3)]) axisRot2 = unitTangent thetaRot2 = dp * -math.acos( vector.dotproduct(v, startVector)) rotFrame2 = matrix.getRotationMatrixFromAxisAngle( axisRot2, thetaRot2) xRot2 = [ rotFrame2[j][0] * xRot1[0] + rotFrame2[j][1] * xRot1[1] + rotFrame2[j][2] * xRot1[2] for j in range(3) ] d1Rot2 = [ rotFrame2[j][0] * d1Rot1[0] + rotFrame2[j][1] * d1Rot1[1] + rotFrame2[j][2] * d1Rot1[2] for j in range(3) ] d2Rot2 = [ rotFrame2[j][0] * d2Rot1[0] + rotFrame2[j][1] * d2Rot1[1] + rotFrame2[j][2] * d2Rot1[2] for j in range(3) ] xTranslate = [xRot2[j] + translateMatrix[j] for j in range(3)] xWarpedList.append(xTranslate) d1WarpedList.append(d1Rot2) d2WarpedList.append(d2Rot2) # Smooth d2 for segment smoothd2Raw = [] for n1 in range(elementsCountAround): nx = [] nd2 = [] for n2 in range(elementsCountAlongSegment + 1): n = n2 * elementsCountAround + n1 nx.append(xWarpedList[n]) nd2.append(d2WarpedList[n]) smoothd2 = interp.smoothCubicHermiteDerivativesLine( nx, nd2, fixStartDerivative=True, fixEndDerivative=True) smoothd2Raw.append(smoothd2) # Re-arrange smoothd2 for n2 in range(elementsCountAlongSegment + 1): for n1 in range(elementsCountAround): smoothd2WarpedList.append(smoothd2Raw[n1][n2]) # Calculate unit d3 for n in range(len(xWarpedList)): d3Unit = vector.normalise( vector.crossproduct3(vector.normalise(d1WarpedList[n]), vector.normalise(smoothd2WarpedList[n]))) d3WarpedUnitList.append(d3Unit) return xWarpedList, d1WarpedList, smoothd2WarpedList, d3WarpedUnitList
def createAnnulusMesh3d(nodes, mesh, nextNodeIdentifier, nextElementIdentifier, startPointsx, startPointsd1, startPointsd2, startPointsd3, startNodeId, startDerivativesMap, endPointsx, endPointsd1, endPointsd2, endPointsd3, endNodeId, endDerivativesMap, forceStartLinearXi3 = False, forceMidLinearXi3 = False, forceEndLinearXi3 = False, maxStartThickness = None, maxEndThickness = None, useCrossDerivatives = False, elementsCountRadial = 1, meshGroups = None, tracksurface = None, startProportions = None, endProportions = None): """ Create an annulus mesh from a loop of start points/nodes with specified derivative mappings to a loop of end points/nodes with specified derivative mappings. Derivative d3 is through the wall. Currently limited to single element layer through wall. Points/nodes order cycles fastest around the annulus, then through the wall. Note doesn't support cross derivatives. Arrays are indexed by n3 (node through wall, size 2), n2 (node along/radial), n1 (node around, variable size) and coordinate component c. :param nodes: The nodeset to create nodes in. :param mesh: The mesh to create elements in. :param nextNodeIdentifier, nextElementIdentifier: Next identifiers to use and increment. :param startPointsx, startPointsd1, startPointsd2, startPointsd3, endPointsx, endPointsd1, endPointsd2, endPointsd3: List array[n3][n1][c] or start/point coordinates and derivatives. To linearise through the wall, pass None to d3. If both ends are linear through the wall, interior points are linear through the wall. :param startNodeId, endNodeId: List array [n3][n1] of existing node identifiers to use at start/end. Pass None for argument if no nodes are specified at end. These arguments are 'all or nothing'. :param startDerivativesMap, endDerivativesMap: List array[n3][n1] of mappings for d/dxi1, d/dxi2, d/dxi3 at start/end of form: ( (1, -1, 0), (1, 0, 0), None ) where the first tuple means d/dxi1 = d/ds1 - d/ds2. Only 0, 1 and -1 may be used. None means use default e.g. d/dxi2 = d/ds2. Pass None for the entire argument to use the defaults d/dxi1 = d/ds1, d/dxi2 = d/ds2, d/dxi3 = d/ds3. Pass a 4th mapping to apply to d/dxi1 on other side of node; if not supplied first mapping applies both sides. :param nodetemplate: Full tricubic Hermite node template, can omit cross derivatives. :param forceStartLinearXi3, forceMidLinearXi3, forceEndLinearXi3: Force start, middle or end elements to be linear through the wall, even if d3 is supplied at either end. Can only use forceMidLinearXi3 only if at least one end is linear in d3. :param maxStartThickness, maxEndThickness: Optional maximum override on start/end thicknesses. :param useCrossDerivatives: May only be True if no derivatives maps are in use. :param elementsCountRadial: Optional number of elements in radial direction between start and end. :param meshGroups: Optional sequence of Zinc MeshGroup for adding all new elements to, or a sequence of length elementsCountRadial containing sequences of mesh groups to add rows of radial elements to from start to end. :param tracksurface: Description for outer surface representation used for creating annulus mesh. Provides information for creating radial nodes on annulus that sit on tracksurface. Need startProportions and endProportions to work. :param startProportions: Proportion around and along of startPoints on tracksurface. These vary with nodes around as for startPoints. Values only given for tracksurface for outer layer (xi3 == 1). :param endProportions: Proportion around and along of endPoints on track surface. These vary with nodes around as for endPoints. Values only given for tracksurface for outer layer (xi3 == 1). :return: Final values of nextNodeIdentifier, nextElementIdentifier """ assert (elementsCountRadial >= 1), 'createAnnulusMesh3d: Invalid number of radial elements' startLinearXi3 = (not startPointsd3) or forceStartLinearXi3 endLinearXi3 = (not endPointsd3) or forceEndLinearXi3 midLinearXi3 = (startLinearXi3 and endLinearXi3) or ((startLinearXi3 or endLinearXi3) and forceMidLinearXi3) # get list whether each row of nodes in elements is linear in Xi3 # this is for element use; start/end nodes may have d3 even if element is linear rowLinearXi3 = [ startLinearXi3 ] + [ midLinearXi3 ]*(elementsCountRadial - 1) + [ endLinearXi3 ] assert (not useCrossDerivatives) or ((not startDerivativesMap) and (not endDerivativesMap)), \ 'createAnnulusMesh3d: Cannot use cross derivatives with derivatives map' elementsCountWall = 1 nodesCountWall = elementsCountWall + 1 assert (len(startPointsx) == nodesCountWall) and (len(startPointsd1) == nodesCountWall) and (len(startPointsd2) == nodesCountWall) and \ (startLinearXi3 or (len(startPointsd3) == nodesCountWall)) and \ (len(endPointsx) == nodesCountWall) and (len(endPointsd1) == nodesCountWall) and (len(endPointsd2) == nodesCountWall) and \ (endLinearXi3 or (len(endPointsd3) == nodesCountWall)) and \ ((startNodeId is None) or (len(startNodeId) == nodesCountWall)) and \ ((endNodeId is None) or (len(endNodeId) == nodesCountWall)) and \ ((startDerivativesMap is None) or (len(startDerivativesMap) == nodesCountWall)) and \ ((endDerivativesMap is None) or (len(endDerivativesMap) == nodesCountWall)), \ 'createAnnulusMesh3d: Mismatch in number of layers through wall' elementsCountAround = nodesCountAround = len(startPointsx[0]) assert (nodesCountAround > 1), 'createAnnulusMesh3d: Invalid number of points/nodes around annulus' for n3 in range(nodesCountWall): assert (len(startPointsx[n3]) == nodesCountAround) and (len(startPointsd1[n3]) == nodesCountAround) and (len(startPointsd2[n3]) == nodesCountAround) and \ (startLinearXi3 or (len(startPointsd3[n3]) == nodesCountAround)) and \ (len(endPointsx[n3]) == nodesCountAround) and (len(endPointsd1[n3]) == nodesCountAround) and (len(endPointsd2[n3]) == nodesCountAround) and \ (endLinearXi3 or (len(endPointsd3[n3]) == nodesCountAround)) and \ ((startNodeId is None) or (len(startNodeId[n3]) == nodesCountAround)) and \ ((endNodeId is None) or (len(endNodeId[n3]) == nodesCountAround)) and \ ((startDerivativesMap is None) or (len(startDerivativesMap[n3]) == nodesCountAround)) and \ ((endDerivativesMap is None) or (len(endDerivativesMap[n3]) == nodesCountAround)), \ 'createAnnulusMesh3d: Mismatch in number of points/nodes in layers through wall' rowMeshGroups = meshGroups if meshGroups: assert isinstance(meshGroups, collections.Sequence), 'createAnnulusMesh3d: Mesh groups is not a sequence' if (len(meshGroups) == 0) or (not isinstance(meshGroups[0], collections.Sequence)): rowMeshGroups = [ meshGroups ]*elementsCountRadial else: assert len(meshGroups) == elementsCountRadial, 'createAnnulusMesh3d: Length of meshGroups sequence does not equal elementsCountRadial' if tracksurface: assert startProportions and endProportions, 'createAnnulusMesh3d: Missing start and/or end proportions for use with tracksurface' assert len(startProportions) == nodesCountAround, 'createAnnulusMesh3d: Length of startProportions does not equal nodesCountAround' assert len(endProportions) == nodesCountAround, 'createAnnulusMesh3d: Length of endProportions does not equal nodesCountAround' fm = mesh.getFieldmodule() fm.beginChange() cache = fm.createFieldcache() coordinates = findOrCreateFieldCoordinates(fm) # Build arrays of points from start to end px = [ [], [] ] pd1 = [ [], [] ] pd2 = [ [], [] ] pd3 = [ [], [] ] for n3 in range(2): px [n3] = [ startPointsx [n3], endPointsx [n3] ] pd1[n3] = [ startPointsd1[n3], endPointsd1[n3] ] pd2[n3] = [ startPointsd2[n3], endPointsd2[n3] ] pd3[n3] = [ startPointsd3[n3] if (startPointsd3 is not None) else None, \ endPointsd3[n3] if (endPointsd3 is not None) else None ] if elementsCountRadial > 1: # add in-between points startPointsd = [ startPointsd1, startPointsd2, startPointsd3 ] startPointsdslimit = 2 if (startPointsd3 is None) else 3 endPointsd = [ endPointsd1, endPointsd2, endPointsd3 ] endPointsdslimit = 2 if (endPointsd3 is None) else 3 for n3 in range(2): for n2 in range(1, elementsCountRadial): px [n3].insert(n2, [ None ]*nodesCountAround) pd1[n3].insert(n2, [ None ]*nodesCountAround) pd2[n3].insert(n2, [ None ]*nodesCountAround) pd3[n3].insert(n2, None if midLinearXi3 else [ None ]*nodesCountAround) # compute on outside / n3 = 1, then map to inside using thickness thicknesses = [] thicknesses.append([ vector.magnitude([ (startPointsx[1][n1][c] - startPointsx[0][n1][c]) for c in range(3) ]) for n1 in range(nodesCountAround) ]) if maxStartThickness: for n1 in range(nodesCountAround): thicknesses[0][n1] = min(thicknesses[0][n1], maxStartThickness) for n2 in range(1, elementsCountRadial): thicknesses.append([ None ]*nodesCountAround) thicknesses.append([ vector.magnitude([ (endPointsx[1][n1][c] - endPointsx[0][n1][c]) for c in range(3) ]) for n1 in range(nodesCountAround) ]) if maxEndThickness: for n1 in range(nodesCountAround): thicknesses[-1][n1] = min(thicknesses[-1][n1], maxEndThickness) n3 == 1 for n1 in range(nodesCountAround): ax = startPointsx [n3][n1] if (startDerivativesMap is None) or (startDerivativesMap[n3][n1][0] is None): ad1 = startPointsd1[n3][n1] else: derivativesMap = startDerivativesMap[n3][n1][0] ad1 = [ 0.0, 0.0, 0.0 ] for ds in range(startPointsdslimit): if derivativesMap[ds] != 0.0: for c in range(3): ad1[c] += derivativesMap[ds]*startPointsd[ds][n3][n1][c] if len(startDerivativesMap[n3][n1]) > 3: # average with d1 map for other side derivativesMap = startDerivativesMap[n3][n1][3] ad1 = [ 0.5*d for d in ad1 ] if not derivativesMap: for c in range(3): ad1[c] += 0.5*startPointsd[0][n3][n1][c] else: for ds in range(startPointsdslimit): if derivativesMap[ds] != 0.0: for c in range(3): ad1[c] += 0.5*derivativesMap[ds]*startPointsd[ds][n3][n1][c] if (startDerivativesMap is None) or (startDerivativesMap[n3][n1][1] is None): ad2 = startPointsd2[n3][n1] else: derivativesMap = startDerivativesMap[n3][n1][1] ad2 = [ 0.0, 0.0, 0.0 ] for ds in range(startPointsdslimit): if derivativesMap[ds] != 0.0: for c in range(3): ad2[c] += derivativesMap[ds]*startPointsd[ds][n3][n1][c] bx = endPointsx [n3][n1] if (endDerivativesMap is None) or (endDerivativesMap[n3][n1][0] is None): bd1 = endPointsd1[n3][n1] else: derivativesMap = endDerivativesMap[n3][n1][0] bd1 = [ 0.0, 0.0, 0.0 ] for ds in range(endPointsdslimit): if derivativesMap[ds] != 0.0: for c in range(3): bd1[c] += derivativesMap[ds]*endPointsd[ds][n3][n1][c] if len(endDerivativesMap[n3][n1]) > 3: # average with d1 map for other side derivativesMap = endDerivativesMap[n3][n1][3] bd1 = [ 0.5*d for d in bd1 ] if not derivativesMap: for c in range(3): bd1[c] += 0.5*endPointsd[0][n3][n1][c] else: for ds in range(endPointsdslimit): if derivativesMap[ds] != 0.0: for c in range(3): bd1[c] += 0.5*derivativesMap[ds]*endPointsd[ds][n3][n1][c] if (endDerivativesMap is None) or (endDerivativesMap[n3][n1][1] is None): bd2 = endPointsd2[n3][n1] else: derivativesMap = endDerivativesMap[n3][n1][1] bd2 = [ 0.0, 0.0, 0.0 ] for ds in range(endPointsdslimit): if derivativesMap[ds] != 0.0: for c in range(3): bd2[c] += derivativesMap[ds]*endPointsd[ds][n3][n1][c] # scaling end derivatives to arc length gives even curvature along the curve if tracksurface: mx, md2, md1 = \ tracksurface.createHermiteCurvePoints(startProportions[n1][0], startProportions[n1][1], endProportions[n1][0], endProportions[n1][1], elementsCountRadial, derivativeStart=ad2, derivativeEnd=bd2)[0:3] else: arcLength = interp.computeCubicHermiteArcLength(ax, ad2, bx, bd2, rescaleDerivatives = False) scaledDerivatives = [ vector.setMagnitude(d2, arcLength) for d2 in [ ad2, bd2 ]] mx, md2, me, mxi = interp.sampleCubicHermiteCurvesSmooth([ ax, bx ], scaledDerivatives, elementsCountRadial, derivativeMagnitudeStart = vector.magnitude(ad2), derivativeMagnitudeEnd = vector.magnitude(bd2))[0:4] md1 = interp.interpolateSampleLinear([ ad1, bd1 ], me, mxi) thi = interp.interpolateSampleLinear([ thicknesses[0][n1], thicknesses[-1][n1] ], me, mxi) #md2 = interp.smoothCubicHermiteDerivativesLine(mx, md2, fixStartDerivative = True, fixEndDerivative = True) for n2 in range(1, elementsCountRadial): px [n3][n2][n1] = mx [n2] pd1[n3][n2][n1] = md1[n2] pd2[n3][n2][n1] = md2[n2] if not tracksurface: thicknesses[n2][n1] = thi[n2] # Interpolate thicknesses using xi calculated using arclength distances between points if tracksurface: for n1 in range(nodesCountAround): arclengthInsideToOutside = 0.0 arcLengthInsideToRadialPoint = [] for n2 in range(elementsCountRadial): arclengthInsideToOutside += interp.computeCubicHermiteArcLength(px[n3][n2][n1], pd2[n3][n2][n1], px[n3][n2+1][n1], pd2[n3][n2+1][n1], False) if n2 < elementsCountRadial: arcLengthInsideToRadialPoint.append(arclengthInsideToOutside) for n2 in range(elementsCountRadial - 1): xi = arcLengthInsideToRadialPoint[n2]/arclengthInsideToOutside thicknesses[n2+1][n1] = thicknesses[0][n1]*xi + thicknesses[-1][n1]*(1-xi) # now get inner positions from normal and thickness, derivatives from curvature for n2 in range(1, elementsCountRadial): # first smooth derivative 1 around outer loop pd1[1][n2] = interp.smoothCubicHermiteDerivativesLoop(px[1][n2], pd1[1][n2], magnitudeScalingMode = interp.DerivativeScalingMode.HARMONIC_MEAN) for n1 in range(nodesCountAround): normal = vector.normalise(vector.crossproduct3(pd1[1][n2][n1], pd2[1][n2][n1])) thickness = thicknesses[n2][n1] d3 = [ d*thickness for d in normal ] px [0][n2][n1] = [ (px [1][n2][n1][c] - d3[c]) for c in range(3) ] # calculate inner d1 from curvature around n1m = n1 - 1 n1p = (n1 + 1)%nodesCountAround curvature = 0.5*( interp.getCubicHermiteCurvature(px[1][n2][n1m], pd1[1][n2][n1m], px[1][n2][n1 ], pd1[1][n2][n1 ], normal, 1.0) + interp.getCubicHermiteCurvature(px[1][n2][n1 ], pd1[1][n2][n1 ], px[1][n2][n1p], pd1[1][n2][n1p], normal, 0.0)) factor = 1.0 + curvature*thickness pd1[0][n2][n1] = [ factor*d for d in pd1[1][n2][n1] ] # calculate inner d2 from curvature radially n2m = n2 - 1 n2p = n2 + 1 curvature = 0.5*( interp.getCubicHermiteCurvature(px[1][n2m][n1], pd2[1][n2m][n1], px[1][n2 ][n1], pd2[1][n2 ][n1], normal, 1.0) + interp.getCubicHermiteCurvature(px[1][n2 ][n1], pd2[1][n2 ][n1], px[1][n2p][n1], pd2[1][n2p][n1], normal, 0.0)) factor = 1.0 + curvature*thickness pd2[0][n2][n1] = [ factor*d for d in pd2[1][n2][n1] ] d2Scaled = [factor*d for d in pd2[1][n2][n1]] if vector.dotproduct(vector.normalise(pd2[1][n2][n1]), vector.normalise(d2Scaled)) == -1: pd2[0][n2][n1] = [-factor * d for d in pd2[1][n2][n1]] if not midLinearXi3: pd3[0][n2][n1] = pd3[1][n2][n1] = d3 # smooth derivative 1 around inner loop pd1[0][n2] = interp.smoothCubicHermiteDerivativesLoop(px[0][n2], pd1[0][n2], magnitudeScalingMode = interp.DerivativeScalingMode.HARMONIC_MEAN) for n3 in range(0, 1): # was (0, nodesCountWall) # smooth derivative 2 radially/along annulus for n1 in range(nodesCountAround): sd2 = interp.smoothCubicHermiteDerivativesLine( [ px [n3][n2][n1] for n2 in range(elementsCountRadial + 1) ], [ pd2[n3][n2][n1] for n2 in range(elementsCountRadial + 1) ], fixAllDirections = True, fixStartDerivative = True, fixEndDerivative = True, magnitudeScalingMode = interp.DerivativeScalingMode.HARMONIC_MEAN) for n2 in range(elementsCountRadial + 1): pd2[n3][n2][n1] = sd2[n2] ############## # Create nodes ############## nodetemplate = nodes.createNodetemplate() nodetemplate.defineField(coordinates) nodetemplate.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_VALUE, 1) nodetemplate.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_D_DS1, 1) nodetemplate.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_D_DS2, 1) if useCrossDerivatives: nodetemplate.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_D2_DS1DS2, 1) nodetemplate.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_D_DS3, 1) if useCrossDerivatives: nodetemplate.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_D2_DS1DS3, 1) nodetemplate.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_D2_DS2DS3, 1) nodetemplate.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_D3_DS1DS2DS3, 1) nodetemplateLinearS3 = nodes.createNodetemplate() nodetemplateLinearS3.defineField(coordinates) nodetemplateLinearS3.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_VALUE, 1) nodetemplateLinearS3.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_D_DS1, 1) nodetemplateLinearS3.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_D_DS2, 1) if useCrossDerivatives: nodetemplateLinearS3.setValueNumberOfVersions(coordinates, -1, Node.VALUE_LABEL_D2_DS1DS2, 1) nodeIdentifier = nextNodeIdentifier nodeId = [ [], [] ] for n3 in range(2): for n2 in range(elementsCountRadial + 1): if (n2 == 0) and (startNodeId is not None): rowNodeId = copy.deepcopy(startNodeId[n3]) elif (n2 == elementsCountRadial) and (endNodeId is not None): rowNodeId = copy.deepcopy(endNodeId[n3]) else: rowNodeId = [] nodetemplate1 = nodetemplate if pd3[n3][n2] else nodetemplateLinearS3 for n1 in range(nodesCountAround): node = nodes.createNode(nodeIdentifier, nodetemplate1) rowNodeId.append(nodeIdentifier) cache.setNode(node) coordinates.setNodeParameters(cache, -1, Node.VALUE_LABEL_VALUE, 1, px[n3][n2][n1]) coordinates.setNodeParameters(cache, -1, Node.VALUE_LABEL_D_DS1, 1, pd1[n3][n2][n1]) coordinates.setNodeParameters(cache, -1, Node.VALUE_LABEL_D_DS2, 1, pd2[n3][n2][n1]) if pd3[n3][n2]: coordinates.setNodeParameters(cache, -1, Node.VALUE_LABEL_D_DS3, 1, pd3[n3][n2][n1]) nodeIdentifier = nodeIdentifier + 1 nodeId[n3].append(rowNodeId) ################# # Create elements ################# tricubichermite = eftfactory_tricubichermite(mesh, useCrossDerivatives) bicubichermitelinear = eftfactory_bicubichermitelinear(mesh, useCrossDerivatives) elementIdentifier = nextElementIdentifier elementtemplateStandard = mesh.createElementtemplate() elementtemplateStandard.setElementShapeType(Element.SHAPE_TYPE_CUBE) elementtemplateX = mesh.createElementtemplate() elementtemplateX.setElementShapeType(Element.SHAPE_TYPE_CUBE) for e2 in range(elementsCountRadial): nonlinearXi3 = (not rowLinearXi3[e2]) or (not rowLinearXi3[e2 + 1]) eftFactory = tricubichermite if nonlinearXi3 else bicubichermitelinear eftStandard = eftFactory.createEftBasic() elementtemplateStandard.defineField(coordinates, -1, eftStandard) mapStartDerivatives = (e2 == 0) and (startDerivativesMap is not None) mapStartLinearDerivativeXi3 = nonlinearXi3 and rowLinearXi3[e2] mapEndDerivatives = (e2 == (elementsCountRadial - 1)) and (endDerivativesMap is not None) mapEndLinearDerivativeXi3 = nonlinearXi3 and rowLinearXi3[e2 + 1] mapDerivatives = mapStartDerivatives or mapStartLinearDerivativeXi3 or mapEndDerivatives or mapEndLinearDerivativeXi3 for e1 in range(elementsCountAround): en = (e1 + 1)%elementsCountAround nids = [ nodeId[0][e2][e1], nodeId[0][e2][en], nodeId[0][e2 + 1][e1], nodeId[0][e2 + 1][en], nodeId[1][e2][e1], nodeId[1][e2][en], nodeId[1][e2 + 1][e1], nodeId[1][e2 + 1][en] ] if mapDerivatives: eft1 = eftFactory.createEftNoCrossDerivatives() setEftScaleFactorIds(eft1, [1], []) if mapStartLinearDerivativeXi3: eftFactory.setEftLinearDerivative2(eft1, [ 1, 5, 2, 6 ], Node.VALUE_LABEL_D_DS3, [ Node.VALUE_LABEL_D2_DS1DS3 ]) if mapStartDerivatives: for i in range(2): lns = [ 1, 5 ] if (i == 0) else [ 2, 6 ] for n3 in range(2): derivativesMap = startDerivativesMap[n3][e1] if (i == 0) else startDerivativesMap[n3][en] # handle different d1 on each side of node d1Map = derivativesMap[0] if ((i == 1) or (len(derivativesMap) < 4)) else derivativesMap[3] d2Map = derivativesMap[1] d3Map = derivativesMap[2] # use temporary to safely swap DS1 and DS2: ln = [ lns[n3] ] if d1Map is not None: remapEftNodeValueLabel(eft1, ln, Node.VALUE_LABEL_D_DS1, [ ( Node.VALUE_LABEL_D2_DS1DS2, [] ) ]) if d3Map is not None: remapEftNodeValueLabel(eft1, ln, Node.VALUE_LABEL_D_DS3, [ ( Node.VALUE_LABEL_D2_DS2DS3, [] ) ]) if d2Map is not None: remapEftNodeValueLabel(eft1, ln, Node.VALUE_LABEL_D_DS2, \ derivativeSignsToExpressionTerms( ( Node.VALUE_LABEL_D_DS1, Node.VALUE_LABEL_D_DS2, Node.VALUE_LABEL_D_DS3 ), d2Map)) if d1Map is not None: remapEftNodeValueLabel(eft1, ln, Node.VALUE_LABEL_D2_DS1DS2, \ derivativeSignsToExpressionTerms( ( Node.VALUE_LABEL_D_DS1, Node.VALUE_LABEL_D_DS2, Node.VALUE_LABEL_D_DS3 ), d1Map)) if d3Map is not None: remapEftNodeValueLabel(eft1, ln, Node.VALUE_LABEL_D2_DS2DS3, \ derivativeSignsToExpressionTerms( ( Node.VALUE_LABEL_D_DS1, Node.VALUE_LABEL_D_DS2, Node.VALUE_LABEL_D_DS3 ), d3Map)) if mapEndLinearDerivativeXi3: eftFactory.setEftLinearDerivative2(eft1, [ 3, 7, 4, 8 ], Node.VALUE_LABEL_D_DS3, [ Node.VALUE_LABEL_D2_DS1DS3 ]) if mapEndDerivatives: for i in range(2): lns = [ 3, 7 ] if (i == 0) else [ 4, 8 ] for n3 in range(2): derivativesMap = endDerivativesMap[n3][e1] if (i == 0) else endDerivativesMap[n3][en] # handle different d1 on each side of node d1Map = derivativesMap[0] if ((i == 1) or (len(derivativesMap) < 4)) else derivativesMap[3] d2Map = derivativesMap[1] d3Map = derivativesMap[2] # use temporary to safely swap DS1 and DS2: ln = [ lns[n3] ] if d1Map is not None: remapEftNodeValueLabel(eft1, ln, Node.VALUE_LABEL_D_DS1, [ ( Node.VALUE_LABEL_D2_DS1DS2, [] ) ]) if d3Map is not None: remapEftNodeValueLabel(eft1, ln, Node.VALUE_LABEL_D_DS3, [ ( Node.VALUE_LABEL_D2_DS2DS3, [] ) ]) if d2Map is not None: remapEftNodeValueLabel(eft1, ln, Node.VALUE_LABEL_D_DS2, \ derivativeSignsToExpressionTerms( ( Node.VALUE_LABEL_D_DS1, Node.VALUE_LABEL_D_DS2, Node.VALUE_LABEL_D_DS3 ), d2Map)) if d1Map is not None: remapEftNodeValueLabel(eft1, ln, Node.VALUE_LABEL_D2_DS1DS2, \ derivativeSignsToExpressionTerms( ( Node.VALUE_LABEL_D_DS1, Node.VALUE_LABEL_D_DS2, Node.VALUE_LABEL_D_DS3 ), d1Map)) if d3Map is not None: remapEftNodeValueLabel(eft1, ln, Node.VALUE_LABEL_D2_DS2DS3, \ derivativeSignsToExpressionTerms( ( Node.VALUE_LABEL_D_DS1, Node.VALUE_LABEL_D_DS2, Node.VALUE_LABEL_D_DS3 ), d3Map)) elementtemplateX.defineField(coordinates, -1, eft1) elementtemplate1 = elementtemplateX else: eft1 = eftStandard elementtemplate1 = elementtemplateStandard element = mesh.createElement(elementIdentifier, elementtemplate1) result2 = element.setNodesByIdentifier(eft1, nids) if mapDerivatives: result3 = element.setScaleFactors(eft1, [ -1.0 ]) #else: # result3 = '-' #print('create element annulus', element.isValid(), elementIdentifier, result2, result3, nids) elementIdentifier += 1 if rowMeshGroups: for meshGroup in rowMeshGroups[e2]: meshGroup.addElement(element) fm.endChange() return nodeIdentifier, elementIdentifier