/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | www.openfoam.com
     \\/     M anipulation  |
-------------------------------------------------------------------------------
    Copyright (C) 2016 OpenFOAM Foundation
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM 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 3 of the License, or
    (at your option) any later version.

    OpenFOAM 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 OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "constraintPlaneCOM.H"
#include "rigidBodyModel.H"
#include "addToRunTimeSelectionTable.H"

// #include "rigidBodyMotion.H"

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

namespace Foam
{
namespace RBD
{
namespace restraints
{
    defineTypeNameAndDebug(constraintPlaneCOM, 0);

    addToRunTimeSelectionTable
    (
        restraint,
        constraintPlaneCOM,
        dictionary
    );
}
}
}


// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::RBD::restraints::constraintPlaneCOM::constraintPlaneCOM
(
    const word& name,
    const dictionary& dict,
    const rigidBodyModel& model
)
:
    restraint(name, dict, model),
    constraintPlaneCOMReport_( dict.getOrDefault<bool>("report", false))
{
    read(dict);
}


// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

Foam::RBD::restraints::constraintPlaneCOM::~constraintPlaneCOM()
{}


// * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //

void Foam::RBD::restraints::constraintPlaneCOM::restrain
(
    scalarField& tau,
    Field<spatialVector>& fx,
    const rigidBodyModelState& state
) const
{
    	// Force and moment on the master body including optional damping: 0.0 means target.
	vector force(Zero), cog((model_.X0(bodyID_).inv() && spatialVector(Zero, Zero)).l());
	/*** x ***/
	if( constraint_plane[0] == 0 )		force[0] = gain[0] * ( constraint_plane[1] - cog.x() )
							 + gain[1] * ( 0.0 - model_.v(bodyID_, Zero).l().x() );
	/*** y ***/
	else if( constraint_plane[0] == 1 )	force[1] = gain[0] * ( constraint_plane[1] - cog.y() )
						 	 + gain[1] * ( 0.0 - model_.v(bodyID_, Zero).l().y() );
	/*** z ***/
	else 					force[2] = gain[0] * ( constraint_plane[1] - cog.z() )
						 	 + gain[1] * ( 0.0 - model_.v(bodyID_, Zero).l().z() );
	vector moment(Zero);

    if (model_.debug || constraintPlaneCOMReport_)
//    if ( RBD::rigidBodyMotion::Iteration_number_for_MB_ == 0 )
    {
	Info<< " constraintPlaneCOM: " << endl
	<< model_.name(bodyID_) << " [" << bodyID_ << ", " << bodyIndex_ << "]" << endl
	<< "pos " << cog << " , vel " << model_.v(bodyID_, Zero).l() << endl;
	if( constraint_plane[0] == 0 )		Info << " P  " << gain[0] * ( constraint_plane[1] - cog.x() ) << " D  " << gain[1] * ( 0.0 - model_.v(bodyID_, Zero).l().x() );
	else if( constraint_plane[0] == 1 )	Info << " P  " << gain[0] * ( constraint_plane[1] - cog.y() ) << " D  " << gain[1] * ( 0.0 - model_.v(bodyID_, Zero).l().y() );
	else 					Info << " P  " << gain[0] * ( constraint_plane[1] - cog.z() ) << " D  " << gain[1] * ( 0.0 - model_.v(bodyID_, Zero).l().z() );

	Info << " foce(P+D) " << force << " moment: " << moment << endl;
    }

    	// Accumulate the force for the restrained body
    	if( bodyIndex_ != 0 )	fx[bodyIndex_]  += spatialVector( moment, force );
}

bool Foam::RBD::restraints::constraintPlaneCOM::read
(
    const dictionary& dict
)
{
    restraint::read(dict);

    coeffs_.readEntry("constraint_plane", constraint_plane);
    coeffs_.readEntry("gain", gain);

    return true;
}


void Foam::RBD::restraints::constraintPlaneCOM::write
(
    Ostream& os
) const
{
    restraint::write(os);

    os.writeEntry("constraint_plane", constraint_plane);
    os.writeEntry("gain", gain);
}


// ************************************************************************* //
