/*
 * This file is part of the GROMACS molecular simulation package.
 *
 * Copyright 2023- The GROMACS Authors
 * and the project initiators Erik Lindahl, Berk Hess and David van der Spoel.
 * Consult the AUTHORS/COPYING files and https://www.gromacs.org for details.
 *
 * GROMACS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * GROMACS 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with GROMACS; if not, see
 * https://www.gnu.org/licenses, or write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
 *
 * If you want to redistribute modifications to GROMACS, please
 * consider that scientific software is very special. Version
 * control is crucial - bugs must be traceable. We will be happy to
 * consider code for inclusion in the official distribution, but
 * derived work must not be called official GROMACS. Details are found
 * in the README & COPYING files - if they are missing, get the
 * official version at https://www.gromacs.org.
 *
 * To help us fund GROMACS development, we humbly ask that you cite
 * the research papers on the package. Check out https://www.gromacs.org.
 */
/*! \internal \file
 * \brief
 * Implements Colvars MDModule class
 *
 * \author Hubert Santuz <hubert.santuz@gmail.com>
 * \ingroup module_applied_forces
 */
#include "gmxpre.h"

#include "colvarsMDModule.h"

#include <functional>
#include <memory>
#include <string>

#include "gromacs/domdec/localatomsetmanager.h"
#include "gromacs/fileio/checkpoint.h"
#include "gromacs/mdrunutility/mdmodulesnotifiers.h"
#include "gromacs/mdrunutility/multisim.h"
#include "gromacs/mdtypes/iforceprovider.h"
#include "gromacs/mdtypes/imdmodule.h"
#include "gromacs/utility/keyvaluetreebuilder.h"
#include "gromacs/utility/mpicomm.h"
#include "gromacs/utility/stringutil.h"

#include "colvars_version.h"
#include "colvarsforceprovider.h"
#include "colvarsoptions.h"
#include "colvarssimulationsparameters.h"

enum class PbcType : int;
struct gmx_mtop_t;


namespace gmx
{
class IMDOutputProvider;
class IMdpOptionProvider;
class KeyValueTreeObject;
class MDLogger;

namespace
{

/*! \internal
 * \brief Colvars module
 *
 * Class that implements the colvars MDModule
 */
class ColvarsMDModule final : public IMDModule
{
public:
    //! \brief Construct the colvars module.
    explicit ColvarsMDModule() = default;

    // Now callbacks for several kinds of MdModuleNotification are created
    // and subscribed, and will be dispatched correctly at run time
    // based on the type of the parameter required by the lambda.

    /*! \brief Requests to be notified during pre-processing.
     *
     * \param[in] notifiers allows the module to subscribe to notifications from MdModules.
     *
     * The Colvars MDModule subscribes to these notifications:
     *   - storing its internal parameters in a tpr file by writing to a
     *     key-value-tree during pre-processing by a function taking a
     *     KeyValueTreeObjectBuilder as parameter
     *   - Acess topology using gmx_mtop_t notification
     *   - Access MDLogger for notifications output
     *   - Access warning for for grompp warnings output
     *   - Coordinates, PBC and box for setting up the proxy
     */
    void subscribeToPreProcessingNotifications(MDModulesNotifiers* notifiers) override
    {

        if (!colvarsOptions_.isActive())
        {
            return;
        }

        // Writing internal parameters during pre-processing
        const auto writeInternalParametersFunction = [this](KeyValueTreeObjectBuilder treeBuilder)
        { colvarsOptions_.writeInternalParametersToKvt(treeBuilder); };
        notifiers->preProcessingNotifier_.subscribe(writeInternalParametersFunction);

        // Access of the topology during pre-processing
        const auto processTopologyFunction = [this](gmx_mtop_t* mtop)
        { colvarsOptions_.processTopology(mtop); };
        notifiers->preProcessingNotifier_.subscribe(processTopologyFunction);

        // Set Logger during pre-processing
        const auto setLoggerFunction = [this](const MDLogger& logger)
        { colvarsOptions_.setLogger(logger); };
        notifiers->preProcessingNotifier_.subscribe(setLoggerFunction);

        //  Notification of the Coordinates, box and pbc during pre-processing
        const auto processCoordinatesFunction = [this](const CoordinatesAndBoxPreprocessed& coord)
        { colvarsOptions_.processCoordinates(coord); };
        notifiers->preProcessingNotifier_.subscribe(processCoordinatesFunction);

        //  Notification for the temperature
        const auto processTemperatureFunction = [this](const EnsembleTemperature& temp)
        { colvarsOptions_.processTemperature(temp); };
        notifiers->preProcessingNotifier_.subscribe(processTemperatureFunction);
    }


    /*! \brief Request to be notified.
     *
     * \param[in] notifiers allows the module to subscribe to notifications from MdModules.
     *
     * The Colvars MDModule subscribes to these notifications:
     *   - the LocalAtomSetManager sets in the simulation parameter setup
     *     by taking a LocalAtomSetManager * as parameter
     *   - the type of periodic boundary conditions that are used
     *     by taking a PeriodicBoundaryConditionType as parameter
     *   - the topology of the system
     *     by taking a gmx_mtop_t * as parameter
     *   - the communicator
     *     by taking a MpiComm as parameter
     *   - the simulation time step
     *     by taking a SimulationTimeStep as a parameter
     *   - MDLogger for notifications output
     *   - the .edr filename
     *     to provide a user-defined output prefix for Colvars output files
     */
    void subscribeToSimulationSetupNotifications(MDModulesNotifiers* notifiers) override
    {
        if (!colvarsOptions_.isActive())
        {
            return;
        }

        // Reading internal parameters during simulation setup
        const auto readInternalParametersFunction = [this](const KeyValueTreeObject& tree)
        { colvarsOptions_.readInternalParametersFromKvt(tree); };
        notifiers->simulationSetupNotifier_.subscribe(readInternalParametersFunction);
        // Retrieve the LocalAtomSetManager during simulation setup
        const auto setLocalAtomManagerFunction = [this](LocalAtomSetManager* localAtomSetManager)
        { this->ColvarsSimulationsParameters_.setLocalAtomSetManager(localAtomSetManager); };
        notifiers->simulationSetupNotifier_.subscribe(setLocalAtomManagerFunction);

        // constructing PBC during simulation setup
        const auto setPeriodicBoundaryContionsFunction = [this](const PbcType& pbc)
        { this->ColvarsSimulationsParameters_.setPeriodicBoundaryConditionType(pbc); };
        notifiers->simulationSetupNotifier_.subscribe(setPeriodicBoundaryContionsFunction);

        // Retrieve the topology during simulation setup
        const auto setTopologyFunction = [this](const gmx_mtop_t& mtop)
        { this->ColvarsSimulationsParameters_.setTopology(mtop); };
        notifiers->simulationSetupNotifier_.subscribe(setTopologyFunction);

        // Retrieve the Communication Record during simulations setup
        const auto setCommFunction = [this](const MpiComm& mpiComm)
        { this->ColvarsSimulationsParameters_.setComm(mpiComm); };
        notifiers->simulationSetupNotifier_.subscribe(setCommFunction);

        // Retrieve the Multisim Record during simulations setup
        const auto setMultisimFunction = [this](const gmx_multisim_t* ms)
        { this->ColvarsSimulationsParameters_.setMultisim(ms); };
        notifiers->simulationSetupNotifier_.subscribe(setMultisimFunction);

        // setting the simulation time step
        const auto setSimulationTimeStepFunction = [this](const SimulationTimeStep& simulationTimeStep)
        { this->ColvarsSimulationsParameters_.setSimulationTimeStep(simulationTimeStep.delta_t); };
        notifiers->simulationSetupNotifier_.subscribe(setSimulationTimeStepFunction);
        // Saving MDLogger during simulation setup
        const auto setLoggerFunction = [this](const MDLogger& logger)
        { this->ColvarsSimulationsParameters_.setLogger(logger); };
        notifiers->simulationSetupNotifier_.subscribe(setLoggerFunction);

        const auto setEdrFileNameFunction = [this](const EdrOutputFilename& filename)
        { colvarsOptions_.processEdrFilename(filename); };
        notifiers->simulationSetupNotifier_.subscribe(setEdrFileNameFunction);

        // writing checkpoint data
        const auto checkpointDataWriting = [this](MDModulesWriteCheckpointData checkpointData)
        { colvarsForceProvider_->writeCheckpointData(checkpointData, ColvarsModuleInfo::sc_name); };
        notifiers->checkpointingNotifier_.subscribe(checkpointDataWriting);

        // reading checkpoint data
        const auto checkpointDataReading = [this](MDModulesCheckpointReadingDataOnMain checkpointData)
        { colvarsState_.readState(checkpointData.checkpointedData_, ColvarsModuleInfo::sc_name); };
        notifiers->checkpointingNotifier_.subscribe(checkpointDataReading);
    }

    /*! \brief Request to be notified.
     *
     * \param[in] notifiers allows the module to subscribe to notifications from MdModules.
     *
     * The Colvars MDModule subscribes to this notification:
     *   - the atom redistributed signal
     */
    void subscribeToSimulationRunNotifications(MDModulesNotifiers* notifiers) override
    {
        if (!colvarsOptions_.isActive())
        {
            return;
        }

        // Handle the atoms redistributed signal
        const auto handleAtomsRedistributedSignal =
                [this](const MDModulesAtomsRedistributedSignal& atomsRedistributedSignal)
        { colvarsForceProvider_->processAtomsRedistributedSignal(atomsRedistributedSignal); };
        notifiers->simulationRunNotifier_.subscribe(handleAtomsRedistributedSignal);
    }

    //! From IMDModule
    IMdpOptionProvider* mdpOptionProvider() override { return &colvarsOptions_; }
    //! From IMDModule
    //! Colvars provide its own output
    IMDOutputProvider* outputProvider() override { return nullptr; }

    //! From IMDModule
    //! Add this module to the force providers if active
    void initForceProviders(ForceProviders* forceProviders) override
    {
        if (colvarsOptions_.isActive())
        {

            colvarsForceProvider_ = std::make_unique<ColvarsForceProvider>(
                    colvarsOptions_.colvarsConfigContent(),
                    ColvarsSimulationsParameters_.topology(),
                    ColvarsSimulationsParameters_.periodicBoundaryConditionType(),
                    ColvarsSimulationsParameters_.logger(),
                    colvarsOptions_.colvarsInputFiles(),
                    colvarsOptions_.colvarsEnsTemp(),
                    colvarsOptions_.colvarsSeed(),
                    ColvarsSimulationsParameters_.localAtomSetManager(),
                    ColvarsSimulationsParameters_.comm(),
                    ColvarsSimulationsParameters_.ms(),
                    ColvarsSimulationsParameters_.simulationTimeStep(),
                    colvarsOptions_.colvarsAtomCoords(),
                    colvarsOptions_.colvarsOutputPrefix(),
                    colvarsState_);
            forceProviders->addForceProvider(colvarsForceProvider_.get(), "Colvars");
        }
    }


private:
    //! The options provided for colvars
    ColvarsOptions colvarsOptions_;

    //! Parameters that become available at simulation setup time.
    ColvarsSimulationsParameters ColvarsSimulationsParameters_;

    //! Object that evaluates the forces
    std::unique_ptr<ColvarsForceProvider> colvarsForceProvider_;

    //! The state of colvars force provider to be written in the checkpoint
    ColvarsForceProviderState colvarsState_;
};

} // namespace

std::unique_ptr<IMDModule> ColvarsModuleInfo::create()
{
    return std::make_unique<ColvarsMDModule>();
}

std::string colvarsDescription()
{
    return formatString("enabled (version %s)", COLVARS_VERSION);
}

} // namespace gmx
