/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | www.openfoam.com
     \\/     M anipulation  |
-------------------------------------------------------------------------------
    Copyright (C) 2019 OpenFOAM Foundation
    Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
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 "externalForce.H"
#include "rigidBodyModel.H"
#include "rigidBodyModelState.H"
#include "OneConstant.H"
#include "addToRunTimeSelectionTable.H"

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

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

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


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

Foam::RBD::restraints::externalForce::externalForce
(
    const word& name,
    const dictionary& dict,
    const rigidBodyModel& model
)
:
    restraint(name, dict, model),
    externalForce_(nullptr),
    location_(Zero)
{
    read(dict);
}


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

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


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

void Foam::RBD::restraints::externalForce::restrain
(
    scalarField& tau,
    Field<spatialVector>& fx,
    const rigidBodyModelState& state
) const
{
    const vector target = externalForce_().value(state.t());
    const vector gain = location_;
	vector moment = Zero;
	const label masterID = 2;
	Tensor<scalar> Rbody = model_.X0(masterID).E().T(), Rpera = model_.X0(bodyID_).E().T();
	Tensor<scalar> bodyRpera = Rbody.T() & Rpera;
	vector angular_vel = Rbody.T() & ( model_.v(bodyID_).w() - model_.v(masterID).w() );	/* in the body coordinate system */
	vector angular_accel = Rbody.T() & ( model_.a(bodyID_).w() - model_.a(masterID).w() );	/* in the body coordinate system */

	moment[0] = gain[0] * ( target[0] - angular_vel[0] ) + gain[1] * ( target[1] - angular_accel[0] );	/* x moment */

	Info<< endl << " target " << bodyID_ << ": " << target[0] << "(" << angular_vel[0] << "), " << target[1] << "(" << angular_accel[0] << "), moment " 
	    << moment[0] << endl;



    if (model_.debug)
    {
	Info<< " kikut: " << endl
	<< " ID " << bodyID_
	<< " Index " << bodyIndex_ 
	<< " masterID " << masterID << endl
	<< " gain " << gain
	<< " target velocity " << (target[0]*180/3.141592) 
	<< " target acceleration " << (target[1]*180/3.141592) << endl
	<< " body  " << model_.X0(masterID).E().T() << endl
	<< " pera  " << model_.X0(bodyID_).E().T() << endl
	<< " bodyRpera " << bodyRpera << endl
	<< " current x angular velocity " << ( angular_vel[0] * 180/3.141592 ) 
	<< " current x angular acceleration " << ( angular_accel[0] * 180/3.141592 ) << endl

	<< " body velocity " << model_.v(masterID)    
	<< " body acceleration " << model_.a(masterID)   << endl 
	<< " pera velocity " << model_.v(bodyID_)   
	<< " pera acceleration " << model_.a(bodyID_)  << endl

	<< " P  " << (gain[0] * ( target[0] - angular_vel[0] ) )
	<< " D  " << (gain[1] * ( target[1] - angular_accel[0] ) )
	
	<< " moment(P+D) " << moment[0] << endl;

    }

    // Accumulate the ONLY MOMENT for the restrained body
    fx[bodyIndex_] += spatialVector(Rpera & moment, Zero /*force*/ );
    fx[masterID] -= spatialVector(Rpera & moment, Zero /*force*/ );
}


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

    coeffs_.readEntry("location", location_);

    externalForce_ = Function1<vector>::New("force", coeffs_);

    return true;
}


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

    os.writeEntry("location", location_);

    externalForce_().writeData(os);
}


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