ex14p.py -m amr-quad.mesh -rs 3 ex14p.py -m amr-hex.mesh ''' import sys from mfem.common.arg_parser import ArgParser from os.path import expanduser, join import numpy as np from mfem import path import mfem.par as mfem from mpi4py import MPI num_procs = MPI.COMM_WORLD.size myid = MPI.COMM_WORLD.rank parser = ArgParser(description='Ex14p') parser.add_argument('-m', '--mesh', default='star.mesh', action='store', type=str, help='Mesh file to use.') parser.add_argument( '-rs', '--refine-serial', action='store', default=-1, type=int, help="Number of times to refine the mesh uniformly before parallel") parser.add_argument( '-rp',
Description: This example code demonstrates the use of MFEM to define a simple finite element discretization of the Laplace problem -Delta u = 1 with homogeneous Dirichlet boundary conditions. ''' import sys from os.path import expanduser, join import numpy as np from mfem.common.arg_parser import ArgParser from mfem import path import mfem.ser as mfem parser = ArgParser(description='Ex1 (Laplace Problem)') parser.add_argument('-m', '--mesh', default='star.mesh', action='store', type=str, help='Mesh file to use.') parser.add_argument('-vis', '--visualization', action='store_true', help='Enable GLVis visualization') parser.add_argument( '-o', '--order', action='store', default=1,
u_coeff = mfem.GridFunctionCoefficient(u_alpha_gf) self.K.AddDomainIntegrator(mfem.DiffusionIntegrator(u_coeff)) self.K.Assemble(0) self.K.FormSystemMatrix(self.ess_tdof_list, self.Kmat) self.T = None class InitialTemperature(mfem.PyCoefficient): def EvalValue(self, x): xx = np.array(x) norm2 = float(np.sum(xx**2)) if norm2 < 0.5: return 2.0 return 1.0 parser = ArgParser(description='Ex16p') parser.add_argument('-m', '--mesh', default='star.mesh', action='store', type=str, help='Mesh file to use.') parser.add_argument( '-rs', '--refine-serial', action='store', default=2, type=int, help="Number of times to refine the mesh uniformly in serial") parser.add_argument( '-rp',
ex12p.py -m beam-quad-nurbs.mesh ex12p.py -m beam-hex-nurbs.mesh ''' import sys from os.path import expanduser, join import numpy as np from mfem import path from mfem.common.arg_parser import ArgParser import mfem.par as mfem from mpi4py import MPI num_procs = MPI.COMM_WORLD.size myid = MPI.COMM_WORLD.rank parser = ArgParser(description='Ex12 (linear elastisity)') parser.add_argument('-m', '--mesh', default='beam-tri.mesh', action='store', type=str, help='Mesh file to use.') parser.add_argument('-o', '--order', action='store', default=1, type=int, help="Finite element order (polynomial degree)") parser.add_argument("-n", "--num-eigs", action='store',
def initialize(self, inMeshObj=None, inMeshFile=None): # 2. Problem initialization self.parser = ArgParser(description='Based on MFEM Ex16p') self.parser.add_argument('-m', '--mesh', default='beam-tet.mesh', action='store', type=str, help='Mesh file to use.') self.parser.add_argument('-rs', '--refine-serial', action='store', default=1, type=int, help="Number of times to refine the mesh \ uniformly in serial") self.parser.add_argument('-rp', '--refine-parallel', action='store', default=0, type=int, help="Number of times to refine the mesh \ uniformly in parallel") self.parser.add_argument('-o', '--order', action='store', default=1, type=int, help="Finite element order (polynomial \ degree)") self.parser.add_argument( '-s', '--ode-solver', action='store', default=3, type=int, help='\n'.join([ "ODE solver: 1 - Backward Euler, 2 - SDIRK2, \ 3 - SDIRK3", "\t\t 11 - Forward Euler, \ 12 - RK2, 13 - RK3 SSP, 14 - RK4." ])) self.parser.add_argument('-t', '--t-final', action='store', default=20., type=float, help="Final time; start time is 0.") self.parser.add_argument("-dt", "--time-step", action='store', default=5e-3, type=float, help="Time step.") self.parser.add_argument("-v", "--viscosity", action='store', default=0.00, type=float, help="Viscosity coefficient.") self.parser.add_argument('-L', '--lmbda', action='store', default=1.e0, type=float, help='Lambda of Hooks law') self.parser.add_argument('-mu', '--shear-modulus', action='store', default=1.e0, type=float, help='Shear modulus for Hooks law') self.parser.add_argument('-rho', '--density', action='store', default=1.0, type=float, help='mass density') self.parser.add_argument('-vis', '--visualization', action='store_true', help='Enable GLVis visualization') self.parser.add_argument('-vs', '--visualization-steps', action='store', default=25, type=int, help="Visualize every n-th timestep.") args = self.parser.parse_args() self.ser_ref_levels = args.refine_serial self.par_ref_levels = args.refine_parallel self.order = args.order self.dt = args.time_step self.visc = args.viscosity self.t_final = args.t_final self.lmbda = args.lmbda self.mu = args.shear_modulus self.rho = args.density self.visualization = args.visualization self.ti = 1 self.vis_steps = args.visualization_steps self.ode_solver_type = args.ode_solver self.t = 0.0 self.last_step = False if self.myId == 0: self.parser.print_options(args) # 3. Reading mesh if inMeshObj is None: self.meshFile = inMeshFile if self.meshFile is None: self.meshFile = args.mesh self.mesh = mfem.Mesh(self.meshFile, 1, 1) else: self.mesh = inMeshObj self.dim = self.mesh.Dimension() print("Mesh dimension: %d" % self.dim) print("Number of vertices in the mesh: %d " % self.mesh.GetNV()) print("Number of elements in the mesh: %d " % self.mesh.GetNE()) # 4. Define the ODE solver used for time integration. # Several implicit singly diagonal implicit # Runge-Kutta (SDIRK) methods, as well as # explicit Runge-Kutta methods are available. if self.ode_solver_type == 1: self.ode_solver = BackwardEulerSolver() elif self.ode_solver_type == 2: self.ode_solver = mfem.SDIRK23Solver(2) elif self.ode_solver_type == 3: self.ode_solver = mfem.SDIRK33Solver() elif self.ode_solver_type == 11: self.ode_solver = ForwardEulerSolver() elif self.ode_solver_type == 12: self.ode_solver = mfem.RK2Solver(0.5) elif self.ode_solver_type == 13: self.ode_solver = mfem.RK3SSPSolver() elif self.ode_solver_type == 14: self.ode_solver = mfem.RK4Solver() elif self.ode_solver_type == 22: self.ode_solver = mfem.ImplicitMidpointSolver() elif self.ode_solver_type == 23: self.ode_solver = mfem.SDIRK23Solver() elif self.ode_solver_type == 24: self.ode_solver = mfem.SDIRK34Solver() else: print("Unknown ODE solver type: " + str(self.ode_solver_type)) exit # 5. Refine the mesh in serial to increase the # resolution. In this example we do # 'ser_ref_levels' of uniform refinement, where # 'ser_ref_levels' is a command-line parameter. for lev in range(self.ser_ref_levels): self.mesh.UniformRefinement() # 6. Define a parallel mesh by a partitioning of # the serial mesh. Refine this mesh further # in parallel to increase the resolution. Once the # parallel mesh is defined, the serial mesh can # be deleted. self.pmesh = mfem.ParMesh(MPI.COMM_WORLD, self.mesh) for lev in range(self.par_ref_levels): self.pmesh.UniformRefinement() # 7. Define the vector finite element space # representing the current and the # initial temperature, u_ref. self.fe_coll = mfem.H1_FECollection(self.order, self.dim) self.fespace = mfem.ParFiniteElementSpace(self.pmesh, self.fe_coll, self.dim) self.fe_size = self.fespace.GlobalTrueVSize() if self.myId == 0: print("FE Number of unknowns: " + str(self.fe_size)) true_size = self.fespace.TrueVSize() self.true_offset = mfem.intArray(3) self.true_offset[0] = 0 self.true_offset[1] = true_size self.true_offset[2] = 2 * true_size self.vx = mfem.BlockVector(self.true_offset) self.v_gf = mfem.ParGridFunction(self.fespace) self.v_gfbnd = mfem.ParGridFunction(self.fespace) self.x_gf = mfem.ParGridFunction(self.fespace) self.x_gfbnd = mfem.ParGridFunction(self.fespace) self.x_ref = mfem.ParGridFunction(self.fespace) self.pmesh.GetNodes(self.x_ref) # 8. Set the initial conditions for u. #self.velo = InitialVelocity(self.dim) self.velo = velBCs(self.dim) #self.deform = InitialDeformation(self.dim) self.deform = defBCs(self.dim) self.v_gf.ProjectCoefficient(self.velo) self.v_gfbnd.ProjectCoefficient(self.velo) self.x_gf.ProjectCoefficient(self.deform) self.x_gfbnd.ProjectCoefficient(self.deform) #self.v_gf.GetTrueDofs(self.vx.GetBlock(0)); #self.x_gf.GetTrueDofs(self.vx.GetBlock(1)); # setup boundary-conditions self.xess_bdr = mfem.intArray( self.fespace.GetMesh().bdr_attributes.Max()) self.xess_bdr.Assign(0) self.xess_bdr[0] = 1 self.xess_bdr[1] = 1 self.xess_tdof_list = intArray() self.fespace.GetEssentialTrueDofs(self.xess_bdr, self.xess_tdof_list) #print('True x essential BCs are') #self.xess_tdof_list.Print() self.vess_bdr = mfem.intArray( self.fespace.GetMesh().bdr_attributes.Max()) self.vess_bdr.Assign(0) self.vess_bdr[0] = 1 self.vess_bdr[1] = 1 self.vess_tdof_list = intArray() self.fespace.GetEssentialTrueDofs(self.vess_bdr, self.vess_tdof_list) #print('True v essential BCs are') #self.vess_tdof_list.Print() # 9. Initialize the stiffness operator self.oper = StiffnessOperator(self.fespace, self.lmbda, self.mu, self.rho, self.visc, self.vess_tdof_list, self.vess_bdr, self.xess_tdof_list, self.xess_bdr, self.v_gfbnd, self.x_gfbnd, self.deform, self.velo, self.vx) # 10. Setting up file output self.smyid = '{:0>2d}'.format(self.myId) # initializing ode solver self.ode_solver.Init(self.oper)
import mfem.par as mfem from mfem.common.arg_parser import ArgParser from os.path import expanduser, join import numpy as np from numpy import sqrt, pi, cos, sin, hypot, arctan2 from scipy.special import erfc from mfem import path from mfem.par import intArray, add_vector from mpi4py import MPI num_procs = MPI.COMM_WORLD.size myid = MPI.COMM_WORLD.rank parser = ArgParser(description='Ex10p') parser.add_argument('-m', '--mesh', default='beam-quad.mesh', action='store', type=str, help='Mesh file to use.') parser.add_argument( '-rs', '--refine-serial', action='store', default=2, type=int, help="Number of times to refine the mesh uniformly before parallel") parser.add_argument( '-rp',
self.output.flush() def send_solution(self, mesh, x): if self.output is None: return self.output.send_solution(mesh, x) def send_text(self, x): if self.output is None: return self.output.send_text(x) def flush(self): if self.output is None: return self.output.flush() parser = ArgParser(description='Ex17') parser.add_argument('-m', '--mesh', default='beam-tri.mesh', action='store', type=str, help='Mesh file to use.') parser.add_argument( '-rs', '--refine-serial', action='store', default=-1, type=int, help="Number of times to refine the mesh uniformly before parallel") parser.add_argument( '-rp',
str(self.win_y + self.stride_y*(sid/self.win_nx)) + ' ' + str(self.win_w) + ' ' + str(self.win_h)) self.output.send_text(command) self.output.flush() def send_solution(self, mesh, x): if self.output is None: return self.output.send_solution(mesh, x) def send_text(self, x): if self.output is None: return self.output.send_text(x) def flush(self): if self.output is None: return self.output.flush() parser = ArgParser(description='Ex17') parser.add_argument('-m', '--mesh', default = 'beam-tri.mesh', action = 'store', type = str, help='Mesh file to use.') parser.add_argument('-r', '--refine', action = 'store', default = -1, type=int, help = "Number of times to refine the mesh uniformly, -1 for auto.") parser.add_argument('-o', '--order', action = 'store', default = 1, type=int, help = "Finite element order (polynomial degree)"); parser.add_argument('-a', '--alpha', action = 'store', default = -1.0, type=float, help = '\n'.join(["One of the two DG penalty parameters, typically +1/-1." " See the documentation of class DGElasticityIntegrator."])) parser.add_argument('-k', '--kappa',
# 11. Recover the solution as a finite element grid function. a.RecoverFEMSolution(X, b, x) # 12. Save the refined mesh and the solution. This output can be viewed later # using GLVis: "glvis -m refined.mesh -g sol.gf". mesh.Print('refined.mesh', 8) x.Save('sol.gf', 8) #13. Send the solution by socket to a GLVis server. if (visualization): sol_sock = mfem.socketstream("localhost", 19916) sol_sock.precision(8) sol_sock.send_solution(mesh, x) if __name__ == "__main__": parser = ArgParser(description='Ex1 (Laplace Problem)') parser.add_argument('-m', '--mesh', default='star.mesh', action='store', type=str, help='Mesh file to use.') parser.add_argument('-vis', '--visualization', action='store_true', help='Enable GLVis visualization') parser.add_argument( '-o', '--order', action='store', default=1,
ex2p.py -m beam-tri.mesh -o 2 -sys ex2p.py -m beam-quad.mesh -o 3 -elast ex2p.py -m beam-quad.mesh -o 3 -sc ''' from mfem import path from mfem.common.arg_parser import ArgParser import mfem.par as mfem from mpi4py import MPI num_procs = MPI.COMM_WORLD.size myid = MPI.COMM_WORLD.rank import sys from os.path import expanduser, join import numpy as np parser = ArgParser(description='Ex2 (linear elastisity)') parser.add_argument('-m', '--mesh', default='beam-tri.mesh', action='store', type=str, help='Mesh file to use.') parser.add_argument('-vis', '--visualization', action='store_true', help='Enable GLVis visualization') parser.add_argument( '-o', '--order', action='store', default=1,
refinement loop. See c++ version in the MFEM library for more detail ''' from mfem import path from mfem.common.arg_parser import ArgParser import mfem.ser as mfem from mfem.ser import intArray from os.path import expanduser, join import numpy as np from numpy import sqrt, pi, cos, sin, hypot, arctan2 from scipy.special import erfc # Equation constant parameters.(using globals to share them with ex18_common) import ex18_common parser = ArgParser(description='Ex18') parser.add_argument('-m', '--mesh', default='periodic-square.mesh', action='store', type=str, help='Mesh file to use.') parser.add_argument( '-p', '--problem', action='store', default=1, type=int, help='Problem setup to use. See options in velocity_function().') parser.add_argument('-r', '--refine',
ex4p.py -m amr-hex.mesh -o 2 -hb ex4p.py -m star-surf.mesh -o 3 -hb ''' from mfem import path from mfem.common.arg_parser import ArgParser import mfem.par as mfem from mpi4py import MPI num_procs = MPI.COMM_WORLD.size myid = MPI.COMM_WORLD.rank import sys from os.path import expanduser, join import numpy as np from numpy import sin, array, cos parser = ArgParser(description='Ex4 ') parser.add_argument('-m', '--mesh', default='star.mesh', action='store', type=str, help='Mesh file to use.') parser.add_argument('-o', '--order', action='store', default=1, type=int, help="Finite element order (polynomial degree)") parser.add_argument('-no-bc', '--dont-impose-bc', action='store_false',
ex11p.py -m mobius-strip.mesh -n 8 ex11p.py -m klein-bottle.mesh -n 10 ''' import sys from os.path import expanduser, join import numpy as np from mfem import path from mfem.common.arg_parser import ArgParser import mfem.par as mfem from mpi4py import MPI num_procs = MPI.COMM_WORLD.size myid = MPI.COMM_WORLD.rank parser = ArgParser(description='Ex2 (linear elastisity)') parser.add_argument('-m', '--mesh', default='beam-tri.mesh', action='store', type=str, help='Mesh file to use.') parser.add_argument( '-rs', '--refine-serial', default=2, action='store', type=int, help="Number of times to refine the mesh uniformly in serial.") parser.add_argument(
ex8p.py -m star-surf.mesh -o 2 ''' import sys from os.path import expanduser, join import numpy as np from numpy import sin, cos, exp, sqrt from mfem.common.arg_parser import ArgParser from mfem import path import mfem.par as mfem from mpi4py import MPI num_procs = MPI.COMM_WORLD.size myid = MPI.COMM_WORLD.rank parser = ArgParser(description='Ex8p') parser.add_argument('-m', '--mesh', default='star.mesh', action='store', type=str, help='Mesh file to use.') parser.add_argument('-o', '--order', action='store', default=1, type=int, help="Finite element order (polynomial degree)") parser.add_argument('-vis', '--visualization', action='store_true',
from mfem import path import mfem.par as mfem from mfem.par import intArray, add_vector, Add from os.path import expanduser, join import numpy as np from numpy import sqrt, pi, cos, sin, hypot, arctan2 from scipy.special import erfc # 1. Initialize MPI.from mpi4py import MPI from mpi4py import MPI num_procs = MPI.COMM_WORLD.size myid = MPI.COMM_WORLD.rank parser = ArgParser(description='Ex19p') parser.add_argument('-m', '--mesh', default='beam-hex.mesh', action='store', type=str, help='Mesh file to use.') parser.add_argument( '-rs', '--refine-serial', action='store', default=0, type=int, help="Number of times to refine the mesh uniformly in serial") parser.add_argument( '-rp',
''' import sys from mfem.common.arg_parser import ArgParser from os.path import expanduser, join import numpy as np from numpy import cos, sin, pi, exp, sqrt, arctan from mfem import path import mfem.par as mfem from mpi4py import MPI num_procs = MPI.COMM_WORLD.size myid = MPI.COMM_WORLD.rank parser = ArgParser(description='Ex15p') parser.add_argument('-m', '--mesh', default='star-hilbert.mesh', action='store', type=str, help='Mesh file to use.') parser.add_argument( "-p", "--problem", action='store', default=0, type=int, help="Problem setup to use: 0 = spherical front, 1 = ball.") parser.add_argument("-n", "--nfeatures",
names = [os.path.join(example_dir, x) for x in names] names = [x for x in names if os.access(x, os.X_OK)] pairs = [(num, name) for num, name in zip(nums, names)] names = [name for num, name in sorted(pairs)] return names def print_help(self): print("python test.py -np -v") if __name__=="__main__": script_path = sys.path[0] # the location of this file from mfem.common.arg_parser import ArgParser parser = ArgParser(description='test') parser.add_argument('-np', default = 2, action = 'store', type = int, help='Number of processes for MPI') parser.add_argument('-verbose', action = 'store_true', default = False, help='Verbose print out') parser.add_argument('-parallel', action = 'store_true', help='Test parallel version') parser.add_argument('-serial', action = 'store_true', help='Test serial version') parser.add_argument('-clean', action = 'store_true', default = False, help='Test serial version')
pairs = [(num, name) for num, name in zip(nums, names)] names = [name for num, name in sorted(pairs)] return names def print_help(self): print("python test.py -np -v") if __name__ == "__main__": script_path = sys.path[0] # the location of this file from mfem.common.arg_parser import ArgParser parser = ArgParser(description='test') parser.add_argument('-np', default=2, action='store', type=long, help='Number of processes for MPI') parser.add_argument('-verbose', action='store_true', default=False, help='Verbose print out') parser.add_argument('-parallel', action='store', default=True, help='Test parallel version') parser.add_argument('-serial', action='store',
class mfemSim(object): def __init__(self, desc=''): # 1. Object initialization self.desc = desc self.myId = MPI.COMM_WORLD.rank self.nProc = MPI.COMM_WORLD.size def __del__(self): # do nothing for now self.finalize() print('MFEM problem is deleted') # problem initialization def initialize(self, inMeshObj=None, inMeshFile=None): # 2. Problem initialization self.parser = ArgParser(description='Based on MFEM Ex16p') self.parser.add_argument('-m', '--mesh', default='beam-tet.mesh', action='store', type=str, help='Mesh file to use.') self.parser.add_argument('-rs', '--refine-serial', action='store', default=1, type=int, help="Number of times to refine the mesh \ uniformly in serial") self.parser.add_argument('-rp', '--refine-parallel', action='store', default=0, type=int, help="Number of times to refine the mesh \ uniformly in parallel") self.parser.add_argument('-o', '--order', action='store', default=1, type=int, help="Finite element order (polynomial \ degree)") self.parser.add_argument( '-s', '--ode-solver', action='store', default=3, type=int, help='\n'.join([ "ODE solver: 1 - Backward Euler, 2 - SDIRK2, \ 3 - SDIRK3", "\t\t 11 - Forward Euler, \ 12 - RK2, 13 - RK3 SSP, 14 - RK4." ])) self.parser.add_argument('-t', '--t-final', action='store', default=20., type=float, help="Final time; start time is 0.") self.parser.add_argument("-dt", "--time-step", action='store', default=5e-3, type=float, help="Time step.") self.parser.add_argument("-v", "--viscosity", action='store', default=0.00, type=float, help="Viscosity coefficient.") self.parser.add_argument('-L', '--lmbda', action='store', default=1.e0, type=float, help='Lambda of Hooks law') self.parser.add_argument('-mu', '--shear-modulus', action='store', default=1.e0, type=float, help='Shear modulus for Hooks law') self.parser.add_argument('-rho', '--density', action='store', default=1.0, type=float, help='mass density') self.parser.add_argument('-vis', '--visualization', action='store_true', help='Enable GLVis visualization') self.parser.add_argument('-vs', '--visualization-steps', action='store', default=25, type=int, help="Visualize every n-th timestep.") args = self.parser.parse_args() self.ser_ref_levels = args.refine_serial self.par_ref_levels = args.refine_parallel self.order = args.order self.dt = args.time_step self.visc = args.viscosity self.t_final = args.t_final self.lmbda = args.lmbda self.mu = args.shear_modulus self.rho = args.density self.visualization = args.visualization self.ti = 1 self.vis_steps = args.visualization_steps self.ode_solver_type = args.ode_solver self.t = 0.0 self.last_step = False if self.myId == 0: self.parser.print_options(args) # 3. Reading mesh if inMeshObj is None: self.meshFile = inMeshFile if self.meshFile is None: self.meshFile = args.mesh self.mesh = mfem.Mesh(self.meshFile, 1, 1) else: self.mesh = inMeshObj self.dim = self.mesh.Dimension() print("Mesh dimension: %d" % self.dim) print("Number of vertices in the mesh: %d " % self.mesh.GetNV()) print("Number of elements in the mesh: %d " % self.mesh.GetNE()) # 4. Define the ODE solver used for time integration. # Several implicit singly diagonal implicit # Runge-Kutta (SDIRK) methods, as well as # explicit Runge-Kutta methods are available. if self.ode_solver_type == 1: self.ode_solver = BackwardEulerSolver() elif self.ode_solver_type == 2: self.ode_solver = mfem.SDIRK23Solver(2) elif self.ode_solver_type == 3: self.ode_solver = mfem.SDIRK33Solver() elif self.ode_solver_type == 11: self.ode_solver = ForwardEulerSolver() elif self.ode_solver_type == 12: self.ode_solver = mfem.RK2Solver(0.5) elif self.ode_solver_type == 13: self.ode_solver = mfem.RK3SSPSolver() elif self.ode_solver_type == 14: self.ode_solver = mfem.RK4Solver() elif self.ode_solver_type == 22: self.ode_solver = mfem.ImplicitMidpointSolver() elif self.ode_solver_type == 23: self.ode_solver = mfem.SDIRK23Solver() elif self.ode_solver_type == 24: self.ode_solver = mfem.SDIRK34Solver() else: print("Unknown ODE solver type: " + str(self.ode_solver_type)) exit # 5. Refine the mesh in serial to increase the # resolution. In this example we do # 'ser_ref_levels' of uniform refinement, where # 'ser_ref_levels' is a command-line parameter. for lev in range(self.ser_ref_levels): self.mesh.UniformRefinement() # 6. Define a parallel mesh by a partitioning of # the serial mesh. Refine this mesh further # in parallel to increase the resolution. Once the # parallel mesh is defined, the serial mesh can # be deleted. self.pmesh = mfem.ParMesh(MPI.COMM_WORLD, self.mesh) for lev in range(self.par_ref_levels): self.pmesh.UniformRefinement() # 7. Define the vector finite element space # representing the current and the # initial temperature, u_ref. self.fe_coll = mfem.H1_FECollection(self.order, self.dim) self.fespace = mfem.ParFiniteElementSpace(self.pmesh, self.fe_coll, self.dim) self.fe_size = self.fespace.GlobalTrueVSize() if self.myId == 0: print("FE Number of unknowns: " + str(self.fe_size)) true_size = self.fespace.TrueVSize() self.true_offset = mfem.intArray(3) self.true_offset[0] = 0 self.true_offset[1] = true_size self.true_offset[2] = 2 * true_size self.vx = mfem.BlockVector(self.true_offset) self.v_gf = mfem.ParGridFunction(self.fespace) self.v_gfbnd = mfem.ParGridFunction(self.fespace) self.x_gf = mfem.ParGridFunction(self.fespace) self.x_gfbnd = mfem.ParGridFunction(self.fespace) self.x_ref = mfem.ParGridFunction(self.fespace) self.pmesh.GetNodes(self.x_ref) # 8. Set the initial conditions for u. #self.velo = InitialVelocity(self.dim) self.velo = velBCs(self.dim) #self.deform = InitialDeformation(self.dim) self.deform = defBCs(self.dim) self.v_gf.ProjectCoefficient(self.velo) self.v_gfbnd.ProjectCoefficient(self.velo) self.x_gf.ProjectCoefficient(self.deform) self.x_gfbnd.ProjectCoefficient(self.deform) #self.v_gf.GetTrueDofs(self.vx.GetBlock(0)); #self.x_gf.GetTrueDofs(self.vx.GetBlock(1)); # setup boundary-conditions self.xess_bdr = mfem.intArray( self.fespace.GetMesh().bdr_attributes.Max()) self.xess_bdr.Assign(0) self.xess_bdr[0] = 1 self.xess_bdr[1] = 1 self.xess_tdof_list = intArray() self.fespace.GetEssentialTrueDofs(self.xess_bdr, self.xess_tdof_list) #print('True x essential BCs are') #self.xess_tdof_list.Print() self.vess_bdr = mfem.intArray( self.fespace.GetMesh().bdr_attributes.Max()) self.vess_bdr.Assign(0) self.vess_bdr[0] = 1 self.vess_bdr[1] = 1 self.vess_tdof_list = intArray() self.fespace.GetEssentialTrueDofs(self.vess_bdr, self.vess_tdof_list) #print('True v essential BCs are') #self.vess_tdof_list.Print() # 9. Initialize the stiffness operator self.oper = StiffnessOperator(self.fespace, self.lmbda, self.mu, self.rho, self.visc, self.vess_tdof_list, self.vess_bdr, self.xess_tdof_list, self.xess_bdr, self.v_gfbnd, self.x_gfbnd, self.deform, self.velo, self.vx) # 10. Setting up file output self.smyid = '{:0>2d}'.format(self.myId) # initializing ode solver self.ode_solver.Init(self.oper) # stepping in time def step(self, nStep): # initialization self.last_step = False tFinal = self.t + self.dt * nStep if (self.t + self.dt >= tFinal - self.dt / 2): self.last_step = True # loop for time stepping while not self.last_step: if (self.t + self.dt >= tFinal - self.dt / 2): self.last_step = True # update BCs self.deform.SetTime(self.t) self.velo.SetTime(self.t) # step in time self.t, self.dt = self.ode_solver.Step(self.vx, self.t, self.dt) # writing to output if (self.last_step or (self.ti % self.vis_steps) == 0): if self.myId == 0: print("Running step " + str(self.ti) + ", t = " + str(self.t)) self.oper.SetBCs() self.v_gf.Distribute(self.vx.GetBlock(0)) self.x_gf.Distribute(self.vx.GetBlock(1)) self.solToFile() # preparing for the next time step self.ti = self.ti + 1 self.oper.SetBCs() # sets up simulation agent after a major update # such as mass matrix or stiffness matrix def postUpdate(self): self.oper.RecoverSln(self.v_gf, self.x_gf) # finalize the simulation def finalize(self): print('mfem simulation is finalizing') # writes solution to file def solToFile(self): # print mesh to vtk format sol_name = "fe-sol-" + self.smyid \ + '-' + str(self.ti).zfill(6) + '.vtu' #sol_name = "fe-sol-" + self.smyid \ #+ '-' + str(self.ti).zfill(6) #self.vtkStream = open(sol_name+'.vtk', 'w') #self.pmesh.PrintVTK(self.vtkStream, 1, 1) fieldName = 'V' #self.v_gf.SaveVTK(self.vtkStream, str(fieldName), 1) fieldName = 'U' #self.x_gf.SaveVTK(self.vtkStream, str(fieldName), 1) #self.vtkStream.close() meshToVtu(self.pmesh, sol_name, (self.v_gf, self.x_gf), ('V', 'U')) # get solution vector def getSolVec(self, name): ret = [] name = name.lower() if name == 'displacement': sz = self.vx.GetBlock(1).Size() ret = np.zeros(sz, np.double) for i in range(sz): ret[i] = self.vx.GetBlock(1)[i] elif name == 'velocity': sz = self.vx.GetBlock(0).Size() ret = np.zeros(sz, np.double) for i in range(sz): ret[i] = self.vx.GetBlock(0)[i] else: raise ValueError('Requested solution is invalid.') return (ret)