本文整理汇总了Python中sfepy.base.base.Struct.to_dict方法的典型用法代码示例。如果您正苦于以下问题:Python Struct.to_dict方法的具体用法?Python Struct.to_dict怎么用?Python Struct.to_dict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sfepy.base.base.Struct
的用法示例。
在下文中一共展示了Struct.to_dict方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process_conf
# 需要导入模块: from sfepy.base.base import Struct [as 别名]
# 或者: from sfepy.base.base.Struct import to_dict [as 别名]
def process_conf(cls, conf, kwargs):
"""
Process configuration parameters.
"""
get = make_get_conf(conf, kwargs)
if len(cls._parameters) and cls._parameters[0][0] != 'name':
options = Solver._parameters + cls._parameters
else:
options = cls._parameters
opts = Struct()
allow_extra = False
for name, _, default, required, _ in options:
if name == '*':
allow_extra = True
continue
msg = ('missing "%s" in options!' % name) if required else None
setattr(opts, name, get(name, default, msg))
if allow_extra:
all_keys = set(conf.to_dict().keys())
other = all_keys.difference(opts.to_dict().keys())
for name in other:
setattr(opts, name, get(name, None, None))
return opts
示例2: solve_direct
# 需要导入模块: from sfepy.base.base import Struct [as 别名]
# 或者: from sfepy.base.base.Struct import to_dict [as 别名]
def solve_direct(conf, options, problem=None, step_hook=None,
post_process_hook=None, post_process_hook_final=None,
pre_process_hook=None, nls_status=None):
"""Generic (simple) problem solver."""
if problem is None:
is_eqs = not options.solve_not
problem = ProblemDefinition.from_conf(conf, init_equations=is_eqs)
problem.setup_default_output(conf, options)
if pre_process_hook is not None: # User pre_processing.
pre_process_hook(problem)
ofn_trunk = problem.ofn_trunk
save_names = Struct( ebc = None, regions = None,
regions_as_groups = None, field_meshes = None,
region_field_meshes = None )
if options.save_ebc:
save_names.ebc = ofn_trunk + '_ebc.vtk'
if options.save_regions:
save_names.regions = ofn_trunk + '_region'
if options.save_regions_as_groups:
save_names.regions_as_groups = ofn_trunk + '_regions'
if options.save_field_meshes:
save_names.field_meshes = ofn_trunk + '_field'
is_extra_save = False
for name, val in save_names.to_dict().iteritems():
if val is not None:
is_extra_save = True
break
if is_extra_save:
save_only( conf, save_names, problem=problem )
if options.solve_not:
return None, None, None
if hasattr( conf.options, 'ts' ):
##
# Time-dependent problem.
state = solve_evolutionary_op(problem, options,
step_hook=step_hook,
post_process_hook=post_process_hook,
nls_status=nls_status)
else:
##
# Stationary problem.
state = solve_stationary_op(problem, options,
post_process_hook=post_process_hook,
nls_status=nls_status)
if post_process_hook_final is not None: # User postprocessing.
post_process_hook_final(problem, state)
return problem, state
示例3: process_conf
# 需要导入模块: from sfepy.base.base import Struct [as 别名]
# 或者: from sfepy.base.base.Struct import to_dict [as 别名]
def process_conf(conf, kwargs):
"""
Missing items are left to SciPy defaults. Unused options are ignored.
Besides 'i_max', use option names according to scipy.optimize
function arguments. The 'i_max' translates either to 'maxiter'
or 'maxfun' as available.
Example configuration::
solver_1 = {
'name' : 'fmin',
'kind' : 'nls.scipy_fmin_like',
'method' : 'bfgs',
'i_max' : 10,
'verbose' : True,
'gtol' : 1e-7
}
"""
get = make_get_conf(conf, kwargs)
common = OptimizationSolver.process_conf(conf)
opts = Struct(method=get('method', 'fmin'),
i_max=get('i_max', 10),
verbose=get('verbose', False)) + common
other = {}
keys = opts.to_dict().keys()
for key, val in conf.to_dict().iteritems():
if key not in keys:
other[key] = val
return opts + Struct(**other)
示例4: PDESolverApp
# 需要导入模块: from sfepy.base.base import Struct [as 别名]
# 或者: from sfepy.base.base.Struct import to_dict [as 别名]
#.........这里部分代码省略.........
self.app_options.use_equations)
def setup_output_info(self, problem, options):
"""Modifies both problem and options!"""
if options.output_filename_trunk is None:
filename_mesh = self.conf.filename_mesh
if isinstance(filename_mesh, MeshIO):
ofn_trunk = filename_mesh.get_filename_trunk()
else:
ofn_trunk = io.get_trunk(filename_mesh)
options.output_filename_trunk = ofn_trunk
else:
ofn_trunk = options.output_filename_trunk
if hasattr(options, 'output_format') \
and (options.output_format is not None):
output_format = options.output_format
else:
output_format = self.app_options.output_format
problem.setup_output(output_filename_trunk=ofn_trunk,
output_dir=self.app_options.output_dir,
output_format=output_format,
file_per_var=self.app_options.file_per_var,
linearization=self.app_options.linearization)
def call(self, nls_status=None):
problem = self.problem
options = self.options
opts = self.app_options
if self.pre_process_hook is not None: # User pre_processing.
self.pre_process_hook(problem)
ofn_trunk = problem.ofn_trunk
self.save_names = Struct(ebc=ofn_trunk + '_ebc.vtk'
if options.save_ebc else None,
regions=ofn_trunk + '_region'
if options.save_regions else None,
regions_as_groups=ofn_trunk + '_regions'
if options.save_regions_as_groups else None,
field_meshes=ofn_trunk + '_field'
if options.save_field_meshes else None)
if any(self.save_names.to_dict().values()):
save_only(self.conf, self.save_names, problem=problem)
if options.solve_not:
return None, None, None
if hasattr(self.conf.options, 'ts'):
time_solver = problem.get_time_solver()
if time_solver.name == 'ts.simple': # Implicit time stepping.
time_solver.set_step_fun(make_implicit_step,
(problem, nls_status))
else: # Explicit time stepping.
mass = MassOperator(problem, time_solver.conf)
time_solver.set_step_fun(make_explicit_step,
(problem, mass, nls_status))
state = solve_evolutionary(problem, time_solver,
save_results=opts.save_results,
step_hook=self.step_hook,
post_process_hook=self.post_process_hook)
else: # Stationary problem.
state = solve_stationary(problem,
save_results=opts.save_results,
post_process_hook=self.post_process_hook,
nls_status=nls_status)
if self.post_process_hook_final is not None: # User postprocessing.
self.post_process_hook_final(problem, state)
return problem, state
def save_dict(self, filename, data):
"""
Utility function to save a dictionary `data` to a HDF5 file
`filename`.
"""
io.write_dict_hdf5(filename, data)
def load_dict(self, filename):
"""
Utility function to load a dictionary `data` from a HDF5 file
`filename`.
"""
data = io.read_dict_hdf5(filename)
return data
示例5: PDESolverApp
# 需要导入模块: from sfepy.base.base import Struct [as 别名]
# 或者: from sfepy.base.base.Struct import to_dict [as 别名]
#.........这里部分代码省略.........
is_eqs = False
self.problem = Problem.from_conf(conf, init_equations=is_eqs, **kwargs)
self.setup_output_info(self.problem, self.options)
def setup_options(self):
self.app_options = PDESolverApp.process_options(self.conf.options)
assign_standard_hooks(self, self.app_options.get, self.conf)
# Override default equations, if use_equations is set.
if hasattr(self.conf, "equations"):
self.conf.equations = getattr(self.conf, self.app_options.use_equations)
def setup_output_info(self, problem, options):
"""Modifies both problem and options!"""
if options.output_filename_trunk is None:
if self.conf.get("filename_mesh") is not None:
filename_mesh = self.conf.filename_mesh
if isinstance(filename_mesh, MeshIO):
ofn_trunk = filename_mesh.get_filename_trunk()
else:
ofn_trunk = io.get_trunk(filename_mesh)
elif self.conf.get("filename_domain") is not None:
ofn_trunk = io.get_trunk(self.conf.filename_domain)
else:
raise ValueError("missing filename_mesh or filename_domain!")
options.output_filename_trunk = ofn_trunk
else:
ofn_trunk = options.output_filename_trunk
if hasattr(options, "output_format") and (options.output_format is not None):
output_format = options.output_format
else:
output_format = self.app_options.output_format
problem.setup_output(
output_filename_trunk=ofn_trunk,
output_dir=self.app_options.output_dir,
output_format=output_format,
file_per_var=self.app_options.file_per_var,
linearization=self.app_options.linearization,
)
def call(self, nls_status=None):
problem = self.problem
options = self.options
opts = self.app_options
if self.pre_process_hook is not None: # User pre_processing.
self.pre_process_hook(problem)
ofn_trunk = problem.ofn_trunk
self.save_names = Struct(
ebc=ofn_trunk + "_ebc.vtk" if options.save_ebc else None,
ebc_nodes=ofn_trunk + "_ebc_nodes.vtk" if options.save_ebc_nodes else None,
regions=ofn_trunk + "_region" if options.save_regions else None,
regions_as_groups=ofn_trunk + "_regions" if options.save_regions_as_groups else None,
field_meshes=ofn_trunk + "_field" if options.save_field_meshes else None,
)
if any(self.save_names.to_dict().values()):
save_only(self.conf, self.save_names, problem=problem)
if options.solve_not:
return None, None, None
time_solver = problem.get_time_solver()
time_solver.init_time(nls_status=nls_status)
for out in time_solver(
save_results=opts.save_results, step_hook=self.step_hook, post_process_hook=self.post_process_hook
):
step, time, state = out
if self.post_process_hook_final is not None: # User postprocessing.
self.post_process_hook_final(problem, state)
return problem, state
def save_dict(self, filename, data):
"""
Utility function to save a dictionary `data` to a HDF5 file
`filename`.
"""
io.write_dict_hdf5(filename, data)
def load_dict(self, filename):
"""
Utility function to load a dictionary `data` from a HDF5 file
`filename`.
"""
data = io.read_dict_hdf5(filename)
return data