/usr/lib/python2.7/dist-packages/PySPH-1.0a4.dev0-py2.7-linux-x86_64.egg/pysph/solver/solver.py is in python-pysph 0~20160514.git91867dc-4build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 | """ An implementation of a general solver base class """
# System library imports.
import os
import numpy
# PySPH imports
from pysph.base.kernels import CubicSpline
from pysph.sph.acceleration_eval import AccelerationEval
from pysph.sph.sph_compiler import SPHCompiler
from pysph.solver.utils import FloatPBar, load, dump
import logging
logger = logging.getLogger(__name__)
EPSILON = numpy.finfo(float).eps*2
class Solver(object):
"""Base class for all PySPH Solvers
"""
def __init__(self, dim=2, integrator=None, kernel=None,
n_damp=0, tf=1.0, dt=1e-3,
adaptive_timestep=False, cfl=0.3,
output_at_times=(),
fixed_h=False, **kwargs):
"""**Constructor**
Any additional keyword args are used to set the values of any
of the attributes.
Parameters
----------
dim : int
Dimension of the problem
integrator : pysph.sph.integrator.Integrator
Integrator to use
kernel : pysph.base.kernels.Kernel
SPH kernel to use
n_damp : int
Number of timesteps for which the initial damping is required.
This is used to improve stability for problems with strong
discontinuity in initial condition.
Setting it to zero will disable damping of the timesteps.
dt : double
Suggested initial time step for integration
tf : double
Final time for integration
adaptive_timestep : bint
Flag to use adaptive time steps
cfl : double
CFL number for adaptive time stepping
pfreq : int
Output files dumping frequency.
output_at_times : list/array
Optional list of output times to force dump the output file
fixed_h : bint
Flag for constant smoothing lengths `h`
Example
-------
>>> integrator = PECIntegrator(fluid=WCSPHStep())
>>> kernel = CubicSpline(dim=2)
>>> solver = Solver(dim=2, integrator=integrator, kernel=kernel,
... n_damp=50, tf=1.0, dt=1e-3, adaptive_timestep=True,
... pfreq=100, cfl=0.5, output_at_times=[1e-1, 1.0])
"""
self.integrator = integrator
self.dim = dim
if kernel is not None:
self.kernel = kernel
else:
self.kernel = CubicSpline(dim)
# set the particles to None
self.particles = None
# Set the AccelerationEval instance to None.
self.acceleration_eval = None
# solver time and iteration count
self.t = 0
self.count = 0
self.execute_commands = None
# list of functions to be called before and after an integration step
self.pre_step_callbacks = []
self.post_step_callbacks = []
# List of functions to be called after each stage of the integrator.
self.post_stage_callbacks = []
# default output printing frequency
self.pfreq = 100
# Compress generated files.
self.compress_output = False
self.disable_output = False
# the process id for parallel runs
self.pid = None
# set the default rank to 0
self.rank = 0
# set the default comm to None.
self.comm = None
# set the default mode to serial
self.in_parallel = False
# arrays to print output
self.arrays_to_print = []
# the default parallel output mode
self.parallel_output_mode = "collected"
# flag to print all arrays
self.detailed_output = False
# flag to save Remote arrays
self.output_only_real = True
# output filename
self.fname = self.__class__.__name__
# output drectory
self.output_directory = self.fname+'_output'
# solution damping to avoid impulsive starts
self.n_damp = n_damp
# Use adaptive time steps and cfl number
self.adaptive_timestep = adaptive_timestep
self.cfl = cfl
# list of output times
self.output_at_times = numpy.asarray(output_at_times)
self.force_output = False
# default time step constants
self.tf = tf
self.dt = dt
self.max_steps = 1 << 31
self._prev_dt = None
self._damping_factor = 1.0
self._epsilon = EPSILON*tf
# flag for constant smoothing lengths
self.fixed_h = fixed_h
# Set all extra keyword arguments
for attr, value in kwargs.items():
if hasattr(self, attr):
setattr(self, attr, value)
else:
msg = 'Unknown keyword arg "%s" passed to constructor'%attr
raise TypeError(msg)
##########################################################################
# Public interface.
##########################################################################
def setup(self, particles, equations, nnps, kernel=None, fixed_h=False):
""" Setup the solver.
The solver's processor id is set if the in_parallel flag is set
to true.
The order of the integrating calcs is determined by the solver's
order attribute.
This is usually called at the start of a PySPH simulation.
"""
self.particles = particles
if kernel is not None:
self.kernel = kernel
mode = 'mpi' if self.in_parallel else 'serial'
self.acceleration_eval = AccelerationEval(
particles, equations, self.kernel, mode
)
sep = '-'*70
eqn_info = '[\n' + ',\n'.join([str(e) for e in equations]) + '\n]'
logger.info('Using equations:\n%s\n%s\n%s'%(sep, eqn_info, sep))
logger.info(
'Using integrator:\n%s\n %s\n%s'%(sep, self.integrator, sep)
)
sph_compiler = SPHCompiler(
self.acceleration_eval, self.integrator
)
sph_compiler.compile()
# Set the nnps for all concerned objects.
self.acceleration_eval.set_nnps(nnps)
self.integrator.set_nnps(nnps)
# set the parallel manager for the integrator
self.integrator.set_parallel_manager(self.pm)
# Set the post_stage_callback.
self.integrator.set_post_stage_callback(self._post_stage_callback)
# set integrator option for constant smoothing length
self.fixed_h = fixed_h
self.integrator.set_fixed_h( fixed_h )
logger.debug("Solver setup complete.")
def add_post_stage_callback(self, callback):
"""These callbacks are called *after* each integrator stage.
The callbacks are passed (current_time, dt, stage). See the the
`Integrator.one_timestep` methods for examples of how this is called.
Example
-------
>>> def post_stage_callback_function(t, dt, stage):
>>> # This function is called after every stage of integrator.
>>> print t, dt, stage
>>> # Do something
>>> solver.add_post_stage_callback(post_stage_callback_function)
"""
self.post_stage_callbacks.append(callback)
def add_post_step_callback(self, callback):
"""These callbacks are called *after* each timestep is performed.
The callbacks are passed the solver instance (i.e. self).
Example
-------
>>> def post_step_callback_function(solver):
>>> # This function is called after every time step.
>>> print solver.t, solver.dt
>>> # Do something
>>> solver.add_post_step_callback(post_step_callback_function)
"""
self.post_step_callbacks.append(callback)
def add_pre_step_callback(self, callback):
"""These callbacks are called *before* each timestep is performed.
The callbacks are passed the solver instance (i.e. self).
Example
-------
>>> def pre_step_callback_function(solver):
>>> # This function is called before every time step.
>>> print solver.t, solver.dt
>>> # Do something
>>> solver.add_pre_step_callback(pre_step_callback_function)
"""
self.pre_step_callbacks.append(callback)
def append_particle_arrrays(self, arrays):
""" Append the particle arrays to the existing particle arrays
"""
if not self.particles:
print('Warning! Particles not defined.')
return
for array in self.particles:
array_name = array.name
for arr in arrays:
if array_name == arr.name:
array.append_parray(arr)
self.setup(self.particles)
def set_adaptive_timestep(self, value):
"""Set it to True to use adaptive timestepping based on
cfl, viscous and force factor.
Look at pysph.sph.integrator.compute_time_step for more details.
"""
self.adaptive_timestep = value
def set_cfl(self, value):
'Set the CFL number for adaptive time stepping'
self.cfl = value
def set_final_time(self, tf):
""" Set the final time for the simulation """
self.tf = tf
self._epsilon = EPSILON*tf
def set_time_step(self, dt):
""" Set the time step to use """
self.dt = dt
def set_print_freq(self, n):
""" Set the output print frequency """
self.pfreq = n
def set_disable_output(self, value):
"""Disable file output.
"""
self.disable_output = value
def set_arrays_to_print(self, array_names=None):
"""Only print the arrays with the given names.
"""
available_arrays = [array.name for array in self.particles]
if array_names:
for name in array_names:
if not name in available_arrays:
raise RuntimeError("Array %s not availabe"%(name))
for arr in self.particles:
if arr.name == name:
array = arr
break
self.arrays_to_print.append(array)
else:
self.arrays_to_print = self.particles
def set_output_fname(self, fname):
""" Set the output file name """
self.fname = fname
def set_output_printing_level(self, detailed_output):
""" Set the output printing level """
self.detailed_output = detailed_output
def set_output_only_real(self, output_only_real):
""" Set the flag to save out only real particles """
self.output_only_real = output_only_real
def set_output_directory(self, path):
""" Set the output directory """
self.output_directory = path
def set_output_at_times(self, output_at_times):
""" Set a list of output times """
self.output_at_times = numpy.asarray(output_at_times)
def set_max_steps(self, max_steps):
"""Set the maximum number of iterations to perform.
"""
self.max_steps = max_steps
def set_compress_output(self, compress):
"""Compress the dumped output files.
"""
self.compress_output = compress
def set_parallel_output_mode(self, mode="collected"):
"""Set the default solver dump mode in parallel.
The available modes are:
collected : Collect array data from all processors on root and
dump a single file.
distributed : Each processor dumps a file locally.
"""
assert mode in ("collected", "distributed")
self.parallel_output_mode = mode
def set_command_handler(self, callable, command_interval=1):
""" set the `callable` to be called at every `command_interval` iteration
the `callable` is called with the solver instance as an argument
"""
self.execute_commands = callable
self.command_interval = command_interval
def set_parallel_manager(self, pm):
self.pm = pm
def barrier(self):
if self.comm:
self.comm.barrier()
def solve(self, show_progress=True):
""" Solve the system
Notes
-----
Pre-stepping functions are those that need to be called before
the integrator is called.
Similarly, post step functions are those that are called after
the stepping within the integrator.
"""
if self.in_parallel:
show = False
else:
show = show_progress
bar = FloatPBar(self.t, self.tf, show=show)
self._epsilon = EPSILON*self.tf
# Initial solution
self.dump_output()
self.barrier() # everybody waits for this to complete
# Compute the accelerations once for the predictor corrector
# integrator to work correctly at the first time step.
self.acceleration_eval.compute(self.t, self.dt)
# Now get a suitable adaptive (if requested) and damped timestep to
# integrate with.
self.dt = self._get_timestep()
while (self.tf - self.t) > self._epsilon and \
(self.count < self.max_steps):
# perform any pre step functions
for callback in self.pre_step_callbacks:
callback(self)
if self.rank == 0:
logger.debug(
"Iteration=%d, time=%f, timestep=%f" % \
(self.count, self.t, self.dt)
)
# perform the integration and update the time.
#print 'Solver Iteration', self.count, self.dt, self.t
self.integrator.step(self.t, self.dt)
# perform any post step functions
for callback in self.post_step_callbacks:
callback(self)
# update time and iteration counters if successfully
# integrated
self.t += self.dt
self.count += 1
self._epsilon = EPSILON*self.tf*self.count
# Compute the next timestep.
self.dt = self._get_timestep()
# Note: this may adjust dt to land at a desired time.
self._dump_output_if_needed()
# update progress bar
bar.update(self.t)
# update the time for all arrays
self.update_particle_time()
if self.execute_commands is not None:
if self.count % self.command_interval == 0:
self.execute_commands(self)
# close the progress bar
bar.finish()
# final output save
self.dump_output()
def update_particle_time(self):
for array in self.particles:
array.set_time(self.t)
def dump_output(self):
"""Dump the simulation results to file
The arrays used for printing are determined by the particle
array's `output_property_arrays` data attribute. For debugging
it is sometimes nice to have all the arrays (including
accelerations) saved. This can be chosen from using the
command line option `--detailed-output`
Output data Format:
A single file named as: <fname>_<rank>_<iteration_count>.npz
The data is saved as a Python dictionary with two keys:
`solver_data` : Solver meta data like time, dt and iteration number
`arrays` : A dictionary keyed on particle array names and with
particle properties as value.
Example:
You can load the data output by PySPH like so:
>>> from pysph.solver.utils import load
>>> data = load('output_directory/filename_x_xxx.npz')
>>> solver_data = data['solver_data']
>>> arrays = data['arrays']
>>> fluid = arrays['fluid']
>>> ...
In the above example, it is assumed that the output file
contained an array named fluid.
"""
if self.disable_output:
return
if self.rank == 0:
msg = 'Writing output at time %g, iteration %d, dt = %g'%(
self.t, self.count, self.dt)
logger.info(msg)
fname = os.path.join(self.output_directory,
self.fname + '_' + str(self.count))
comm = None
if self.parallel_output_mode == "collected" and self.in_parallel:
comm = self.comm
dump(fname, self.particles, self._get_solver_data(),
detailed_output=self.detailed_output,
only_real=self.output_only_real, mpi_comm=comm,
compress=self.compress_output)
def load_output(self, count):
"""Load particle data from dumped output file.
Parameters
----------
count : str
The iteration time from which to load the data. If time is '?' then
list of available data files is returned else the latest available
data file is used
Notes
-----
Data is loaded from the :py:attr:`output_directory` using the same format
as stored by the :py:meth:`dump_output` method.
Proper functioning required that all the relevant properties of arrays be
dumped.
"""
# get the list of available files
available_files = [i.rsplit('_',1)[1][:-4]
for i in os.listdir(self.output_directory)
if i.startswith(self.fname) and i.endswith('.npz')]
if count == '?':
return sorted(set(available_files), key=int)
else:
if not count in available_files:
msg = "File with iteration count `%s` does not exist"%(count)
msg += "\nValid iteration counts are %s"%(sorted(set(available_files), key=int))
#print msg
raise IOError(msg)
array_names = [pa.name for pa in self.particles]
# load the output file
data = load(os.path.join(self.output_directory,
self.fname+'_'+str(count)+'.npz'))
arrays = [ data["arrays"][i] for i in array_names ]
# set the Particle's arrays
self.particles = arrays
solver_data = data['solver_data']
self.t = float(solver_data['t'])
self.dt = float(solver_data['dt'])
self.count = int(solver_data['count'])
def get_options(self, arg_parser):
""" Implement this to add additional options for the application """
pass
def setup_solver(self, options=None):
""" Implement the basic solvers here
All subclasses of Solver may implement this function to add the
necessary operations for the problem at hand.
Parameters
----------
options : dict
options set by the user using commandline (there is no guarantee
of existence of any key)
"""
pass
##########################################################################
# Non-public interface.
##########################################################################
def _compute_timestep(self):
undamped_dt = self._get_undamped_timestep()
if self.adaptive_timestep:
# locally stable time step
dt = self.integrator.compute_time_step(undamped_dt, self.cfl)
# set the globally stable time step across all processors
if self.in_parallel:
if dt is None:
# For some reason this processor does not have an adaptive
# timestep constraint so we set it to a large number so the
# timestep is determined by the other processors.
dt = 1e20
dt = self.pm.update_time_steps(dt)
else:
if dt is None:
dt = undamped_dt
else:
dt = undamped_dt
return dt
def _damp_timestep(self, dt):
"""Damp the timestep initially to prevent transient errors at startup.
This basically damps the initial timesteps by the factor
0.5 (sin(pi*(-0.5 + count/n_damp)) + 1)
Where n_damp is the number of iterations to damp the timestep for and
count is the number of iterations.
"""
n_damp = self.n_damp
if self.count < n_damp and n_damp > 0:
iter_fraction = (self.count+1)/float(n_damp)
fac = 0.5*(numpy.sin(numpy.pi*(-0.5 + iter_fraction)) + 1.0)
self._damping_factor = fac
else:
self._damping_factor = 1.0
return dt*self._damping_factor
def _dump_output_if_needed(self):
"""Dump output if needed while solve is running.
This is called by `solve`.
Warning
-------
This will adjust `dt` if the user has asked for output at a
non-integral multiple of dt.
"""
if abs(self.t - self.tf) < self._epsilon:
return
# dump output if the iteration number is a multiple of the printing
# frequency.
dump = self.count % self.pfreq == 0
# Consider the other cases if user has requested output at a specified
# time.
output_at_times = self.output_at_times
dt = self.dt
# adjust dt to land on specific output times or dump output if we have
# reached a desired time.
if len(output_at_times) > 0:
tdiff = output_at_times - self.t
if numpy.any(numpy.abs(tdiff) < self._epsilon):
dump = True
# Our next step may exceed a required timestep so we adjust the
# timestep.
timestep_too_big = (tdiff > 0.0) & (tdiff < dt)
if numpy.any(timestep_too_big):
index = numpy.where(timestep_too_big)[0]
output_time = output_at_times[index]
if abs(output_time - self.t) > self._epsilon:
# It sometimes happens that the current time is just
# shy of the requested output time which results in a
# ridiculously small dt so we skip that case.
# Compute the new time-step to fall on the specified output
# time instant and save the previous dt value.
self._prev_dt = dt
self.dt = float(output_time - self.t)
if dump:
self.dump_output()
self.barrier()
def _get_solver_data(self):
if self._prev_dt is not None:
dt = self._prev_dt/self._damping_factor
else:
dt = self._get_undamped_timestep()
return {'dt': dt, 't': self.t, 'count': self.count}
def _get_timestep(self):
if abs(self.tf - self.t) < self._epsilon:
# We have reached the end, so no need to adjust the timestep
# anymore.
return self.dt
if self._prev_dt is not None and \
abs(self._prev_dt - self.dt) > self._epsilon:
# if the _prev_dt was set then we need to use it as the current dt
# was set to print at an intermediate time.
self.dt = self._prev_dt
self._prev_dt = None
dt = self._compute_timestep()
dt = self._damp_timestep(dt)
# adjust dt to land exactly on final time
if (self.t + dt) > (self.tf - self._epsilon):
dt = self.tf - self.t
return dt
def _get_undamped_timestep(self):
return self.dt/self._damping_factor
def _post_stage_callback(self, time, dt, stage):
for callback in self.post_stage_callbacks:
callback(time, dt, stage)
############################################################################
|