from abaqus import *
from abaqusConstants import *
myViewport = session.Viewport(name='Viewport: 1', origin=(0.0, 0.0), width=300.0,
    height=200.0)
myViewport.makeCurrent()
myViewport.maximize()
from caeModules import *
from driverUtils import executeOnCaeStartup
executeOnCaeStartup()
#: Executing "onCaeStartup()" in the site directory ...
Mdb()
plateModel = mdb.Model(name='plateModel')
#delete model Model-1
if 'Model-1' in mdb.models:
    del mdb.models['Model-1']
#
# Create shell part from sketch
#
s = plateModel.ConstrainedSketch(name='__profile__',
    sheetSize=2000.0)
g, v, d, c = s.geometry, s.vertices, s.dimensions, s.constraints
s.setPrimaryObject(option=STANDALONE)
s.rectangle(point1=(-300.0, 300.0), point2=(300.0, -300.0))
s.resetView()
plate = plateModel.Part(name='ShellPlate', dimensionality=THREE_D,
    type=DEFORMABLE_BODY)
plate.BaseShell(sketch=s)
s.unsetPrimaryObject()
del plateModel.sketches['__profile__']
#
# Create material and shell section
#
steel = plateModel.Material(name='Steel')
steel.Elastic(table=((210000.0, 0.3), ))
steel.Density(table=((0.0785, ), ))
plateModel.HomogeneousShellSection(name='ShellSection',
    preIntegrate=OFF, material='Steel', thicknessType=UNIFORM, thickness=1.5,
    thicknessField='', idealization=NO_IDEALIZATION, poissonDefinition=DEFAULT,
    thicknessModulus=None, temperature=GRADIENT, useDensity=OFF,
    integrationRule=SIMPSON, numIntPts=5)
allFaces = plate.faces
allEdges = plate.edges
allDatums = plate.datums
face1 = allFaces.findAt((0.,0.,0.), )
region = plate.Set(faces=allFaces, name='Set-1')
plate.SectionAssignment(region=region, sectionName='ShellSection', offset=0.0,
    offsetType=MIDDLE_SURFACE, offsetField='',
    thicknessAssignment=FROM_SECTION)
mdb.saveAs(pathName='BeadPlate')
plateAssembly = plateModel.rootAssembly
plateAssembly.DatumCsysByDefault(CARTESIAN)
plateInstance = plateAssembly.Instance(name='ShellPlate-1', part=plate, dependent=ON)
#
# Create step - linear perturbation with default time incrementation
#
plateModel.StaticLinearPerturbationStep(name='StaticStep',
    previous='Initial', description='Static, linear perturbation')
#
# Partition face with a sketch
#
f, e, d1 = allFaces, allEdges, allDatums
t = plate.MakeSketchTransform(sketchPlane=f[0], sketchUpEdge=e[3],
    sketchPlaneSide=SIDE1, origin=(0.0, 0.0, 0.0))
s1 = plateModel.ConstrainedSketch(name='__profile__',
    sheetSize=1697.05, gridSpacing=30.0, transform=t)
g, v, d, c = s1.geometry, s1.vertices, s1.dimensions, s1.constraints
s1.setPrimaryObject(option=SUPERIMPOSE)
#
plate.projectReferencesOntoSketch(sketch=s1, filter=COPLANAR_EDGES)
s1.rectangle(point1=(-30.0, 30.0), point2=(30.0, -30.0))
#
allFaces = plate.faces
face1 = allFaces.findAt((0.,0.,0.), )
e1, d2 = plate.edges, plate.datums
plate.PartitionFaceBySketch(sketchUpEdge=e1[3], faces=face1, sketch=s1)
s1.unsetPrimaryObject()
del plateModel.sketches['__profile__']
plateAssembly.regenerate()
#
# Create BC - u1 = u2 = u3 = 0
# Create load - pressure of 1.0 on partitioned face
#
allEdges = plateAssembly.instances['ShellPlate-1'].edges
e1 = allEdges.findAt(((0,-300,0),))
e2 = allEdges.findAt(((-300,0,0),))
e3 = allEdges.findAt(((0,300,0),))
e4 = allEdges.findAt(((300,0,0),))
outerEdges = ((e1,e2,e3,e4),)
plateModel.DisplacementBC(name='FixEdges',
    createStepName='StaticStep', region=outerEdges, u1=0.0, u2=0.0, u3=0.0, amplitude=UNSET, fixed=OFF,
    distributionType=UNIFORM, fieldName='', localCsys=None)
#
# Apply pressure of 1.0 at center of plate
#
instanceFaces = plateInstance.faces
side1Faces1 = instanceFaces.findAt(((-10.0, -10.0, 0.0), ))
region = plateAssembly.Surface(side1Faces=side1Faces1, name='Surf-1')
mdb.models['plateModel'].Pressure(name='Load-1', createStepName='StaticStep',
    region=region, distributionType=UNIFORM, field='', magnitude=1.0)

#
# Mesh plate
# Seed size = 15
# Element type = S4
# Mesh technique = medial axis
#
#
plate.seedPart(size=15.0, deviationFactor=0.1, minSizeFactor=0.1)
elemType1 = mesh.ElemType(elemCode=S4, elemLibrary=STANDARD)
bothFaces = allFaces.findAt(((-10.0, -10.0, 0.0), ), ((200.0, 10.0, 0.0), ))
regions = (bothFaces, )
plate.setElementType(regions=regions, elemTypes=(elemType1, ))
plate.setMeshControls(regions=bothFaces, algorithm=MEDIAL_AXIS)
plate.generateMesh()
plateAssembly.regenerate()
#
# Create bead optimization task, bead width = 60
#
plateModel.BeadTask(name='BeadTask', region=MODEL, beadWidth=60.0)
#
# Create two design responses
# strain energy
# bead height
#
plateModel.optimizationTasks['BeadTask'].SingleTermDesignResponse(
    name='strainResponse', region=MODEL, identifier='STRAIN_ENERGY',
    drivingRegion=None, operation=SUM, stepOptions=())
plateModel.optimizationTasks['BeadTask'].SingleTermDesignResponse(
    name='beadHeightResponse', region=MODEL, identifier='BEAD_HEIGHT',
    drivingRegion=None, operation=MAXIMUM, stepOptions=())
#
# Minimize strain energy is the objective
#
plateModel.optimizationTasks['BeadTask'].ObjectiveFunction(
    name='Objective-1', objectives=((OFF, 'strainResponse', 1.0, 0.0, ''), ))
#
# Bead height = 20 is the constraint
#
plateModel.optimizationTasks['BeadTask'].OptimizationConstraint(
    name='Opt-Constraint-1', designResponse='beadHeightResponse',
    restrictionMethod=ABSOLUTE_EQUAL, restrictionValue=20.0)
plateModel.optimizationTasks['BeadTask'].setValues(
    areBCRegionsFrozen=ON, frozenBoundaryConditionRegion=MODEL)
#
# Display assembly
#
myViewport = session.viewports['Viewport: 1']
myViewport.setValues(displayedObject=plateAssembly)
#
# Create optimization process
#
myJob=mdb.OptimizationProcess(name='BeadProcess', model='plateModel', task='BeadTask',
    description='', prototypeJob='BeadJob', maxDesignCycle=3,
    odbMergeFrequency=2, dataSaveFrequency=OPT_DATASAVE_SPECIFY_CYCLE)
#
# Create optimization job
#
mdb.optimizationProcesses['BeadProcess'].Job(name='BeadJob',
    model='plateModel', atTime=None, waitMinutes=0, waitHours=0, queue=None,
    memory=90, memoryUnits=PERCENTAGE, getMemoryFromAnalysis=True,
    multiprocessingMode=DEFAULT, numCpus=1, numGPUs=0)
mdb.saveAs(pathName='BeadPlate')
myJob.submit()
myJob.waitForCompletion()
#
# Combine ODBs
#
mdb.CombineOptResults(optResultLocation='BeadProcess',
    includeResultsFrom=INITIAL, optIter=INITIAL_AND_LAST, models=ALL, steps=ALL, analysisFieldVariables=('S', 'U'))
#
# Manually open the combined output database file and display the optimized nodal displacement
#