Skip to content
Snippets Groups Projects
Commit dc0a4b4b authored by Jean-Baptiste Keck's avatar Jean-Baptiste Keck
Browse files

cleaned all examples

parent 44858321
No related branches found
No related tags found
No related merge requests found
...@@ -6,9 +6,9 @@ def compute(args): ...@@ -6,9 +6,9 @@ def compute(args):
''' '''
HySoP Analytic Example: Initialize a field with a space and time dependent analytic formula. HySoP Analytic Example: Initialize a field with a space and time dependent analytic formula.
''' '''
from hysop import IO, Field, Box, MPIParams, \ from hysop import Field, Box, MPIParams, \
Simulation, Problem, ScalarParameter Simulation, Problem, ScalarParameter
from hysop.constants import Implementation, AutotunerFlags from hysop.constants import Implementation
from hysop.operators import AnalyticField from hysop.operators import AnalyticField
# Define domain # Define domain
...@@ -18,10 +18,15 @@ def compute(args): ...@@ -18,10 +18,15 @@ def compute(args):
# Define parameters and field (time and analytic field) # Define parameters and field (time and analytic field)
t = ScalarParameter('t', dtype=args.dtype) t = ScalarParameter('t', dtype=args.dtype)
scalar = Field(domain=box, name='S0', nb_components=1, dtype=args.dtype) scalar = Field(domain=box, name='S0', nb_components=1, dtype=args.dtype)
# We need to first get default MPI parameters (even for non MPI jobs)
# so we use default domain communicator and task.
mpi_params = MPIParams(comm=box.task_comm,
task_id=box.current_task())
# Setup implementation specific variables # Setup implementation specific variables
impl = args.impl impl = args.impl
op_kwds = {} op_kwds = {'mpi_params': mpi_params}
if (impl is Implementation.PYTHON): if (impl is Implementation.PYTHON):
# Setup python specific extra operator keyword arguments # Setup python specific extra operator keyword arguments
# (mapping: variable name => variable value) # (mapping: variable name => variable value)
...@@ -29,11 +34,6 @@ def compute(args): ...@@ -29,11 +34,6 @@ def compute(args):
elif (impl is Implementation.OPENCL_CODEGEN): elif (impl is Implementation.OPENCL_CODEGEN):
# For the OpenCL implementation we need to setup the compute device # For the OpenCL implementation we need to setup the compute device
# and configure how the code is generated and compiled at runtime. # and configure how the code is generated and compiled at runtime.
# We need to first create MPI parameters to initialize an OpenCL environment
# (even for non MPI jobs) so we use default domain communicator and task.
mpi_params = MPIParams(comm=box.task_comm,
task_id=box.current_task())
# Create an explicit OpenCL context from user parameters # Create an explicit OpenCL context from user parameters
from hysop.backend.device.opencl.opencl_tools import get_or_create_opencl_env from hysop.backend.device.opencl.opencl_tools import get_or_create_opencl_env
...@@ -44,7 +44,6 @@ def compute(args): ...@@ -44,7 +44,6 @@ def compute(args):
method = {OpenClKernelConfig: args.opencl_kernel_config} method = {OpenClKernelConfig: args.opencl_kernel_config}
# Setup opencl specific extra operator keyword arguments # Setup opencl specific extra operator keyword arguments
op_kwds['mpi_params'] = mpi_params
op_kwds['cl_env'] = cl_env op_kwds['cl_env'] = cl_env
op_kwds['method'] = method op_kwds['method'] = method
else: else:
...@@ -64,8 +63,10 @@ def compute(args): ...@@ -64,8 +63,10 @@ def compute(args):
data[0][...] *= np.cos(x-t()) data[0][...] *= np.cos(x-t())
elif (impl is Implementation.OPENCL_CODEGEN): elif (impl is Implementation.OPENCL_CODEGEN):
# With the opencl codegen implementation we use a symbolic expression # With the opencl codegen implementation we use a symbolic expression
# generated using sympy. OpenCL code will be automatically generated. # generated using sympy. OpenCL code will be automatically generated,
# For a vector field, multiple expressions should be given as a tuple. # compiled and passed trough a runtime parameter autotuner to achieve
# maximal performances on a variety of different devices.
# For a vector field, multiple symbolic expressions should be given as a tuple.
# Here xs and ts are the symbolic variables representing coordinates and time. # Here xs and ts are the symbolic variables representing coordinates and time.
xs = box.frame.coords xs = box.frame.coords
ts = t.s ts = t.s
...@@ -89,15 +90,21 @@ def compute(args): ...@@ -89,15 +90,21 @@ def compute(args):
problem.insert(analytic) problem.insert(analytic)
problem.build() problem.build()
# If a visu_rank was provided, display the graph on the given process rank. # If a visu_rank was provided, and show_graph was set,
problem.display(args.visu_rank) # display the graph on the given process rank.
if args.display_graph:
problem.display(args.visu_rank)
# Create a simulation and solve the problem # Create a simulation and solve the problem
# (do not forget to specify the time parameter here) # (do not forget to specify the time parameter here)
simu = Simulation(start=args.tstart, end=args.tend, simu = Simulation(start=args.tstart, end=args.tend,
nb_iter=args.nb_iter, dt0=args.dt, nb_iter=args.nb_iter, dt0=args.dt,
t=t) t=t)
problem.solve(simu)
# Finally solve the problem
problem.solve(simu, dry_run=args.dry_run)
# Finalize
problem.finalize() problem.finalize()
......
...@@ -111,11 +111,15 @@ def compute(args): ...@@ -111,11 +111,15 @@ def compute(args):
dt0 = (args.cfl*dx)/Vinf dt0 = (args.cfl*dx)/Vinf
if (args.dt is not None): if (args.dt is not None):
dt0 = min(args.dt, dt0) dt0 = min(args.dt, dt0)
dt0 = 0.99*dt0
# Create a simulation and solve the problem # Create a simulation and solve the problem
# (do not forget to specify the dt parameter here) # (do not forget to specify the dt parameter here)
simu = Simulation(start=args.tstart, end=args.tend, simu = Simulation(start=args.tstart, end=args.tend,
dt0=dt0, dt=dt) nb_iter=args.nb_iter,
max_iter=args.max_iter,
times_of_interest=args.dump_times,
dt=dt, dt0=dt0)
# Finally solve the problem # Finally solve the problem
problem.solve(simu, dry_run=args.dry_run) problem.solve(simu, dry_run=args.dry_run)
......
...@@ -12,16 +12,16 @@ def compute(args): ...@@ -12,16 +12,16 @@ def compute(args):
Simulation, Problem, ScalarParameter, \ Simulation, Problem, ScalarParameter, \
MPIParams MPIParams
from hysop.constants import Implementation from hysop.constants import Implementation, ComputeGranularity
from hysop.operator.directional.diffusion_dir import DirectionalDiffusion from hysop.operator.directional.diffusion_dir import DirectionalDiffusion
from hysop.methods import StrangOrder, TimeIntegrator, SpaceDiscretization from hysop.methods import StrangOrder, TimeIntegrator, SpaceDiscretization
from hysop.numerics.splitting.strang import StrangSplitting from hysop.numerics.splitting.strang import StrangSplitting
from hysop.numerics.odesolvers.runge_kutta import Euler, RK2, RK3, RK4
# Define domain # Define domain
dim = args.ndim
npts = args.npts npts = args.npts
box = Box(origin=args.box_origin, length=args.box_length, dim=args.ndim) box = Box(origin=args.box_origin, length=args.box_length, dim=dim)
# Get default MPI Parameters from domain (even for serial jobs) # Get default MPI Parameters from domain (even for serial jobs)
mpi_params = MPIParams(comm=box.task_comm, mpi_params = MPIParams(comm=box.task_comm,
...@@ -33,11 +33,15 @@ def compute(args): ...@@ -33,11 +33,15 @@ def compute(args):
scalar = Field(domain=box, name='S0', nb_components=1, dtype=args.dtype) scalar = Field(domain=box, name='S0', nb_components=1, dtype=args.dtype)
# Diffusion operator discretization parameters # Diffusion operator discretization parameters
method = {TimeIntegrator: Euler, SpaceDiscretization: 2} method = {
ComputeGranularity: args.compute_granularity,
SpaceDiscretization: args.fd_order,
TimeIntegrator: args.time_integrator,
}
# Setup implementation specific variables # Setup implementation specific variables
impl = args.impl impl = args.impl
extra_op_kwds = {} extra_op_kwds = { 'mpi_params': mpi_params }
if (impl is Implementation.PYTHON): if (impl is Implementation.PYTHON):
pass pass
elif (impl is Implementation.OPENCL_CODEGEN): elif (impl is Implementation.OPENCL_CODEGEN):
...@@ -51,10 +55,10 @@ def compute(args): ...@@ -51,10 +55,10 @@ def compute(args):
# Configure OpenCL kernel generation and tuning (already done by HysopArgParser) # Configure OpenCL kernel generation and tuning (already done by HysopArgParser)
from hysop.methods import OpenClKernelConfig from hysop.methods import OpenClKernelConfig
method = {OpenClKernelConfig: args.opencl_kernel_config} method[OpenClKernelConfig] = args.opencl_kernel_config
# Setup opencl specific extra operator keyword arguments # Setup opencl specific extra operator keyword arguments
extra_op_kwds['cl_env'] = cl_env extra_op_kwds['cl_env'] = cl_env
else: else:
msg='Unknown implementation \'{}\'.'.format(impl) msg='Unknown implementation \'{}\'.'.format(impl)
raise ValueError(msg) raise ValueError(msg)
...@@ -64,12 +68,10 @@ def compute(args): ...@@ -64,12 +68,10 @@ def compute(args):
name='diffusion', dt=dt, name='diffusion', dt=dt,
fields=scalar, coeffs=nu, fields=scalar, coeffs=nu,
variables = {scalar: npts}, variables = {scalar: npts},
method=method, mpi_params=mpi_params,
**extra_op_kwds) **extra_op_kwds)
# Build the directional splitting operator graph # Build the directional splitting operator graph
splitting = StrangSplitting(splitting_dim=box.dim, splitting = StrangSplitting(splitting_dim=dim, order=args.strang_order)
order=StrangOrder.STRANG_SECOND_ORDER)
splitting.push_operators(diffusion) splitting.push_operators(diffusion)
# Create the problem we want to solve and insert our # Create the problem we want to solve and insert our
...@@ -80,8 +82,10 @@ def compute(args): ...@@ -80,8 +82,10 @@ def compute(args):
problem.dump_inputs(fields=scalar, filename='S0', frequency=args.dump_freq) problem.dump_inputs(fields=scalar, filename='S0', frequency=args.dump_freq)
problem.build() problem.build()
# If a visu_rank was provided, display the graph on the given process rank. # If a visu_rank was provided, and show_graph was set,
problem.display(args.visu_rank) # display the graph on the given process rank.
if args.display_graph:
problem.display(args.visu_rank)
# Initialize discrete scalar field # Initialize discrete scalar field
dfields = problem.input_discrete_fields dfields = problem.input_discrete_fields
...@@ -90,9 +94,14 @@ def compute(args): ...@@ -90,9 +94,14 @@ def compute(args):
# Create a simulation and solve the problem # Create a simulation and solve the problem
# (do not forget to specify the dt parameter here) # (do not forget to specify the dt parameter here)
simu = Simulation(start=args.tstart, end=args.tend, simu = Simulation(start=args.tstart, end=args.tend,
dt0=args.dt, nb_iter=args.nb_iter, nb_iter=args.nb_iter, max_iter=args.max_iter,
dt=dt) times_of_interest=args.dump_times,
problem.solve(simu) dt=dt, dt0=args.dt)
# Finally solve the problem
problem.solve(simu, dry_run=args.dry_run)
# Finalize
problem.finalize() problem.finalize()
...@@ -128,7 +137,7 @@ if __name__=='__main__': ...@@ -128,7 +137,7 @@ if __name__=='__main__':
parser = ScalarDiffusionArgParser() parser = ScalarDiffusionArgParser()
parser.set_defaults(box_origin=(0.0,), box_length=(1.0,), parser.set_defaults(box_origin=(0.0,), box_length=(1.0,),
tstart=0.0, tend=1.0, nb_iter=500, tstart=0.0, tend=1.0, nb_iter=500,
dump_freq=5, nu=0.01, impl='cl') dump_freq=5, nu=0.01, impl='cl')
parser.run(compute) parser.run(compute)
...@@ -158,7 +158,7 @@ def compute(args): ...@@ -158,7 +158,7 @@ def compute(args):
max_iter=args.max_iter, max_iter=args.max_iter,
dt0=args.dt, times_of_interest=args.dump_times, dt0=args.dt, times_of_interest=args.dump_times,
t=t, dt=dt) t=t, dt=dt)
simu.write_parameters(t, dt_cfl, dt_advec, dt, enstrophy, #TODO dt_stretch simu.write_parameters(t, dt_cfl, dt_advec, dt, enstrophy,
filename='parameters.txt', precision=6) filename='parameters.txt', precision=6)
# Initialize only the vorticity # Initialize only the vorticity
......
...@@ -146,7 +146,7 @@ class Simulation(object): ...@@ -146,7 +146,7 @@ class Simulation(object):
# backup initial time step, required to reset simulation. # backup initial time step, required to reset simulation.
self.max_iter = first_not_None(max_iter, 1e9) self.max_iter = first_not_None(max_iter, 1e9)
assert max_iter >= nb_iter assert self.max_iter >= nb_iter
# Starting time for the current iteration # Starting time for the current iteration
if (t is None): if (t is None):
self.t = ScalarParameter(name='t', dtype=HYSOP_REAL, self.t = ScalarParameter(name='t', dtype=HYSOP_REAL,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment