'''
-----------------------------------------------------------------------------
 Two dimensional submodel symmetric double edged notch specimen (elastic
 plastic material) modeled using hybrid reduced integration plane stress
 elements (CPE8RH).

 First the global model job is completed. The *.odb file from the global
 model is used to drive this submodel.

 Global model scripts to be run:
 2DDoubleEdPlasGlCPE8RH_model.py and 2DDoubleEdPlasGlCPE8RH_job.py
-----------------------------------------------------------------------------
'''

from abaqus import *
import testUtils
testUtils.setBackwardCompatibility()
from abaqusConstants import *

import part, material, section, assembly, step, interaction
import regionToolset, displayGroupMdbToolset as dgm, mesh, load, job 

#----------------------------------------------------------------------------

# Copy the global model into a new model

Mdb()
globalModelName = '2DDoubleEdPlasGlCPE8RH'
openMdb(globalModelName)
subModelName = '2DDoubleEdPlasSubCPE8RH'
mySubModel = mdb.Model(name=subModelName)

mySubModel = (mdb.Model(name=subModelName,
    objectToCopy=mdb.models[globalModelName]))
    
# Create a new viewport in which to display the model
# and the results of the analysis.

myAssembly = mySubModel.rootAssembly
myViewport = session.viewports['Viewport: 1']
myViewport.setValues(displayedObject=myAssembly)
myViewport.makeCurrent()
myViewport.maximize()

#---------------------------------------------------------------------------

# Edit the model attributes to refer to the global model ODB file
# to drive the sub model.

mySubModel.setValues(description='Double edged notch specimen submodel', 
    globalJob='2DDoubleEdPlasGlCPE8RH.odb')

#---------------------------------------------------------------------------

# Create the sub model section of the global model

# Create a sketch for the base feature

myPlate = mySubModel.parts['Plate']
myViewport.setValues(displayedObject=myPlate)

mySubSketch = mySubModel.Sketch(name='plateProfile',sheetSize=200.0)
mySubSketch.sketchOptions.setValues(viewStyle=REGULAR)
mySubSketch.setPrimaryObject(option=STANDALONE)

mySubSketch.rectangle(point1=(0.0,0.0), point2=(20.0,10.0))
mySubPlate = mySubModel.Part(name='subPlate', 
    dimensionality=TWO_D_PLANAR, type=DEFORMABLE_BODY)
mySubPlate.BaseShell(sketch=mySubSketch)
mySubSketch.unsetPrimaryObject()
del mySubModel.sketches['plateProfile']

myViewport.setValues(displayedObject=mySubPlate)

# Create a set referring to the whole part

faces = mySubPlate.faces.findAt(((10,2.5,0),))
mySubPlate.Set(faces=faces, name='subAll')

#---------------------------------------------------------------------------

# Assign material properties

del mySubModel.materials['ElasticMaterial']
mySubModel.Material(name='A1')
mySubModel.materials['A1'].Elastic(table=((30000000.0, 0.3), ))
mySubModel.materials['A1'].Plastic(table=((40000.0,0.0), (60000.0,0.0085)))

mySubModel.sections['SolidHomogeneous'].setValues(material='A1',
    thickness=1.0)

region = mySubPlate.sets['subAll']
mySubPlate.SectionAssignment(region=region,
    sectionName='SolidHomogeneous')

#---------------------------------------------------------------------------

# Create an assembly
# Place the plate created above at the same position as in the
# global model. Then the instance of the full plate is deleted.

myViewport.setValues(displayedObject=myAssembly)
myAssembly.Instance(name='subPlate-1', part=mySubPlate, dependent=OFF)
mySubPlateInstance = myAssembly.instances['subPlate-1']
del myAssembly.features['myPlate-1']

# Partition the edge at t=0.5

pickedEdges = mySubPlateInstance.edges.findAt((9,0,0))
myAssembly.PartitionEdgeByParam(edges=pickedEdges, parameter=0.5)

# Partition the instance for sweep meshing

face1 = mySubPlateInstance.faces.findAt((10,5,0))
t = myAssembly.MakeSketchTransform(sketchPlane=face1,
    sketchPlaneSide=SIDE1, origin=(10.0, 5.0, 0.0))
mySketch = mySubModel.Sketch(name='partitionProfile', 
    sheetSize=44.72, gridSpacing=1.11, transform=t)
mySketch.setPrimaryObject(option=SUPERIMPOSE)
myAssembly.projectReferencesOntoSketch(sketch=mySketch,
    filter=COPLANAR_EDGES)
r1 = mySketch.referenceVertices
mySketch.sketchOptions.setValues(gridOrigin=(0.0,-5.0))
mySketch.ArcByCenterEnds(center=(0.0,-5.0), point1=(5.0,-5.0),
    point2=(-5.0,-5.0), direction=COUNTERCLOCKWISE)
mySketch.Line(point1=(0.0,-5.0), point2=(10.0,5.0))
mySketch.Line(point1=(0.0,-5.0), point2=(-10.0,5.0))
mySketch.constraintReferences(vertex1=r1.findAt((0.0,-5.0),1))
faces = mySubPlateInstance.faces
pickedFaces = faces[face1.index:(face1.index+1)]
myAssembly.PartitionFaceBySketch(faces=pickedFaces,
    sketch=mySketch)
mySketch.unsetPrimaryObject()
del mySubModel.sketches['partitionProfile']

# Delete all the sets/BCs/Loads/step/crack properties
# created for the global model

del mySubModel.steps['ApplyLoad']
#del myAssembly.sets['Monitor']
del myAssembly.sets['crackTip']
del myAssembly.sets['xEdge']
del myAssembly.sets['yEdge']
del myAssembly.surfaces['topSurf']
del myAssembly.engineeringFeatures.cracks['Crack']
del mySubModel.boundaryConditions['xFixed']
del mySubModel.boundaryConditions['yFixed']

# Create a set for the crack tip

verts = mySubPlateInstance.vertices
v1 = mySubPlateInstance.vertices.findAt((10,0,0))
verts1 = verts[v1.index:(v1.index+1)]
myAssembly.Set(vertices=verts1, name='subCrackTip')

# Create a set for the X edge of the plate

edges = mySubPlateInstance.edges
e1 = mySubPlateInstance.edges.findAt(((2.5,0,0),),
    ((7.5,0.,0.),),)
myAssembly.Set(edges=e1, name='subXEdge')

# Create a set for the Y edge of the plate

edges = mySubPlateInstance.edges
e1 = mySubPlateInstance.edges.findAt(((0,5,0),),)
myAssembly.Set(edges=e1, name='subYEdge')

# Create a set for the sub modeling boundary

e1 = mySubPlateInstance.edges.findAt((10,10,0))
e1 = edges[e1.index:(e1.index+1)]
myAssembly.Set(edges=e1, name='drivenBoundary')

#---------------------------------------------------------------------------

# Create a step

mySubModel.StaticStep(name='subApplyLoad', 
    previous='Initial', description='Drive the submodel')

#---------------------------------------------------------------------------

# Create interaction properties

# Create the contour integral definition for the crack

crackFront = crackTip = myAssembly.sets['subCrackTip']
verts = mySubPlateInstance.vertices
v1 = mySubPlateInstance.vertices.findAt((10,0,0))
v2 = mySubPlateInstance.vertices.findAt((0,0,0))
myAssembly.engineeringFeatures.ContourIntegral(name='subCrack',
    symmetric=ON, crackFront=crackFront, crackTip=crackTip,
    extensionDirectionMethod=Q_VECTORS, qVectors=((v1,v2),),
    midNodePosition=0.25, collapsedElementAtTip=SINGLE_NODE)

#---------------------------------------------------------------------------

# Create loads and boundary conditions 

# Create a boundary condition for X edge to be fixed in Y

region = myAssembly.sets['subXEdge']
mySubModel.DisplacementBC(name='subYFixed', createStepName='Initial',
    region=region, u2=SET, distributionType=UNIFORM, localCsys=None)

region = myAssembly.sets['subYEdge']
mySubModel.DisplacementBC(name='subXFixed', createStepName='Initial',
    region=region, u1=SET, distributionType=UNIFORM, localCsys=None)

# Create the sub modeling boundary condition

region = myAssembly.sets['drivenBoundary']
mySubModel.SubmodelBC(name='subBC', createStepName='subApplyLoad',
    region=region, globalStep='1', globalIncrement=0,
    timeScale=OFF, dof=(1, 2), globalDrivingRegion='', 
    absoluteExteriorTolerance=0.0, exteriorTolerance=0.05)

#---------------------------------------------------------------------------

# Create a mesh 

# Seed all the edges

pickedEdges1 = mySubPlateInstance.edges.findAt(((10.789162,0.,0.),),)
pickedEdges2 = mySubPlateInstance.edges.findAt(((10.526264,0.526264,0.),),)
myAssembly.seedEdgeByBias(end1Edges=pickedEdges1, end2Edges=pickedEdges2,
    ratio=3.0, number=6, constraint=FIXED)

pickedEdges2 = mySubPlateInstance.edges.findAt(((9.194185,0.,0.),),)
pickedEdges1 = mySubPlateInstance.edges.findAt(((9.473736,0.526264,0.),),)
myAssembly.seedEdgeByBias(end1Edges=pickedEdges1, end2Edges=pickedEdges2,
    ratio=3.0, number=6, constraint=FIXED)

pickedEdges = mySubPlateInstance.edges.findAt(((3.617743,0.,0.),),
    ((5.23299,4.76701,0.),), ((14.237743,4.237743,0.),),
    ((15.741623,0.,0.),),)
myAssembly.seedEdgeByNumber(edges=pickedEdges, number=2, constraint=FIXED)

pickedEdges = mySubPlateInstance.edges.findAt(((5.380602,1.913417,0.),),
    ((0.,5.,0.),), ((20.,5.,0.),), ((14.619398,1.913417,0.),),)
myAssembly.seedEdgeByNumber(edges=pickedEdges, number=3, constraint=FIXED)

pickedEdges = mySubPlateInstance.edges.findAt(((10.,5.,0.),),
    ((10.,10.,0.),),)
myAssembly.seedEdgeByNumber(edges=pickedEdges, number=6, constraint=FIXED)

# Assign meshing controls to the respective regions

pickedRegions = mySubPlateInstance.faces.findAt(((8.649209,0.450264,0.),),
    ((10.,0.900527,0.),), ((11.350791,0.450263,0.),),)
myAssembly.setMeshControls(regions=pickedRegions, elemShape=QUAD_DOMINATED,
    technique=SWEEP)

pickedRegions = mySubPlateInstance.faces.findAt(((9.805735,5.959446,0.),),
    ((16.54391,1.931576,0.),), ((4.425367,2.383505,0.),),)
myAssembly.setMeshControls(regions=pickedRegions, elemShape=QUAD,
    technique=STRUCTURED)

elemType1 = mesh.ElemType(elemCode=CPE8RH, elemLibrary=STANDARD)
elemType2 = mesh.ElemType(elemCode=CPE6M, elemLibrary=STANDARD)
faces1 = mySubPlateInstance.faces
pickedRegions =(faces1, )
myAssembly.setElementType(regions=pickedRegions,
    elemTypes=(elemType1, elemType2))
partInstances =(mySubPlateInstance, )
myAssembly.generateMesh(regions=partInstances)

#---------------------------------------------------------------------------

# Request history output for the crack

mySubModel.historyOutputRequests.changeKey(fromName='H-Output-1',
    toName='JInt')
mySubModel.historyOutputRequests['JInt'].setValues(contourIntegral='subCrack',
    numberOfContours=6)

#---------------------------------------------------------------------------

# Create the job 

myJob = mdb.Job(name=subModelName, model=subModelName,
    description='Contour integral analysis')
mdb.saveAs(pathName=subModelName)

#---------------------------------------------------------------------------
