#!@PYTHON@
# -*- coding: utf-8 -*-
# @configure_input@

#-------------------------------------------------------------------------------

# This file is part of Code_Saturne, a general-purpose CFD tool.
#
# Copyright (C) 1998-2013 EDF S.A.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA.

#-------------------------------------------------------------------------------

#===============================================================================
# Import required Python modules
#===============================================================================

import sys

# Trick so that one doesn't have to set the PYTHONPATH variable
sys.path.insert(0, '@pkgpythondir@')
sys.path.insert(1, '<code_saturne.cfg/syrthes>')

from cs_package import package
from cs_exec_environment import *
from cs_case import *
from cs_case_coupling import *

#===============================================================================
# User variable settings to specify a coupling computation environnement
#
# Variables set to 'None' will be determined automatically
#===============================================================================

casedir = 'CASEDIRNAME'

# A coupling case is defined by a dictionnary, containing the following:

# Solver type ('Code_Saturne', 'SYRTHES', 'NEPTUNE_CFD' or 'Code_Aster')
# Domain directory name
# Run parameter setting file
# Number of processors (or None for automatic setting)
# Optional command line parameters. If not useful = None

# Define coupled domains
domains = [

    {'solver': 'Code_Saturne',
     'domain': 'fluid',
     'script': 'runcase',
     'n_procs_weight': None,
     'n_procs_min': 1,
     'n_procs_max': None}

    ,
    {'solver': 'SYRTHES',
     'domain': 'solid',
     'script': 'solid-coupling.syd',
     'n_procs_weight': None,
     'n_procs_min': 1,
     'n_procs_max': None,
     'opt' : '-v ens'}               # Additional SYRTHES options
                               # (ex.: postprocessing with '-v ens' or '-v med')

    ]

# SCRATCHDIR variable allows the user to specify in which temporary
# directory the calculation will run. If set to None, a default directory
# will be used (architecture dependent). If a value is specified,
# the temporary directory will be of the form:
#  <SCRATCHDIR>/tmp_Saturne/<study>.<case>.<date>

SCRATCHDIR = None

# PSET_SIZE allows defining the size of a pset (set of processors sharing
# an I/O node) on Blue Gene type machines. On Blue Gene/P, only one
# executable per pset may be run. Typical pset sizes are 32 or 64, but
# different partitions may have different pset sizes in some configurations.

PSET_SIZE = 1

#-------------------------------------------------------------------------------

if __name__ == '__main__':

    # Run coupling case

    case = coupling(package(),
                    domains,
                    casedir,
                    pset_size=PSET_SIZE)

    # Select run id
    # (usually date+time; defined automatically when preparing data, but we need
    # to set this so as to match an existing run when only running the solver or
    # saving results with previously prepared data)

    run_id = None

    # Force MPI environment if mpi_environment != None

    mpi_env = None

    # Syntax is as follows:
    #
    # mpi_env = mpi_environment(case.package_compute)
    #
    # Some fields may need to be modified in case of incorrect defaults
    # (due to the wide variety of MPI implementations and build options,
    # the default configuration may not give correct values in some cases).

    # mpi_env.bindir = path to mpi binaries
    # mpi_env.mpiexec = mpiexec, mpirun, or equivalent command
    # mpi_env.mpiexec_opts = mpiexec command options
    # mpi_env.mpiexec_args = option to pass arguments (usually None, or -args)
    # mpi_env.mpiexec_exe = option to define executable (usually None, or -exe)
    # mpi_env.mpiexec_n = option to define number of ranks (e.g. ' -n ', ' -np ')
    # mpi_env.mpiexec_n_per_node = option to define number of ranks per node
    #                              (e.g. ' -ppn 6 ', ' --ranks-per-node 16 ')
    # mpi_env.mpiexec_separator = separator after options (':' for Blue Gene/Q)
    # mpi_env.gen_hostsfile = shell command to generate hostsfile if required
    # mpi_env.del_hostsfile = shell command to delete hostsfile if required
    # mpi_env.mpiboot = command to start environment (e.g. mpdboot, lamboot)
    # mpi_env.mpihalt = command to halt environment (e.g. mpdallexit, lamhalt)
    # mpi_env.mpmd = MPI_MPMD_mpiexec (mpiexec colon-separated syntax), or
    #                MPI_MPMD_configfile (mpiexec -configfile syntax), or
    #                MPI_MPMD_script, or
    #                MPI_MPMD_execve

    # Execute script

    case.run(n_procs=None,
             mpi_environment=mpi_env,
             scratchdir=SCRATCHDIR,
             run_id=run_id,
             prepare_data=True,
             run_solver=True,
             save_results=True)

