model build run class

这个提交包含在:
John Hartley
2019-08-01 12:00:00 +01:00
父节点 d74375b0fc
当前提交 cebde1c97a

查看文件

@@ -78,36 +78,30 @@ class Printer():
if self.printing: if self.printing:
print(str) print(str)
class ModelBuildRun:
def __init__(self, solver, sim_config, model_config):
self.solver = solver
self.sim_config = sim_config
self.model_config = model_config
self.G = solver.get_G()
def run_model(solver, sim_config, model_config): def tm_grid_update(self):
"""Runs a model - processes the input file; builds the Yee cells; calculates update coefficients; runs main FDTD loop. if '2D TMx' in self.model_config.mode:
self.G.tmx()
elif '2D TMy' in self.model_config.mode:
self.G.tmy()
elif '2D TMz' in self.model_config.mode:
self.G.tmz()
Args: def build_geometry(self):
args (dict): Namespace with command line arguments model_config = self.model_config
currentmodelrun (int): Current model run number. sim_config = self.sim_config
modelend (int): Number of last model to run. G = self.G
numbermodelruns (int): Total number of model runs.
inputfile (object): File object for the input file.
usernamespace (dict): Namespace that can be accessed by user
in any Python code blocks in input file.
Returns:
tsolve (int): Length of time (seconds) of main FDTD calculations
"""
printer = Printer(sim_config) printer = Printer(sim_config)
# Monitor memory usage
p = psutil.Process()
# Normal model reading/building process; bypassed if geometry information to be reused
if not sim_config.geometry_fixed:
printer.print(model_config.next_model) printer.print(model_config.next_model)
G = solver.get_G()
# api for multiple scenes / model runs # api for multiple scenes / model runs
try: try:
scene = model_config.get_scene() scene = model_config.get_scene()
@@ -141,26 +135,8 @@ def run_model(solver, sim_config, model_config):
pbar.update() pbar.update()
pbar.close() pbar.close()
# Add PEC boundaries to invariant direction in 2D modes # update grid for tm modes
# N.B. 2D modes are a single cell slice of 3D grid tm_grid_update(model_config)
if '2D TMx' in config.mode:
# Ey & Ez components
G.ID[1, 0, :, :] = 0
G.ID[1, 1, :, :] = 0
G.ID[2, 0, :, :] = 0
G.ID[2, 1, :, :] = 0
elif '2D TMy' in config.mode:
# Ex & Ez components
G.ID[0, :, 0, :] = 0
G.ID[0, :, 1, :] = 0
G.ID[2, :, 0, :] = 0
G.ID[2, :, 1, :] = 0
elif '2D TMz' in config.mode:
# Ex & Ey components
G.ID[0, :, :, 0] = 0
G.ID[0, :, :, 1] = 0
G.ID[1, :, :, 0] = 0
G.ID[1, :, :, 1] = 0
# Process any voltage sources (that have resistance) to create a new # Process any voltage sources (that have resistance) to create a new
# material at the source location # material at the source location
@@ -224,9 +200,8 @@ def run_model(solver, sim_config, model_config):
# set the dispersive update functions based on the model configuration # set the dispersive update functions based on the model configuration
solver.updates.set_dispersive_updates(model_config) solver.updates.set_dispersive_updates(model_config)
def reuse_geometry(self):
# If geometry information to be reused between model runs printer = Printer(model_config)
else:
inputfilestr = '\n--- Model {}/{}, input file (not re-processed, i.e. geometry fixed): {}'.format(currentmodelrun, modelend, model_config.input_file_path) inputfilestr = '\n--- Model {}/{}, input file (not re-processed, i.e. geometry fixed): {}'.format(currentmodelrun, modelend, model_config.input_file_path)
printer.print(Fore.GREEN + '{} {}\n'.format(model_config.inputfilestr, '-' * (get_terminal_width() - 1 - len(model_config.inputfilestr))) + Style.RESET_ALL) printer.print(Fore.GREEN + '{} {}\n'.format(model_config.inputfilestr, '-' * (get_terminal_width() - 1 - len(model_config.inputfilestr))) + Style.RESET_ALL)
@@ -237,6 +212,30 @@ def run_model(solver, sim_config, model_config):
for pml in G.pmls: for pml in G.pmls:
pml.initialise_field_arrays() pml.initialise_field_arrays()
def build(self):
"""Runs a model - processes the input file; builds the Yee cells; calculates update coefficients; runs main FDTD loop.
Args:
args (dict): Namespace with command line arguments
currentmodelrun (int): Current model run number.
modelend (int): Number of last model to run.
numbermodelruns (int): Total number of model runs.
inputfile (object): File object for the input file.
usernamespace (dict): Namespace that can be accessed by user
in any Python code blocks in input file.
Returns:
tsolve (int): Length of time (seconds) of main FDTD calculations
"""
# Monitor memory usage
p = psutil.Process()
# Normal model reading/building process; bypassed if geometry information to be reused
if not self.sim_config.geometry_fixed:
self.build_geometry()
else:
self.reuse_geometry()
# Adjust position of simple sources and receivers if required # Adjust position of simple sources and receivers if required
if G.srcsteps[0] != 0 or G.srcsteps[1] != 0 or G.srcsteps[2] != 0: if G.srcsteps[0] != 0 or G.srcsteps[1] != 0 or G.srcsteps[2] != 0:
for source in itertools.chain(G.hertziandipoles, G.magneticdipoles): for source in itertools.chain(G.hertziandipoles, G.magneticdipoles):
@@ -273,8 +272,9 @@ def run_model(solver, sim_config, model_config):
if args.geometry_only: if args.geometry_only:
tsolve = 0 tsolve = 0
# Run simulation
else: def run_model(self):
# Check and set output directory and filename # Check and set output directory and filename
if not os.path.isdir(config.outputfilepath): if not os.path.isdir(config.outputfilepath):
os.mkdir(config.outputfilepath) os.mkdir(config.outputfilepath)
@@ -311,14 +311,9 @@ def run_model(solver, sim_config, model_config):
printer.print('\nMemory (RAM) used: ~{}{}'.format(human_size(p.memory_full_info().uss), memGPU)) printer.print('\nMemory (RAM) used: ~{}{}'.format(human_size(p.memory_full_info().uss), memGPU))
printer.print('Solving time [HH:MM:SS]: {}'.format(datetime.timedelta(seconds=tsolve))) printer.print('Solving time [HH:MM:SS]: {}'.format(datetime.timedelta(seconds=tsolve)))
# If geometry information to be reused between model runs then FDTDGrid
# class instance must be global so that it persists
if not args.geometry_fixed:
del G
return tsolve return tsolve
def solve_cpu(solver, sim_config, model_config): def solve_cpu(solver, sim_config, model_config):
""" """
Solving using FDTD method on CPU. Parallelised using Cython (OpenMP) for Solving using FDTD method on CPU. Parallelised using Cython (OpenMP) for