#It is presumed that QuickField is already started, the problem coil.pbm is opened. require 'win32ole' def getQfForceX(x_position) #=============================================== #calculates force in QuickField #=============================================== #0. open model #1. check plunger position, move plunger #2. calculate force # 0. hook QuickField qf = WIN32OLE.new("QuickField.Application") pbm = qf.Problems.Item(1) #get the first opened Problem pbm.LoadModel() #load Model mdl = pbm.Model #hook the Model #1. check plunger x-position, the cooridate of vertex "R" on plunger theVertex = mdl.Shapes.Vertices.LabeledAs("R","","").Item(1) theXposition = theVertex.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.LabeledAs("","","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(pbm.Solved)) pbm.SolveProblem() #solve problem end pbm.AnalyzeResults() #open results res = pbm.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 end #=============================================== # 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 puts "Time [s] Position [m] Speed[m/s]" while ((t_time < max_time) and (x_position > x_in)) do 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 puts "#{t_time} #{x_position} #{v_speed}" # output results end