# -*- coding: mbcs -*-
"""
HSK_63_axi_aniso.py

Publication-ready Abaqus script for the generation and analysis of an
axisymmetric anisotropic HSK63 shaft-hub connection model.

Purpose
-------
This script generates the axisymmetric finite-element model used for the
anisotropic material investigations of Chapter 4 of the dissertation.
The model contains holder, spindle interface and clamping ring geometry.

Parameter interface
-------------------
The script is intended to be executed from Abaqus driver scripts via
``execfile(..., __main__.__dict__)``. If globals are not provided, the
original in-script test values are used as fallbacks in order to preserve
the legacy execution behaviour of the source script.

Relevant globals
    g_Jobname : str
        Abaqus job name.
    g_Emod_H : float
        Holder Young's modulus [MPa].
    g_Emod_A : float
        Spindle Young's modulus [MPa].
    g_reib : float
        Friction coefficient [-].
    g_F_sp : float
        Clamping force [N].
    g_Umin : float
        Rotational speed [rpm].
    g_tol : int
        Tolerance case selector.
    material_i : str
        Material name for anisotropic holder definition.
    g_M_t : float
        Applied torque [Nm].

Notes
-----
The cleanup preserves the modelling logic of the legacy script and focuses
on documentation, parameter transparency and consistent structure.
"""

from part import *
from material import *
from section import *
from assembly import *
from step import *
from interaction import *
from load import *
from mesh import *
from optimization import *
from job import *
from sketch import *
from visualization import *
from connectorBehavior import *
import regionToolset
import math
from math import pi, tan, sqrt


def _optional_global(name, default):
    return globals().get(name, default)


# ---------------------------------------------------------------------------
# Study control parameters
# ---------------------------------------------------------------------------
g_Emod_H = _optional_global('g_Emod_H', 210000.0)
g_Emod_A = _optional_global('g_Emod_A', 210000.0)
g_q = _optional_global('g_q', 0)
g_reib = _optional_global('g_reib', 0.15)
g_F_sp = _optional_global('g_F_sp', 18000.0)
g_Umin = _optional_global('g_Umin', 25000.0)
g_tol = _optional_global('g_tol', 2)
material_i = _optional_global('material_i', 'HexMC')
g_M_t = _optional_global('g_M_t', 53.0)
g_Jobname = _optional_global('g_Jobname', 'HSK63_axi_aniso')

# Clamping ring state: 1 = active, 0 = inactive
r = 1


# ---------------------------------------------------------------------------
# HSK holder geometry parameters
# ---------------------------------------------------------------------------
# Geometry definition
H_d1=62.94
H_d2=48.01
H_d2_min= 48.007 #HSK-E min
H_d2_max= 48.013 #HSK-E max
if g_tol == 0:
	H_d2 = H_d2_max
elif g_tol ==1:
	H_d2 = H_d2_min
else:
 H_d2 = H_d2
H_d3=34.05
H_d4=40.08
H_d5=37.0
H_d6=8.4
#H_d7=34.5 (O-Ringeinstich wird vernachlaessigt)
H_d15=18.0
H_L1=31.9
H_L2=6.3
H_L5=18.13
H_L6=9.95
H_L7=1.15
H_L9=11.85
H_f1=25.95
H_r1=1.2
H_r4=8.0
H_r5=0.6
H_r6=1.5
H_AL2=2.8681
#Freistich
H_rf=0.6
H_t1=0.25
H_t2=0.125
H_ff=2
#AL4=15 Grad Winkel Freistich
H_AL4=15.0
#AL5=8 Grad Winkel Freistich
H_AL5=8.0
##Werte ohne Norm 
#r10 Ersatzfreistich  
H_r10=0.4
#L20 Bundhoehe
H_L20=H_f1
#AL1 Spannwinkel
H_AL1=29.75
#AL3 Fasenwinkel bei L7 
H_AL3=30.0
##Arbeitvariablen Null setzen
H_r11=0.0
H_r12=0.0
H_r13=0.0
H_r14=0.0
H_r15=0.0
H_r16=0.0
H_r17=0.0
H_r18=0.0
H_r19=0.0
H_r20=0.0
H_r21=0.0
H_r22=0.0
H_r23=0.0
H_r24=0.0
H_r25=0.0
H_r26=0.0
H_r27=0.0
H_r28=0.0
##Anfang Zeichnen
mdb.models['Model-1'].ConstrainedSketch(name='__profile__', sheetSize=200.0)
mdb.models['Model-1'].sketches['__profile__'].sketchOptions.setValues(viewStyle=AXISYM)
mdb.models['Model-1'].sketches['__profile__'].ConstructionLine(point1=(0.0, -100.0), point2=(0.0, 100.0))
mdb.models['Model-1'].sketches['__profile__'].FixedConstraint(entity= mdb.models['Model-1'].sketches['__profile__'].geometry[2])
#Beginn Geometrie
#Mathematik
H_r11=(H_AL3*pi/180)
H_r12=(H_AL1*pi/180)
H_r13=(H_AL2*pi/180)
H_r25=(H_AL4*pi/180)
H_r26=(H_AL5*pi/180)
H_r14=(tan(H_r13)*(H_L2))
H_r15=(tan(H_r12)*(H_d5-H_d3)/2)
H_r16=(H_L1-H_L5-H_r15)
H_r17=(tan(H_r12)*(H_d4-H_d5)/2)
H_r18=(H_L1-H_L5+H_r17)
H_r19=(H_d2/2+H_r14)
H_r20=(tan(H_r13)*(H_L1-H_L2))
H_r21=(H_d2/2-H_r20)
H_r22=(tan(H_r11)*(H_L7))
H_r23=(H_d3/2+H_r22)
H_r24=(H_d5/2)
H_r27=(tan(H_r13)*(H_L2-H_ff))
H_r28=(H_r27+H_d2/2)

H=mdb.models['Model-1'].sketches['__profile__']

#Linie Sektion 3
H.Line(point1=(H_d3/2 , H_L7),point2=(H_d3/2 , H_r16))
H.VerticalConstraint(addUndoState=False, entity=H.geometry[3])
H.FixedConstraint(entity= H.vertices[0])
H.FixedConstraint(entity= H.vertices[1])
#Linie Sektion 4 
H.Line(point1=(H_d3/2 , H_r16), point2=(H_d5/2 , (H_L1-H_L5)))
H.FixedConstraint(entity= H.vertices[2])
#Linie Sektion 5 
H.Line(point1=(H_d5/2 , H_L1-H_L5), point2=(H_d4/2 , H_r18))
H.FixedConstraint(entity= H.vertices[3])
#Linie Sektion 6
H.Line(point1=(H_d4/2, H_r18), point2=(H_d4/2 , 20))
H.VerticalConstraint(addUndoState=False, entity=H.geometry[6])
#Kreisbogen Sektion 7
H.ArcByStartEndTangent(entity= H.geometry[6], point1=(H_d4/2, 20), point2=(H_d3/2, H_L1))
H.RadialDimension(curve= H.geometry[7], radius=(H_r4), textPoint=(11.875, 17.5))
H.DistanceDimension(entity1= H.vertices[5], entity2= H.geometry[2], textPoint=(6.2999267578125, 21.4470520019531), value=(H_d3/2))
H.VerticalDimension(textPoint=( 4.65307235717773, 8.4326114654541), value=(H_L1-H_L7), vertex1= H.vertices[5], vertex2= H.vertices[0])
H.FixedConstraint(entity= H.vertices[4])
H.FixedConstraint(entity= H.vertices[5])
H.FixedConstraint(entity= H.vertices[6])
#Linie Sektion 8
H.Line(point1=(H_d3/2, H_L1), point2=(H_d3/2 , (H_L1+H_L6)))
H.VerticalConstraint(addUndoState=False, entity=H.geometry[8])
H.FixedConstraint(entity= H.vertices[7])
#Linie Sektion 9
H.Line(point1=(H_d3/2, (H_L1+H_L6)), point2=(H_d15/2 , (H_L1+H_L6)))
H.HorizontalConstraint(addUndoState=False, entity=H.geometry[9])
H.FixedConstraint(entity= H.vertices[8])
#Linie Sektion 10
H.Line(point1=(H_d15/2, (H_L1+H_L6) ), point2=(H_d15/2 , (H_L1+H_L6+H_L9)))
H.VerticalConstraint(addUndoState=False, entity=H.geometry[10])
H.FixedConstraint(entity= H.vertices[9])
#Linie Sektion 11
H.Line(point1=(H_d15/2, (H_L1+H_L6+H_L9)), point2=(H_d6/2 , (H_L1+H_L6+H_L9)))
H.HorizontalConstraint(addUndoState=False, entity=H.geometry[11])
H.FixedConstraint(entity= H.vertices[10])
#Linie Sektion 12
H.Line(point1=(H_d6/2, (H_L1+H_L6+H_L9) ), point2=(H_d6/2 , (H_L1+H_L20)))
H.VerticalConstraint(addUndoState=False, entity=H.geometry[12])
H.FixedConstraint(entity= H.vertices[11])
#Linie Sektion 13
H.Line(point1=(H_d6/2, (H_L1+H_L20)), point2=(H_d1/2 , (H_L1+H_L20)))
H.HorizontalConstraint(addUndoState=False, entity=H.geometry[13])
H.FixedConstraint(entity= H.vertices[12])
#Linie Sektion 14
H.Line(point1=(H_d1/2, (H_L1+H_L20) ), point2=(H_d1/2 , (H_L1)))
H.VerticalConstraint(addUndoState=False, entity=H.geometry[14])
H.FixedConstraint(entity= H.vertices[13])
#Linie Sektion 15
H.Line(point1=(H_d1/2, (H_L1)), point2=(H_r19 , (H_L1)))
H.HorizontalConstraint(addUndoState=False, entity=H.geometry[15])
H.FixedConstraint(entity= H.vertices[14])
#Linie Sektion 16 
H.Line(point1=(H_r19 , H_L1), point2=(H_r21 , 0))
H.FixedConstraint(entity= H.vertices[15])
#Linie Sektion 17
H.Line(point1=(H_r21, 0), point2=( H_r23, 0))
H.HorizontalConstraint(addUndoState=False, entity=H.geometry[17])
H.FixedConstraint(entity= H.vertices[16])
#Linie Sektion 18 
H.Line(point1=(H_r23, 0), point2=(H_d3/2 , H_L7))
#Verrundung r5
H.FilletByRadius(curve1= H.geometry[5], curve2= H.geometry[6], nearPoint1=((H_d4/2-0.5), (H_r18-0.5)), nearPoint2=((H_d4/2+0.2), (H_r18+2)), radius= H_r5)
H.FixedConstraint(entity= H.vertices[17])
H.FixedConstraint(entity= H.vertices[18])
H.FixedConstraint(entity= H.vertices[19])
#Verrundung r6
H.FilletByRadius(curve1= H.geometry[8], curve2= H.geometry[9], nearPoint1=((H_d3/2+0.2), (H_L1+H_L6-3)), nearPoint2=((H_d3/2-2), (H_L1+H_L6+0.5)), radius= H_r6)
H.FixedConstraint(entity= H.vertices[22])
H.FixedConstraint(entity= H.vertices[23])
H.FixedConstraint(entity= H.vertices[24])
#Verrundung r10 (Ersatzfreistich)
H.FilletByRadius(curve1= H.geometry[15], curve2= H.geometry[16], nearPoint1=((H_r19+2), (H_L1+0.2)), nearPoint2=((H_r19-0.5), (H_L1-1)), radius= H_r10)
H.FixedConstraint(entity= H.vertices[27])
H.FixedConstraint(entity= H.vertices[28])
H.FixedConstraint(entity= H.vertices[29])
#Verrundung r1 
H.FilletByRadius(curve1= H.geometry[16], curve2= H.geometry[17], nearPoint1=((H_r21+2), (2)), nearPoint2=((H_r21-2), (0-0.1)), radius= H_r1)
H.FixedConstraint(entity= H.vertices[32])
H.FixedConstraint(entity= H.vertices[33])
H.FixedConstraint(entity= H.vertices[34])

#Loeschen Bemassungen
H.delete(objectList=(H.dimensions[0], ))
H.delete(objectList=(H.dimensions[1], ))
H.delete(objectList=(H.dimensions[2], ))
H.delete(objectList=(H.dimensions[3], ))
H.delete(objectList=(H.dimensions[4], ))
H.delete(objectList=(H.dimensions[5], ))
H.delete(objectList=(H.dimensions[6], ))

#Freistich nach Norm einfuegen (0 = Ersatzradius, 1=Freistich nach Norm)
q=1
if q == 0:
    print('Radius statt Freistich')
else:
    H.delete(objectList=( H.geometry[21], ))
    H.ConstructionLine(point1=( H_d4/2, (H_L1+H_t2)), point2=(H_d1/2,(H_L1+H_t2)))
    H.HorizontalConstraint(addUndoState=False, entity=H.geometry[23])
    H.DistanceDimension(entity1=H.geometry[15], entity2=H.geometry[23], textPoint=(27.1692657470703, 32.4754295349121), value=H_t2)
    H.DistanceDimension(entity1= H.vertices[30], entity2=  H.geometry[2], textPoint=( 6.78884696960449, 35.9036750793457), value=H_r19+2)
    H.DistanceDimension(entity1=H.geometry[15], entity2=H.vertices[31], textPoint=(29.1119537353516, 31.4285888671875), value=H_ff)
    #Linie unter 8 Grad
    H.Line(point1=(H_r19+2, H_L1), point2=(H_r19-0.5, H_L1+H_t2-0.01))
    #Linie Parallel zur Drehachse im Freistich
    H.Line(point1=(H_r19-0.5, H_L1+H_t2-0.01), point2=(H_r19-0.5, H_L1-1.5))
    H.VerticalConstraint(addUndoState= False, entity=H.geometry[25])
    #Linie unter 15 Grad
    H.Line(point1=(H_r19-0.5, H_L1-1.5), point2=(H_r28, H_L1-H_ff))
    #Verrundungen im Freistich
    H.FilletByRadius(curve1=H.geometry[25], curve2=H.geometry[24], nearPoint1=( H_r19-0.5, H_L1+H_t2-0.3), nearPoint2=(H_r19-0.3, H_L1+H_t2-0.1), radius=H_rf)
    H.FilletByRadius(curve1= H.geometry[25], curve2= H.geometry[26], nearPoint1=( H_r19-0.5, H_L1-1.25), nearPoint2=(H_r19-0.3, H_L1-1.6), radius=H_rf)
    #Bedingungen einbauen
    H.DistanceDimension(entity1= H.vertices[31], entity2= H.geometry[25], textPoint=(24.0665969848633, 30.8173751831055), value=H_t1)
    H.delete(objectList=(H.dimensions[8], ))
    H.AngularDimension(line1= H.geometry[24], line2= H.geometry[15], textPoint=(27.9360599517822, 31.8545627593994), value=H_AL5)
    H.TangentConstraint(entity1=H.geometry[27], entity2= H.geometry[23])
    H.AngularDimension(line1=H.geometry[26], line2=H.geometry[16], textPoint=(24.3862228393555, 29.4134311676025), value=H_AL4)
    
    print('Kontur mit Freistich')


mdb.models['Model-1'].Part(dimensionality=AXISYMMETRIC, name='Halter_Parameter', type=DEFORMABLE_BODY, twist=ON)
mdb.models['Model-1'].parts['Halter_Parameter'].BaseShell(sketch= mdb.models['Model-1'].sketches['__profile__'])
del mdb.models['Model-1'].sketches['__profile__']

print('Erstellung Halter erfolgreich')

#g_Emod_H=400000.0
#g_Emod_A=210000.0
# Geometry definition
A_d1=63.0
#A_d1=g_d_1
#A_d2=47.998
A_d2=47.995 #Werte fuer HSK-E
A_d2_min=47.993 #Werte fuer HSK-E min
A_d2_max=47.997 #Werte fuer HSK-E min
if g_tol == 0:
	A_d2 = A_d2_min
elif g_tol ==1:
	A_d2 = A_d2_max
else:
 A_d2 = A_d2
A_d4=46.58
A_d5=47.3
A_L1=33.1
A_L2=6.3
A_L3=32.0
A_L5=1.0
A_L7=2.5
A_AL2=2.8624
##Werte ohne Norm 
s_l_esp=50.0
#L20 Laenge bis zur Spindel
A_L20=s_l_esp
#AL3 Fasenwinkel bei L5 
A_AL3=30.0
#d10 Hohlzylinder Durchmesser Spindel = D3
A_d10=34.0
#r1 Übergangsradius an Spindelfase beide Enden und Einstich
A_r1=0.1
##Arbeitvariablen Null setzen
A_r11=0.0
A_r13=0.0
A_r14=0.0
A_r15=0.0
A_r16=0.0
A_r17=0.0
A_r18=0.0
A_r19=0.0
##Anfang Zeichnen
mdb.models['Model-1'].ConstrainedSketch(name='__profile__', sheetSize=200.0)
mdb.models['Model-1'].sketches['__profile__'].sketchOptions.setValues(viewStyle=AXISYM)
mdb.models['Model-1'].sketches['__profile__'].ConstructionLine(point1=(0.0, -100.0), point2=(0.0, 100.0))
mdb.models['Model-1'].sketches['__profile__'].FixedConstraint(entity= mdb.models['Model-1'].sketches['__profile__'].geometry[2])

A=mdb.models['Model-1'].sketches['__profile__']
#Beginn Geometrie
#Mathematik
A_r11=(A_AL3*pi/180)
A_r13=(A_AL2*pi/180)
A_r14=(tan(A_r13)*(A_L2-A_L5))
A_r15=(tan(A_r11)*(A_L5))
A_r16=(A_d2/2+A_r14)
A_r17=(tan(A_r13)*(A_L3-A_L7-A_L2))
A_r18=(A_r16+A_r15)
A_r19=(A_d2/2-A_r17)

#Linie Sektion 3
A.Line(point1=(A_r18 , 0),point2=(A_d1/2 , 0))
A.HorizontalConstraint(addUndoState=False, entity=A.geometry[3])
A.FixedConstraint(entity= A.vertices[0])
A.FixedConstraint(entity= A.vertices[1])
#Linie Sektion 4 
A.Line(point1=(A_d1/2 , 0), point2=(A_d1/2 , (-1*A_L20)))
A.VerticalConstraint(addUndoState=False, entity=A.geometry[4])
A.FixedConstraint(entity= A.vertices[2])
#Linie Sektion 5
A.Line(point1=(A_d1/2, (-1*A_L20)), point2=(A_d10/2 , (-1*A_L20)))
A.HorizontalConstraint(addUndoState=False, entity=A.geometry[5])
A.FixedConstraint(entity= A.vertices[3])
#Linie Sektion 6
A.Line(point1=(A_d10/2, (-1*A_L20)), point2=(A_d10/2 , (-1*A_L1)))
A.VerticalConstraint(addUndoState=False, entity=A.geometry[6])
A.FixedConstraint(entity= A.vertices[4])
#Linie Sektion 7
A.Line(point1=(A_d10/2, (-1*A_L1)), point2=(A_d4/2 , (-1*A_L1)))
A.HorizontalConstraint(addUndoState=False, entity=A.geometry[7])
A.FixedConstraint(entity= A.vertices[5])
#Linie Sektion 8
A.Line(point1=(A_d4/2, (-1*A_L1) ), point2=(A_d4/2 , (-1*A_L3)))
A.VerticalConstraint(addUndoState=False, entity=A.geometry[8])
A.FixedConstraint(entity= A.vertices[6])
#Linie Sektion 9
A.Line(point1=(A_d4/2, (-1*A_L3)), point2=(A_d5/2 , (-1*A_L3)))
A.HorizontalConstraint(addUndoState=False, entity=A.geometry[9])
A.FixedConstraint(entity= A.vertices[7])
#Linie Sektion 10
A.Line(point1=(A_d5/2, (-1*A_L3) ), point2=(A_d5/2 , (A_L7-A_L3)))
A.VerticalConstraint(addUndoState=False, entity=A.geometry[10])
A.FixedConstraint(entity= A.vertices[8])
#Linie Sektion 11
A.Line(point1=(A_d5/2, (A_L7-A_L3)), point2=(A_r19 , (A_L7-A_L3)))
A.HorizontalConstraint(addUndoState=False, entity=A.geometry[11])
A.FixedConstraint(entity= A.vertices[9])
#Linie Sektion 12
A.Line(point1=(A_r19, (A_L7-A_L3) ), point2=(A_r16 , (-1*A_L5)))
A.FixedConstraint(entity= A.vertices[10])
#Linie Sektion 13
A.Line(point1=(A_r16, (-1*A_L5)), point2=(A_r18 , 0))
#Verrundung Stelle 12-13
A.FilletByRadius(curve1= A.geometry[12], curve2= A.geometry[13], nearPoint1=((A_r16-0.2), (-1*A_L5-0.2)), nearPoint2=((A_r16+0.2), (-1*A_L5+0.2)), radius= A_r1)
A.FixedConstraint(entity=A.vertices[11])
A.FixedConstraint(entity=A.vertices[12])
A.FixedConstraint(entity=A.vertices[13])
A.delete(objectList=(A.dimensions[0], ))
#Verrundung Stelle 13-3
A.FilletByRadius(curve1= A.geometry[13], curve2= A.geometry[3], nearPoint1=((A_r18-0.2), (-0.2)), nearPoint2=((A_r18+0.2), (+0.2)), radius= A_r1)
A.FixedConstraint(entity=A.vertices[16])
A.FixedConstraint(entity=A.vertices[17])
A.FixedConstraint(entity=A.vertices[18])
A.delete(objectList=(A.dimensions[1], ))
#Verrundung Stelle 8-9
A.FilletByRadius(curve1= A.geometry[8], curve2= A.geometry[9], nearPoint1=((A_d4/2), (-1*A_L3-0.2)), nearPoint2=((A_d4/2+0.2), (-1*A_L3)), radius= A_r1)
A.FixedConstraint(entity=A.vertices[21])
A.FixedConstraint(entity=A.vertices[22])
A.FixedConstraint(entity=A.vertices[23])
A.delete(objectList=(A.dimensions[2], ))
#Verrundung Stelle 9-10
A.FilletByRadius(curve1= A.geometry[9], curve2= A.geometry[10], nearPoint1=((A_d5/2-0.2), (-1*A_L3)), nearPoint2=((A_d5/2), (-1*A_L3+0.2)), radius= A_r1)
A.FixedConstraint(entity=A.vertices[26])
A.FixedConstraint(entity=A.vertices[27])
A.FixedConstraint(entity=A.vertices[28])
A.delete(objectList=(A.dimensions[3], ))
#Verrundung Stelle 10-11
A.FilletByRadius(curve1= A.geometry[10], curve2= A.geometry[11], nearPoint1=((A_d5/2), (A_L7-A_L3-0.2)), nearPoint2=((A_d5/2-0.2), (A_L7-A_L3)), radius= A_r1)
A.FixedConstraint(entity=A.vertices[31])
A.FixedConstraint(entity=A.vertices[32])
A.FixedConstraint(entity=A.vertices[33])
A.delete(objectList=(A.dimensions[4], ))
#Verrundung Stelle 11-12
A.FilletByRadius(curve1= A.geometry[11], curve2= A.geometry[12], nearPoint1=((A_r19+0.2), (A_L7-A_L3)), nearPoint2=((A_r19), (A_L7-A_L3+0.2)), radius= A_r1)
A.FixedConstraint(entity=A.vertices[36])
A.FixedConstraint(entity=A.vertices[37])
A.FixedConstraint(entity=A.vertices[38])
A.delete(objectList=(A.dimensions[5], ))


mdb.models['Model-1'].Part(dimensionality=AXISYMMETRIC, name='Aufnahme_Parameter', type=DEFORMABLE_BODY, twist=ON)
mdb.models['Model-1'].parts['Aufnahme_Parameter'].BaseShell(sketch= mdb.models['Model-1'].sketches['__profile__'])
del mdb.models['Model-1'].sketches['__profile__']

mdb.models['Model-1'].parts['Aufnahme_Parameter'].PartitionEdgeByParam(edges=mdb.models['Model-1'].parts['Aufnahme_Parameter'].edges.findAt(((A_d1/2, -0.1, 0.0), )), parameter=(A_L1/A_L20))
#mdb.models['Model-1'].parts['Aufnahme_Parameter'].PartitionEdgeByParam(edges=mdb.models['Model-1'].parts['Aufnahme_Parameter'].edges.findAt(((A_d2/2, -A_L2, 0.0), )), parameter=0.9)

AF=mdb.models['Model-1'].parts['Aufnahme_Parameter']
AF.Set(edges= AF.edges.findAt((((A_r18-tan(A_r11)*A_L5/2), (-1*A_L5/2), 0.0), ), ((A_d2/2, (-1*A_L2), 0.0), ), ((A_d5/2-0.1, (-1*A_L3+A_L7), 0.0), ), ((A_d5/2, (-1*A_L3+0.1), 0.0), ), ((A_d5/2-0.1, (-1*A_L3), 0.0), ), 
    ((A_d1/2-1, 0.0, 0.0), ), ), 
    name='Verfeinerte Kanten Aufnahme')

#print('Erstellung Aufnahme erfolgreich')

##Spannband Variablen
# S_pp=0.001
# S_d1=A_d1-S_pp
# S_ws=8.0
# S_d2=S_d1+S_ws
# S_L1=A_L1

##Anfang Zeichnen
# mdb.models['Model-1'].ConstrainedSketch(name='__profile__', sheetSize=200.0)
# mdb.models['Model-1'].sketches['__profile__'].sketchOptions.setValues(viewStyle=AXISYM)
# mdb.models['Model-1'].sketches['__profile__'].ConstructionLine(point1=(0.0, -100.0), point2=(0.0, 100.0))
# mdb.models['Model-1'].sketches['__profile__'].FixedConstraint(entity= mdb.models['Model-1'].sketches['__profile__'].geometry[2])

# S=mdb.models['Model-1'].sketches['__profile__']

##Linie Sektion 3
# S.Line(point1=(S_d1/2 , 0),point2=(S_d2/2 , 0))
# S.HorizontalConstraint(addUndoState=False, entity=S.geometry[3])
# S.FixedConstraint(entity= S.vertices[0])
# S.FixedConstraint(entity= S.vertices[1])
##Linie Sektion 4 
# S.Line(point1=(S_d2/2 , 0), point2=(S_d2/2 , (-1*S_L1)))
# S.VerticalConstraint(addUndoState=False, entity=S.geometry[4])
# S.FixedConstraint(entity= S.vertices[2])
##Linie Sektion 5
# S.Line(point1=(S_d2/2, (-1*S_L1)), point2=(S_d1/2 , (-1*S_L1)))
# S.HorizontalConstraint(addUndoState=False, entity=S.geometry[5])
# S.FixedConstraint(entity= S.vertices[3])
##Linie Sektion 6
# S.Line(point1=(S_d1/2, (-1*S_L1)), point2=(S_d1/2 , 0))
# S.VerticalConstraint(addUndoState=False, entity=S.geometry[6])

# mdb.models['Model-1'].Part(dimensionality=AXISYMMETRIC, name='Spannband', type=DEFORMABLE_BODY)
# mdb.models['Model-1'].parts['Spannband'].BaseShell(sketch= mdb.models['Model-1'].sketches['__profile__'])
# del mdb.models['Model-1'].sketches['__profile__']


### Create Materials:

# Halter:
Emod_H=g_Emod_H
m = mdb.models['Model-1']
m.Material(name='Halter')
m.materials['Halter'].Density(table=((0.0000000078, ), ))
m.materials['Halter'].Elastic(table=((Emod_H, 0.3), ))
print('Der E-Modul des Halter betraegt %E N/mm^2'% (Emod_H))

#Spannband
# Emod_S=210000.0
# m = mdb.models['Model-1']
# m.Material(name='Spannband')
# m.materials['Spannband'].Density(table=((0.0000000078, ), ))
# m.materials['Spannband'].Elastic(table=((Emod_S, 0.3), ))
# print('Der E-Modul des Spannbandes betraegt %E N/mm^2'% (Emod_S))

#Spindelaufnahme
Emod_A=g_Emod_A
m = mdb.models['Model-1']
m.Material(name='Spindelaufnahme')
m.materials['Spindelaufnahme'].Density(table=((0.0000000078, ), ))
m.materials['Spindelaufnahme'].Elastic(table=((Emod_A, 0.3), ))
print('Der E-Modul der Spindelaufnahme betraegt %E N/mm^2'% (Emod_A))

execfile('D:/Abaqus_Scratch/Parameterstudie_HSK/Materialdaten/CFK0-Grad-HSB_case_4aniso.py',  __main__.__dict__)
execfile('D:/Abaqus_Scratch/Parameterstudie_HSK/Materialdaten/CFK15-Grad-HSB_case_4aniso.py',  __main__.__dict__)
execfile('D:/Abaqus_Scratch/Parameterstudie_HSK/Materialdaten/CFK30-Grad-HSB_case_4aniso.py',  __main__.__dict__)
execfile('D:/Abaqus_Scratch/Parameterstudie_HSK/Materialdaten/CFK45-Grad-HSB_case_4aniso.py',  __main__.__dict__)
execfile('D:/Abaqus_Scratch/Parameterstudie_HSK/Materialdaten/CFK60-Grad-HSB_case_4aniso.py',  __main__.__dict__)
execfile('D:/Abaqus_Scratch/Parameterstudie_HSK/Materialdaten/CFK75-Grad-HSB_case_4aniso.py',  __main__.__dict__)
execfile('D:/Abaqus_Scratch/Parameterstudie_HSK/Materialdaten/CFK90-Grad-HSB_case_4aniso.py',  __main__.__dict__)
####HEXMC
mdb.models['Model-1'].Material(name='HexMC')
m.materials['HexMC'].Density(table=((0.00000000155, ), ))
mdb.models['Model-1'].materials['HexMC'].Elastic(type=ENGINEERING_CONSTANTS, 
    table=((12039.0, 28727.0, 28727.0, 0.24, 0.24, 0.2779, 4849.0, 4849.0, 11240.0), ))
mdb.models['Model-1'].HomogeneousSolidSection(material='HexMC', name='HexMC', thickness=None)

### Create Section:
mdb.models['Model-1'].HomogeneousSolidSection(material='Spindelaufnahme', name='Spindelaufnahme', thickness=None)
mdb.models['Model-1'].HomogeneousSolidSection(material='Halter', name='Halter', thickness=None)
mdb.models['Model-1'].HomogeneousSolidSection(material='Spannband', name='Spannband', thickness=None)


# Create Assembly
m = mdb.models['Model-1']
a = m.rootAssembly
a.DatumCsysByDefault(CARTESIAN)
a.Instance(name='Aufnahme_Parameter', part=mdb.models['Model-1'].parts['Aufnahme_Parameter'], dependent=ON)
a.Instance(name='Halter_Parameter', part=mdb.models['Model-1'].parts['Halter_Parameter'], dependent=ON)

#### Section Assignement
#Section Assignment Aufnahme
p = mdb.models['Model-1'].parts['Aufnahme_Parameter']
f = p.faces
faces = f.findAt(((A_r18 + 2.0, 0-1 , 0.0),(A_r18 + 2.0, 0-2 , 0.0),))
region = p.Set(faces=faces, name='Aufnahme_Parameter')	
p.SectionAssignment(region=region, sectionName='Spindelaufnahme', offset=0.0, 
    offsetType=MIDDLE_SURFACE, offsetField='', 
    thicknessAssignment=FROM_SECTION)
#print('Section Assignment Aufnahme erfolgreich')	
mdb.models['Model-1'].rootAssembly.regenerate()

#Section Assignment Spannband
# p = mdb.models['Model-1'].parts['Spannband']
# f = p.faces
# faces = f.findAt(((S_d1/2 + 1.0, 0-1 , 0.0),(S_d1/2 + 2.0, 0-2 , 0.0),))
# region = p.Set(faces=faces, name='Spannband')	
# p.SectionAssignment(region=region, sectionName='Spindelaufnahme', offset=0.0, 
    # offsetType=MIDDLE_SURFACE, offsetField='', 
    # thicknessAssignment=FROM_SECTION)
# print('Section Assignment Spannband erfolgreich')	
# mdb.models['Model-1'].rootAssembly.regenerate()

#Section Assignement WZH
p = mdb.models['Model-1'].parts['Halter_Parameter']
f = p.faces
faces = f.findAt(((H_d4/2 + 1, H_L1 +5 , 0.0),(H_d1/2 - 0.5, H_L1 +10 , 0.0),))
region = p.Set(faces=faces, name='Halter_Parameter')	
# p.SectionAssignment(region=region, sectionName='Spindelaufnahme', offset=0.0, 
    # offsetType=MIDDLE_SURFACE, offsetField='', 
    # thicknessAssignment=FROM_SECTION)
p.SectionAssignment(region=region, sectionName=material_i, offset=0.0, 
    offsetType=MIDDLE_SURFACE, offsetField='', 
    thicknessAssignment=FROM_SECTION)
# p.SectionAssignment(region=region, sectionName='CFK0-Grad-HSB_case_4aniso', offset=0.0, 
    # offsetType=MIDDLE_SURFACE, offsetField='', 
    # thicknessAssignment=FROM_SECTION)
#print('Section Assignment Halter erfolgreich')	
mdb.models['Model-1'].rootAssembly.regenerate()

#print('Sections erfolreich zugewiesen')

###Materialorientierung
p = mdb.models['Model-1'].parts['Halter_Parameter']
f = p.faces
faces = f[0:1]
region = regionToolset.Region(faces=faces)
orientation=None
mdb.models['Model-1'].parts['Halter_Parameter'].MaterialOrientation(
    region=region, orientationType=SYSTEM, axis=AXIS_3, localCsys=orientation, 
    fieldName='', additionalRotationType=ROTATION_ANGLE, 
    additionalRotationField='', angle=90.0, stackDirection=STACK_3)


#mount assembly
mdb.models['Model-1'].rootAssembly.instances['Halter_Parameter'].translateTo(clearance=0.0, direction=(0.0, -1.0, 0.0), fixedList=(mdb.models['Model-1'].rootAssembly.instances['Aufnahme_Parameter'].edges.findAt((A_d1/2-1, 0.0, 0.0), ), ), movableList=(mdb.models['Model-1'].rootAssembly.instances['Halter_Parameter'].edges.findAt((H_d1/2-1, H_L1, 0.0), ), ))
# mdb.models['Model-1'].rootAssembly.Instance(name='Spannband-1', part=mdb.models['Model-1'].parts['Spannband'], dependent=ON)
#print('Zusammenbau erfolgreich')

#meshing
import mesh
###Mesh Control
A_mesh_size = 0.65
A_mesh_size_contactzone=0.025
A_mesh_size_Uebergangsradius=0.005
A_mesh_size_Spannungsspitze=0.005
H_mesh_size= 0.65
H_mesh_size_ersatzfreistich=0.025
H_mesh_size_r5=0.025
H_mesh_size_Kontaktflaechen=0.02
H_mesh_size_Spannflaeche=0.05
H_mesh_size_freistich=0.05
H_mesh_size_Kontaktzone_freistich=0.01
S_mesh_size=0.25

#Quadratisch berechnungsansatz
#elemType1 = mesh.ElemType(elemCode=CAX8R, elemLibrary=STANDARD)
#elemType2 = mesh.ElemType(elemCode=CAX6, elemLibrary=STANDARD)
#linearer Berechnungsansatz
# elemType1 = mesh.ElemType(elemCode=CAX4R, elemLibrary=STANDARD)
# elemType2 = mesh.ElemType(elemCode=CAX3, elemLibrary=STANDARD)
elemType1 = mesh.ElemType(elemCode=CGAX4R, elemLibrary=STANDARD)
elemType2 = mesh.ElemType(elemCode=CGAX3, elemLibrary=STANDARD)

# Meshing Aufnahme
p = mdb.models['Model-1'].parts['Aufnahme_Parameter']
f = p.faces
pickedRegions = f.findAt(((A_r18 + 2.0, 0-1 , 0.0), ))
p.setMeshControls(regions=pickedRegions, elemShape=QUAD_DOMINATED, algorithm=ADVANCING_FRONT)
pickedRegions = (faces,)
p.setElementType(regions=pickedRegions, elemTypes=(elemType1, elemType2))
p.seedPart(size=A_mesh_size, deviationFactor=0.1, minSizeFactor=0.1)
p.seedEdgeBySize(constraint= FINER, deviationFactor=0.1, edges= p.edges.findAt((((A_r18-tan(A_r11)*A_L5/2), (-1*A_L5/2), 0.0), ), ((A_d2/2, (-1*A_L2), 0.0), ), (((A_d5/2+A_r19)/2, (-1*A_L3+A_L7), 0.0), ), ((A_d5/2, (-1*A_L3+A_L7/2), 0.0), ), (((A_d5/2+A_d4/2)/2, (-1*A_L3), 0.0), ), ((A_d1/2-1, 0.0, 0.0), ), ), minSizeFactor=0.1, size=A_mesh_size_contactzone)
#p.seedEdgeBySize(constraint= FINER, deviationFactor=0.1, edges= p.edges.findAt((((A_r16-tan(A_r13)*0.25), (-0.25-1*A_L5), 0.0), ),  ), minSizeFactor=0.1, size=A_mesh_size_Spannungsspitze)
p.seedEdgeBySize(constraint= FINER, deviationFactor=0.1, edges= p.edges.getSequenceFromMask(('[#5 ]', ), ), minSizeFactor=0.1, size=A_mesh_size_Uebergangsradius)
p = mdb.models['Model-1'].parts['Aufnahme_Parameter']

p.generateMesh()

#print('Aufnahme Mesh erfolgreich')

# Meshing Halter
p = mdb.models['Model-1'].parts['Halter_Parameter']
f = p.faces
faces = f.findAt(((H_d4/2 + 1, H_L1 +5 , 0.0), ))
pickedRegions = faces
p.setMeshControls(regions=pickedRegions, elemShape=QUAD_DOMINATED, algorithm=ADVANCING_FRONT)
pickedRegions = (faces,)
p.setElementType(regions=pickedRegions, elemTypes=(elemType1, elemType2))
p.seedPart(size=H_mesh_size, deviationFactor=0.1, minSizeFactor=0.1)
if q == 0:
    p.seedEdgeBySize(constraint=FINER, deviationFactor=0.1, edges= p.edges.getSequenceFromMask(('[#10 ]', ), ), minSizeFactor=0.1, size=H_mesh_size_ersatzfreistich)
    p.seedEdgeBySize(constraint=FINER, deviationFactor=0.1, edges= p.edges.getSequenceFromMask(('[#10000 ]', ), ), minSizeFactor=0.1, size=H_mesh_size_r5)
    #p.seedEdgeBySize(constraint=FINER, deviationFactor=0.1, edges= p.edges.getSequenceFromMask(('[#10 ]', ), ), minSizeFactor=0.1, size=H_mesh_size_Kontaktzone_fase)
    #print('Ersatzradius')
else:
    p.seedEdgeBySize(constraint=FINER, deviationFactor=0.1, edges= p.edges.getSequenceFromMask(('[#e0 ]', ), ), minSizeFactor=0.1, size=H_mesh_size_freistich)
    p.seedEdgeBySize(constraint=FINER, deviationFactor=0.1, edges= p.edges.getSequenceFromMask(('[#100000 ]', ), ), minSizeFactor=0.1, size=H_mesh_size_r5)
    p.seedEdgeBySize(constraint=FINER, deviationFactor=0.1, edges= p.edges.getSequenceFromMask(('[#110 ]', ), ), minSizeFactor=0.1, size=H_mesh_size_Kontaktzone_freistich)
    #print('Mit Freistich')

p.seedEdgeBySize(constraint=FINER, deviationFactor=0.1, edges= p.edges.findAt(((H_d2/2, (H_L1-H_L2), 0.0), ), ((H_d1/2-0.1, H_L1, 0.0), ), ), minSizeFactor=0.1, size= H_mesh_size_Kontaktflaechen)
p.seedEdgeBySize(constraint=FINER, deviationFactor=0.1, edges= p.edges.findAt((((H_d5/2+1), (H_L1+1*tan(H_r12)-H_L5), 0.0), ), (((H_d5/2-1), (H_L1-1*tan(H_r12)-H_L5), 0.0), ), ), minSizeFactor=0.1, size=H_mesh_size_Spannflaeche)
p = mdb.models['Model-1'].parts['Halter_Parameter']
p.generateMesh()
#print('WZH Mesh erfolgreich')

# Meshing Spannband
# p = mdb.models['Model-1'].parts['Spannband']
# f = p.faces
# pickedRegions = f.findAt(((S_d1/2 + 1.0, 0-1 , 0.0), ))
# p.setMeshControls(regions=pickedRegions, elemShape=QUAD_DOMINATED, algorithm=ADVANCING_FRONT)
# pickedRegions = (faces,)
# p.setElementType(regions=pickedRegions, elemTypes=(elemType1, elemType2))
# p.seedPart(size=S_mesh_size, deviationFactor=0.1, minSizeFactor=0.1)
# p = mdb.models['Model-1'].parts['Spannband']
# p.generateMesh()
# print('Spannband Mesh erfolgreich')

#print('Mesh komplett')

### Lastfaelle:
# Steps erzeugen:
mdb.models['Model-1'].StaticStep(name='Uebermass', previous='Initial', nlgeom=ON, initialInc = 0.1)
mdb.models['Model-1'].StaticStep(name='Stabilisierung', previous='Uebermass', nlgeom=ON, initialInc = 0.1)
mdb.models['Model-1'].StaticStep(name='Vorspannkraft', previous='Stabilisierung', nlgeom=ON, initialInc = 0.1)
mdb.models['Model-1'].StaticStep(name='Radialbeschleunigung', previous='Vorspannkraft', nlgeom=ON, initialInc = 0.1)
mdb.models['Model-1'].StaticStep(name='Axialkraft', previous='Radialbeschleunigung', nlgeom=ON, initialInc = 0.1)
mdb.models['Model-1'].StaticStep(name='Drehmoment', previous='Axialkraft', nlgeom=ON, initialInc = 0.1)
#print('Lastfaelle erzeugt')

#Punkte Spannflaeche und Spannkraftpunkt
J=mdb.models['Model-1']
J.rootAssembly.Surface(name='Spannflaeche', side1Edges=J.rootAssembly.instances['Halter_Parameter'].edges.findAt((((H_d5/2+1), (1*tan(H_r12)-H_L5), 0.0), ), (((H_d5/2-1), (-1*tan(H_r12)-H_L5), 0.0), ), ))
J.rootAssembly.Set(name='Spannkraftpunkt', vertices= J.rootAssembly.instances['Halter_Parameter'].vertices.findAt(((H_d5/2, (-1*H_L5), 0.0), )))
#print('Spannflaeche erzeugt')

#Berechnung der Kraefte und Pressungen
#Spannkraft
#F_SP1=-18000.0
F_SP1=-g_F_sp
F_SP2=(tan(H_AL1*pi/180)*F_SP1*-1)
F_SPges=math.sqrt(F_SP1*F_SP1+F_SP2*F_SP2)
#Flaeche des Spannkegels
#Mantelflaeche theoretischer Kegel- nicht vorhandene spitze
r_th=H_d4/2-0.3
h_th=tan(H_AL1*pi/180)*r_th
s_th=math.sqrt(r_th*r_th+h_th*h_th)
M_th=pi*r_th*s_th
r_th2=H_d3/2
h_th2=tan(H_AL1*pi/180)*r_th2
s_th2=math.sqrt(r_th2*r_th2+h_th2*h_th2)
M_th2=pi*r_th2*s_th2
M_r=M_th-M_th2
pr_SP=F_SPges/M_r
print('Die resultierende Kraft aus der Spannkraft betraegt = %F N'% (F_SPges))

#Konzentrierte Kraft(0) oder Flaechenpressung(1)
x = 1
if x == 0:
	m.ConcentratedForce(name='Stabilisierung', createStepName='Stabilisierung', region=J.parts['Halter_Parameter'].sets['Spannkraftpunkt'],cf1=F_SP2, cf2=F_SP1, distributionType=UNIFORM, field='', localCsys=None)
else:
	m.Pressure(amplitude=UNSET, createStepName='Stabilisierung', distributionType=UNIFORM, field='', magnitude=pr_SP, name='Spannkraft', region=J.rootAssembly.surfaces['Spannflaeche'] )	

#print('Spannkraft aufgebracht')

#Fliehkraft Drehzahl in U/min mit Einheitentuning
drehzahl=g_Umin
wurzelkorrektur=math.sqrt(1000.0)
kreisgeschwindigkeit = drehzahl/(60*2*pi)*wurzelkorrektur
J.RotationalBodyForce(centrifugal=ON, createStepName='Radialbeschleunigung', magnitude=kreisgeschwindigkeit, name='Zentrifugalkraft_Halter', point1=(0.0, 0.0, 0.0), point2=(0.0, 1.0, 0.0), region= J.rootAssembly.instances['Halter_Parameter'].sets['Halter_Parameter'], rotaryAcceleration=OFF)
J.RotationalBodyForce(centrifugal=ON, createStepName='Radialbeschleunigung', magnitude=kreisgeschwindigkeit, name='Zentrifugalkraft_Aufnahme', point1=(0.0, 0.0, 0.0), point2=(0.0, 1.0, 0.0), region= J.rootAssembly.instances['Aufnahme_Parameter'].sets['Aufnahme_Parameter'], rotaryAcceleration=OFF)
# J.RotationalBodyForce(centrifugal=ON, createStepName='Radialbeschleunigung', magnitude=kreisgeschwindigkeit, name='Zentrifugalkraft_Spannband', point1=(0.0, 0.0, 0.0), point2=(0.0, 1.0, 0.0), region= J.rootAssembly.instances['Spannband-1'].sets['Spannband'], rotaryAcceleration=OFF)

##Randbedingunen+Kontaktflaechen
J.rootAssembly.Set(edges= J.rootAssembly.instances['Aufnahme_Parameter'].edges.findAt(((A_d1/2-2, (-1*A_L20), 0.0), )), name='Einspannung')
J.rootAssembly.Surface(name='Kegel Aufnahme', side1Edges= J.rootAssembly.instances['Aufnahme_Parameter'].edges.findAt(((A_d2/2, (-1*A_L2), 0.0), )))
J.rootAssembly.Surface(side1Edges=J.rootAssembly.instances['Aufnahme_Parameter'].edges.getSequenceFromMask(mask=('[#1c ]', ), ),name='Kegel Aufnahme')

if q == 0:
    J.rootAssembly.Surface(name='Kegel Halter', side1Edges=J.rootAssembly.instances['Halter_Parameter'].edges.findAt(((H_d2/2, (-1*H_L2), 0.0), )))
    J.rootAssembly.Set(edges=J.rootAssembly.instances['Halter_Parameter'].edges.getSequenceFromMask(mask=('[#10000 ]', ), ), name='Radius_Spannflaeche')
    J.rootAssembly.Set(edges=J.rootAssembly.instances['Halter_Parameter'].edges.getSequenceFromMask(mask=('[#10 ]', ), ), name='Ersatzradius_am_Kegel')
else:
    J.rootAssembly.Surface(name='Kegel Halter', side1Edges=J.rootAssembly.instances['Halter_Parameter'].edges.findAt(((H_d2/2, (-1*H_L2), 0.0), )))
    J.rootAssembly.Surface(side1Edges=J.rootAssembly.instances['Halter_Parameter'].edges.getSequenceFromMask(mask=('[#18 ]', ), ),name='Kegel Halter')
    J.rootAssembly.Set(edges=J.rootAssembly.instances['Halter_Parameter'].edges.getSequenceFromMask(mask=('[#70 ]', ), ), name='Freistich-y-anteil')
    J.rootAssembly.Set(edges=J.rootAssembly.instances['Halter_Parameter'].edges.getSequenceFromMask(mask=('[#180 ]', ), ), name='Freistich-x-anteil')
    J.rootAssembly.Set(edges=J.rootAssembly.instances['Halter_Parameter'].edges.getSequenceFromMask(mask=('[#100000 ]', ), ), name='Radius_Spannflaeche')

J.rootAssembly.Surface(name='Plananlage Aufnahme',side1Edges= J.rootAssembly.instances['Aufnahme_Parameter'].edges.findAt(((A_d1/2-1, 0.0, 0.0), )))

J.rootAssembly.Surface(name='Plananlage Halter', side1Edges= J.rootAssembly.instances['Halter_Parameter'].edges.findAt(((H_d1/2-1, 0.0, 0.0), )))
J.rootAssembly.Surface(name='Spannband Aufnahme',side1Edges= J.rootAssembly.instances['Aufnahme_Parameter'].edges.findAt(((A_d1/2, -8.25, 0.0), )))
# J.rootAssembly.Surface(name='Spannband Spannband',side1Edges= J.rootAssembly.instances['Spannband-1'].edges.findAt(((S_d1/2, -8.25, 0.0), )))

J.rootAssembly.Set(edges= J.rootAssembly.instances['Aufnahme_Parameter'].edges.findAt(((A_d1/2, -8.25, 0.0), )), name='Spannband_Aufnahme')
J.rootAssembly.Set(edges= J.rootAssembly.instances['Aufnahme_Parameter'].edges.findAt(((A_d1/2-2, 0.0, 0.0), )), name='Flaechenpressung-Plan')
J.rootAssembly.Set(edges= J.rootAssembly.instances['Aufnahme_Parameter'].edges.findAt(((A_d2/2,(-1*A_L2) , 0.0), )), name='Kegel')
J.rootAssembly.Set(edges= J.rootAssembly.instances['Halter_Parameter'].edges.findAt(((H_d2/2,(-1*H_L2) , 0.0), )), name='Kegel_Halter')
# J.rootAssembly.Set(name='Fixpunkt Spannband', vertices= mdb.models['Model-1'].rootAssembly.instances['Spannband-1'].vertices.findAt(((S_d2/2, -S_L1, 0.0), )))
J.rootAssembly.Surface(name='Betriebskraftstelle', side1Edges= J.rootAssembly.instances['Halter_Parameter'].edges.findAt(((H_d1/2-1,(H_L20) , 0.0), )))
J.rootAssembly.Set(name='U2-Sperr', vertices= J.rootAssembly.instances['Halter_Parameter'].vertices.findAt(((H_d6/2, (H_L20), 0.0), )))
#J.rootAssembly.Set(edges= J.rootAssembly.instances['Halter_Parameter'].edges.findAt(((H_d1/2-0.1,(0.2) , 0.0), )), name='Planf_Halter')
a = mdb.models['Model-1'].rootAssembly
e1 = a.instances['Halter_Parameter'].edges
edges1 = e1[9:10]
a.Set(edges=edges1, name='Planf_Halter')

#Betriebskraft
#Axialkraft negativ=zug positiv=druck
F_Ax=-1000
p_Ax=F_Ax/(pi/4*(H_d1*H_d1-(H_d6*H_d6)))
J.Pressure(name='Axialkraft', createStepName='Axialkraft', region=J.rootAssembly.surfaces['Betriebskraftstelle'], distributionType=UNIFORM, field='', magnitude=p_Ax, amplitude=UNSET)

#Drehmoment
#Referenzpunkt fuer Moment
J.rootAssembly.ReferencePoint(point=(0.0, 30.0, 0.0))
J.Coupling(name='Drehmoment', controlPoint=J.rootAssembly.Set(referencePoints=(J.rootAssembly.referencePoints[26], ), name='refPoint'), surface=J.rootAssembly.surfaces['Betriebskraftstelle'], influenceRadius=WHOLE_SURFACE, couplingType=KINEMATIC, localCsys=None, u1=ON, u2=ON, ur3=ON)
#J.Coupling(name='Drehmoment', controlPoint=J.rootAssembly.Set(referencePoints=(J.rootAssembly.referencePoints[98], ), name='refPoint'), surface=J.rootAssembly.surfaces['Betriebskraftstelle'], influenceRadius=WHOLE_SURFACE, couplingType=KINEMATIC, localCsys=None, u1=ON, u2=ON, ur3=ON)
M_t=M_tinNm*1000
J.Moment(name='Drehmoment', createStepName='Drehmoment', region=J.rootAssembly.sets['refPoint'], cm2=M_t, distributionType=UNIFORM, field='', localCsys=None)

#Einspannung
J.EncastreBC(createStepName='Initial', localCsys=None, name='Feste Einspannung', region=J.rootAssembly.sets['Einspannung'])
#Eckpunkt oben links U2 Richtung gesperrt nur wahrend Uebermass
mdb.models['Model-1'].DisplacementBC(amplitude=UNSET, createStepName='Initial', distributionType=UNIFORM, fieldName='', localCsys=None, name='Fixpunkt', region=mdb.models['Model-1'].rootAssembly.sets['U2-Sperr'], u1=UNSET, u2=SET, ur3=UNSET)
mdb.models['Model-1'].boundaryConditions['Fixpunkt'].deactivate('Vorspannkraft')
#Eckpunkt oben rechts in U2 Richtung sperren spannband
# mdb.models['Model-1'].DisplacementBC(amplitude=UNSET, createStepName='Initial', distributionType=UNIFORM, fieldName='', localCsys=None, name='Fixpunkt Spannband', region=mdb.models['Model-1'].rootAssembly.sets['Fixpunkt Spannband'], u1=UNSET, u2=SET, ur3=UNSET)

#Verschiebung um -0.2 in U2 Richtung
J.DisplacementBC(name='Einzug Halter', createStepName='Uebermass', region=J.rootAssembly.sets['Planf_Halter'], u1=UNSET, u2=-0.2, ur3=UNSET, amplitude=UNSET, fixed=OFF, distributionType=UNIFORM, fieldName='', localCsys=None)
J.boundaryConditions['Einzug Halter'].deactivate('Vorspannkraft')

#Aufklappen der Spannhuelse
#J.DisplacementBC(amplitude=UNSET, createStepName='Initial', distributionType=UNIFORM, fieldName='', localCsys=None, name='Aufklappen verhindern', region= J.rootAssembly.sets['Spannband'], u1=SET, u2=UNSET, ur3=UNSET)
#J.boundaryConditions['Aufklappen verhindern'].deactivate('Aufklappen')

#Kontaktdefinition 0=reibungsfrei 1=reibungsbehaftet
reib=g_reib

J.ContactProperty('Normalkontakt')
J.interactionProperties['Normalkontakt'].NormalBehavior(allowSeparation=ON, constraintEnforcementMethod=DEFAULT, pressureOverclosure=HARD)
J.ContactProperty('Reibungsbehaftet')
J.interactionProperties['Reibungsbehaftet'].TangentialBehavior(dependencies=0, directionality=ISOTROPIC, elasticSlipStiffness=None, formulation=PENALTY, fraction=0.005, maximumElasticSlip=FRACTION, pressureDependency=OFF, shearStressLimit=None, slipRateDependency=OFF, table=((reib, ), ), temperatureDependency=OFF)
J.interactionProperties['Reibungsbehaftet'].NormalBehavior(allowSeparation=ON, constraintEnforcementMethod=DEFAULT, pressureOverclosure=HARD)

#, interferenceDistance=0.02
J.StdInitialization(name='Presspassung', overclosureType= INTERFERENCE,overclosureTolerance=0.2)
J.ContactStd(createStepName='Initial', name='Kontakt')
J.interactions['Kontakt'].includedPairs.setValuesInStep(stepName='Initial', useAllstar=ON)

n = 1 
if n == 0:
    J.interactions['Kontakt'].contactPropertyAssignments.appendInStep(assignments=((GLOBAL, SELF, 'Normalkontakt'), ), stepName='Initial')
    J.interactions['Kontakt'].initializationAssignments.appendInStep(assignments=((J.rootAssembly.surfaces['Kegel Aufnahme'], J.rootAssembly.surfaces['Kegel Halter'], 'Presspassung'), ), stepName='Initial')
    # J.interactions['Kontakt'].initializationAssignments.appendInStep(stepName='Initial', assignments=((J.rootAssembly.surfaces['Spannband Aufnahme'], J.rootAssembly.surfaces['Spannband Spannband'], 'Presspassung'), ))
    #J.SurfaceToSurfaceContactStd(adjustMethod=NONE, clearanceRegion=None, createStepName='Initial', datumAxis=None, initialClearance=OMIT, interactionProperty='Normalkontakt', master= J.rootAssembly.surfaces['Plananlage Aufnahme'], name= 'Plananlage', slave=  J.rootAssembly.surfaces['Plananlage Halter'], sliding= FINITE, thickness=ON)
    #J.SurfaceToSurfaceContactStd(adjustMethod=NONE, clearanceRegion=None, createStepName='Initial', datumAxis=None, initialClearance=OMIT, interactionProperty='Normalkontakt', master= J.rootAssembly.surfaces['Kegel Aufnahme'], name='Kegelkontakt', slave=  J.rootAssembly.surfaces['Kegel Halter'], sliding=FINITE, thickness=ON)
    #print('Bedingungen ohne Reibung abgeschlossen')
else:
    J.interactions['Kontakt'].contactPropertyAssignments.appendInStep(assignments=((GLOBAL, SELF, 'Reibungsbehaftet'), ), stepName='Initial')
    J.interactions['Kontakt'].initializationAssignments.appendInStep(assignments=((J.rootAssembly.surfaces['Kegel Aufnahme'], J.rootAssembly.surfaces['Kegel Halter'], 'Presspassung'), ), stepName='Initial')
    # J.interactions['Kontakt'].initializationAssignments.appendInStep(stepName='Initial', assignments=((J.rootAssembly.surfaces['Spannband Aufnahme'], J.rootAssembly.surfaces['Spannband Spannband'], 'Presspassung'), ))
    #mdb.models['Model-1'].boundaryConditions['Fixpunkt Spannband'].deactivate('Vorspannkraft')
    #J.SurfaceToSurfaceContactStd(adjustMethod=NONE, clearanceRegion=None, createStepName='Initial', datumAxis=None, initialClearance=OMIT, interactionProperty='Reibungsbehaftet', master= J.rootAssembly.surfaces['Plananlage Aufnahme'], name= 'Plananlage', slave=  J.rootAssembly.surfaces['Plananlage Halter'], sliding= FINITE, thickness=ON)
    #J.SurfaceToSurfaceContactStd(adjustMethod=NONE, clearanceRegion=None, createStepName='Initial', datumAxis=None, initialClearance=OMIT, interactionProperty='Reibungsbehaftet', master= J.rootAssembly.surfaces['Kegel Aufnahme'], name='Kegelkontakt', slave=  J.rootAssembly.surfaces['Kegel Halter'], sliding=FINITE, thickness=ON)
    #print('Bedingungen mit Reibung abgeschlossen')

##generateoutput
##generateoutput
mdb.models['Model-1'].FieldOutputRequest(name='Ausgang',  createStepName='Uebermass', variables=('S', 'E', 'U', 'CSTRESS', 'CSTRESSETOS', 'CDISP', 'CTANDIR', 'CFORCE', 'CNAREA', 'CSTATUS'))

J.rootAssembly.regenerate()

# if r == 0:
	# J.rootAssembly.features['Spannband-1'].suppress()
	# session.viewports['Viewport: 1'].assemblyDisplay.setValues(adaptiveMeshConstraints=OFF)
	# session.viewports['Viewport: 1'].assemblyDisplay.setValues(interactions=ON, constraints=ON, connectors=ON, engineeringFeatures=ON)
	# session.viewports['Viewport: 1'].assemblyDisplay.setValues(step='Initial')
	# J.interactions['Kontakt'].initializationAssignments.delete(indices=(0, ))
	# session.viewports['Viewport: 1'].assemblyDisplay.setValues(loads=ON, bcs=ON, predefinedFields=ON, interactions=OFF, constraints=OFF, engineeringFeatures=OFF)
	# J.loads['Zentrifugalkraft_Spannband'].suppress()
	# J.boundaryConditions['Fixpunkt Spannband'].suppress()
	# r11=mdb.models['Model-1'].rootAssembly.surfaces['Spannband Aufnahme']
	# r12=mdb.models['Model-1'].rootAssembly.surfaces['Spannband Spannband']
	# mdb.models['Model-1'].interactions['Kontakt'].excludedPairs.setValuesInStep(stepName='Initial', addPairs=((r11, r12), ))
	# print('Baugruppe ohne Spannband')
# else:
    # print('Test')

# J.rootAssembly.regenerate()




#Berechnungsjob anlegen
Jobname=g_Jobname

mdb.Job(atTime=None, contactPrint=OFF, description='', echoPrint=OFF,explicitPrecision=SINGLE, getMemoryFromAnalysis=True, historyPrint=OFF, memory=90, memoryUnits=PERCENTAGE, model='Model-1', modelPrint=OFF, name=Jobname, nodalOutputPrecision=SINGLE, queue=None, resultsFormat=ODB, scratch='', type=ANALYSIS, userSubroutine='', waitHours=0, waitMinutes=0)
 
#AutoBerechnung
mdb.jobs[Jobname].submit(consistencyChecking=OFF)
mdb.jobs[Jobname].waitForCompletion()
 
   

