#It is presumed that QuickField is already started, the problem coil.pbm is opened. import win32com.client def getQfForceX(x_position): #=============================================== #calculates force in QuickField #=============================================== #0. open model #1. check plunger position, move plunger #2. calculate force # 0. hook QuickField QF = win32com.client.Dispatch("QuickField.Application") pbm = QF.Problems.Item(1) #get the first opened Problem #need to convert from common IDocument to specific IProblem pbm1 = win32com.client.CastTo(pbm, "IProblem") pbm1.LoadModel() #load Model mdl = pbm1.Model #hook the Model #1. check plunger x-position, the cooridate of vertex "R" on plunger theVertex = mdl.Shapes.Vertices.GetLabeledAs("R","","").Item(1) #need to convert from common IShape to specific Vertex object theVertex1 = win32com.client.CastTo(theVertex, "Vertex") theXposition = theVertex1.Point.X #calculate x-coordinate of vertex "position" theVector = QF.PointXY(x_position - theXposition, 0) #calculate displacement vector #plunger consists of 2 blocks: "insulation" (external), "plunger_core" (internal) #if we move the external block then the internal block will be moved too theBlock = mdl.Shapes.Blocks.GetLabeledAs("","","insulation").Item(1)#select "plunger" block theBlock.Move(0, theVector) #move plunger. 0=qfShift, displacement #2. Solve problem mdl.Shapes.BuildMesh(True, False) #build mesh mdl.Save() #save model if (not(pbm1.Solved)): pbm1.SolveProblem() #solve problem pbm1.AnalyzeResults() #open results res = pbm1.Result #link to Result object theFieldWindow = res.GetFieldWindow(1) #hook Field window theContour = theFieldWindow.Contour #build integration contour #plunger consists of 2 blocks: "insulation" (external), "plunger_core" (internal) theContour.AddBlock1("insulation") #add to contour block "plunger" theContour.AddBlock1("plunger_core")#add to contour block "plunger_core" #calculate magnetic force integral theForce = res.GetIntegral(15, theContour).Value #15=qfInt_MaxwellForce return theForce.X #get x-component of force #=============================================== # main programm starts here #=============================================== #relay physical parameters x_out = 0.01 # pull out position. m x_in = 0.006 # pull in position, m m_mass = 0.0045 # plunger weight, kg k = 4.0 # spring constant, N/m x_spring_free = 0.015 # spring free would be position, m #initial conditions t_time = 0.0 # time, s x_position = x_out # plunger position, m v_speed = 0.0 # plunger speed, m/s dt_time_step = 0.005 #integration time step, s max_time = 0.2 # maximal integration time print("Time [s] Position [m] Speed[m/s]") while ((t_time < max_time) and (x_position > x_in)): theForce = getQfForceX(x_position) # electromagnetic force theForce += (x_spring_free - x_position) * k # add spring force v_speed += theForce / m_mass * dt_time_step # calculate acceleration and speed x_position += v_speed * dt_time_step # calculate plunger position t_time += dt_time_step # time increment print(t_time," ",x_position," ",v_speed) # output results