/////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2020-     Equinor ASA
//
//  ResInsight 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.
//
//  ResInsight 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 at <http://www.gnu.org/licenses/gpl.html>
//  for more details.
//
/////////////////////////////////////////////////////////////////////////////////

#include "RigFemPartResultCalculatorStressAnisotropy.h"

#include "RigFemPart.h"
#include "RigFemPartCollection.h"
#include "RigFemPartResultsCollection.h"
#include "RigFemResultAddress.h"
#include "RigFemScalarResultFrames.h"

#include "cafProgressInfo.h"

#include <QString>

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RigFemPartResultCalculatorStressAnisotropy::RigFemPartResultCalculatorStressAnisotropy( RigFemPartResultsCollection& collection )
    : RigFemPartResultCalculator( collection )
{
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RigFemPartResultCalculatorStressAnisotropy::~RigFemPartResultCalculatorStressAnisotropy()
{
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RigFemPartResultCalculatorStressAnisotropy::isMatching( const RigFemResultAddress& resVarAddr ) const
{
    return isAnisotropyResult( resVarAddr );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RigFemScalarResultFrames* RigFemPartResultCalculatorStressAnisotropy::calculate( int partIndex, const RigFemResultAddress& resVarAddr )
{
    CVF_ASSERT( isMatching( resVarAddr ) );

    caf::ProgressInfo stepCountProgress( m_resultCollection->timeStepCount() * 4, "" );
    stepCountProgress.setProgressDescription( "Calculating " +
                                              QString::fromStdString( resVarAddr.fieldName + ": " + resVarAddr.componentName ) );

    RigFemScalarResultFrames* s1Frames = nullptr;
    {
        auto task = stepCountProgress.task( "Loading S1.", m_resultCollection->timeStepCount() );
        s1Frames  = m_resultCollection->findOrLoadScalarResult( partIndex,
                                                               RigFemResultAddress( resVarAddr.resultPosType, resVarAddr.fieldName, "S1" ) );
    }

    RigFemScalarResultFrames* s2Frames = nullptr;
    {
        auto task = stepCountProgress.task( "Loading S2.", m_resultCollection->timeStepCount() );
        s2Frames  = m_resultCollection->findOrLoadScalarResult( partIndex,
                                                               RigFemResultAddress( resVarAddr.resultPosType, resVarAddr.fieldName, "S2" ) );
    }

    RigFemScalarResultFrames* s3Frames = nullptr;
    {
        auto task = stepCountProgress.task( "Loading S3.", m_resultCollection->timeStepCount() );
        s3Frames  = m_resultCollection->findOrLoadScalarResult( partIndex,
                                                               RigFemResultAddress( resVarAddr.resultPosType, resVarAddr.fieldName, "S3" ) );
    }

    RigFemScalarResultFrames* s12Frames =
        m_resultCollection->createScalarResult( partIndex, RigFemResultAddress( resVarAddr.resultPosType, resVarAddr.fieldName, "SA12" ) );
    RigFemScalarResultFrames* s13Frames =
        m_resultCollection->createScalarResult( partIndex, RigFemResultAddress( resVarAddr.resultPosType, resVarAddr.fieldName, "SA13" ) );
    RigFemScalarResultFrames* s23Frames =
        m_resultCollection->createScalarResult( partIndex, RigFemResultAddress( resVarAddr.resultPosType, resVarAddr.fieldName, "SA23" ) );

    const int timeSteps = s1Frames->timeStepCount();
    for ( int stepIdx = 0; stepIdx < timeSteps; stepIdx++ )
    {
        auto task = stepCountProgress.task( QString( "Calculating %1/%2." ).arg( stepIdx ).arg( timeSteps - 1 ) );

        const int frameCount = s1Frames->frameCount( stepIdx );
        for ( int fIdx = 0; fIdx < frameCount; fIdx++ )
        {
            const std::vector<float>& s1 = s1Frames->frameData( stepIdx, fIdx );
            const std::vector<float>& s2 = s2Frames->frameData( stepIdx, fIdx );
            const std::vector<float>& s3 = s3Frames->frameData( stepIdx, fIdx );

            std::vector<float>& s12 = s12Frames->frameData( stepIdx, fIdx );
            std::vector<float>& s13 = s13Frames->frameData( stepIdx, fIdx );
            std::vector<float>& s23 = s23Frames->frameData( stepIdx, fIdx );

            size_t valCount = s1.size();

            s12.resize( valCount );
            s13.resize( valCount );
            s23.resize( valCount );

#pragma omp parallel for schedule( dynamic )
            for ( long vIdx = 0; vIdx < static_cast<long>( valCount ); ++vIdx )
            {
                s12[vIdx] = 2.0 * ( s1[vIdx] - s2[vIdx] ) / ( s1[vIdx] + s2[vIdx] );
                s13[vIdx] = 2.0 * ( s1[vIdx] - s3[vIdx] ) / ( s1[vIdx] + s3[vIdx] );
                s23[vIdx] = 2.0 * ( s2[vIdx] - s3[vIdx] ) / ( s2[vIdx] + s3[vIdx] );
            }
        }
    }
    RigFemScalarResultFrames* requestedStress = m_resultCollection->findOrLoadScalarResult( partIndex, resVarAddr );
    return requestedStress;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RigFemScalarResultFrames* RigFemPartResultCalculatorStressAnisotropy::calculateTimeLapse( int partIndex, const RigFemResultAddress& resVarAddr )
{
    CVF_ASSERT( isMatching( resVarAddr ) );

    caf::ProgressInfo stepCountProgress( m_resultCollection->timeStepCount() * 4, "" );
    stepCountProgress.setProgressDescription( "Calculating " +
                                              QString::fromStdString( resVarAddr.fieldName + ": " + resVarAddr.componentName ) );

    RigFemScalarResultFrames* s1Frames = nullptr;
    {
        auto task = stepCountProgress.task( "Loading S1.", m_resultCollection->timeStepCount() );
        s1Frames  = m_resultCollection->findOrLoadScalarResult( partIndex,
                                                               RigFemResultAddress( resVarAddr.resultPosType, resVarAddr.fieldName, "S1" ) );
    }

    RigFemScalarResultFrames* s2Frames = nullptr;
    {
        auto task = stepCountProgress.task( "Loading S2.", m_resultCollection->timeStepCount() );
        s2Frames  = m_resultCollection->findOrLoadScalarResult( partIndex,
                                                               RigFemResultAddress( resVarAddr.resultPosType, resVarAddr.fieldName, "S2" ) );
    }

    RigFemScalarResultFrames* s3Frames = nullptr;
    {
        auto task = stepCountProgress.task( "Loading S3.", m_resultCollection->timeStepCount() );
        s3Frames  = m_resultCollection->findOrLoadScalarResult( partIndex,
                                                               RigFemResultAddress( resVarAddr.resultPosType, resVarAddr.fieldName, "S3" ) );
    }

    RigFemScalarResultFrames* s12Frames = m_resultCollection->createScalarResult( partIndex,
                                                                                  RigFemResultAddress( resVarAddr.resultPosType,
                                                                                                       resVarAddr.fieldName,
                                                                                                       "SA12",
                                                                                                       resVarAddr.timeLapseBaseStepIdx ) );
    RigFemScalarResultFrames* s13Frames = m_resultCollection->createScalarResult( partIndex,
                                                                                  RigFemResultAddress( resVarAddr.resultPosType,
                                                                                                       resVarAddr.fieldName,
                                                                                                       "SA13",
                                                                                                       resVarAddr.timeLapseBaseStepIdx ) );
    RigFemScalarResultFrames* s23Frames = m_resultCollection->createScalarResult( partIndex,
                                                                                  RigFemResultAddress( resVarAddr.resultPosType,
                                                                                                       resVarAddr.fieldName,
                                                                                                       "SA23",
                                                                                                       resVarAddr.timeLapseBaseStepIdx ) );

    constexpr float inf = std::numeric_limits<float>::infinity();
    int             baseTimeStep, baseFrame;
    std::tie( baseTimeStep, baseFrame ) = m_resultCollection->stepListIndexToTimeStepAndDataFrameIndex( resVarAddr.timeLapseBaseStepIdx );

    const int timeSteps = s1Frames->timeStepCount();
    for ( int stepIdx = 0; stepIdx < timeSteps; stepIdx++ )
    {
        auto task = stepCountProgress.task( QString( "Calculating %1 of %2." ).arg( stepIdx ).arg( timeSteps - 1 ) );

        const int frameCount = s1Frames->frameCount( stepIdx );
        for ( int fIdx = 0; fIdx < frameCount; fIdx++ )
        {
            const std::vector<float>& s1t = s1Frames->frameData( stepIdx, fIdx );
            const std::vector<float>& s2t = s2Frames->frameData( stepIdx, fIdx );
            const std::vector<float>& s3t = s3Frames->frameData( stepIdx, fIdx );

            const std::vector<float>& s1b = s1Frames->frameData( baseTimeStep, baseFrame );
            const std::vector<float>& s2b = s2Frames->frameData( baseTimeStep, baseFrame );
            const std::vector<float>& s3b = s3Frames->frameData( baseTimeStep, baseFrame );

            std::vector<float>& s12 = s12Frames->frameData( stepIdx, fIdx );
            std::vector<float>& s13 = s13Frames->frameData( stepIdx, fIdx );
            std::vector<float>& s23 = s23Frames->frameData( stepIdx, fIdx );

            size_t valCount = s1t.size();

            s12.resize( valCount, 0.0 );
            s13.resize( valCount, 0.0 );
            s23.resize( valCount, 0.0 );

            double epsilon = 0.0000001;
#pragma omp parallel for schedule( dynamic )
            for ( long vIdx = 0; vIdx < static_cast<long>( valCount ); ++vIdx )
            {
                if ( fIdx != baseTimeStep )
                {
                    double diffS1 = s1t[vIdx] - s1b[vIdx];
                    double diffS2 = s2t[vIdx] - s2b[vIdx];
                    double diffS3 = s3t[vIdx] - s3b[vIdx];
                    if ( std::abs( diffS1 + diffS2 ) > epsilon )
                        s12[vIdx] = 2.0 * ( diffS1 - diffS2 ) / ( diffS1 + diffS2 );
                    else
                        s12[vIdx] = inf;

                    if ( std::abs( diffS1 + diffS3 ) > epsilon )
                        s13[vIdx] = 2.0 * ( diffS1 - diffS3 ) / ( diffS1 + diffS3 );
                    else
                        s13[vIdx] = inf;

                    if ( std::abs( diffS2 + diffS3 ) > epsilon )
                        s23[vIdx] = 2.0 * ( diffS2 - diffS3 ) / ( diffS2 + diffS3 );
                    else
                        s23[vIdx] = inf;
                }
            }
        }
    }
    RigFemScalarResultFrames* requestedStress = m_resultCollection->findOrLoadScalarResult( partIndex, resVarAddr );
    return requestedStress;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RigFemPartResultCalculatorStressAnisotropy::isAnisotropyResult( const RigFemResultAddress& resVarAddr )
{
    return ( ( ( resVarAddr.fieldName == "ST" || resVarAddr.fieldName == "SE" ) &&
               ( resVarAddr.componentName == "SA12" || resVarAddr.componentName == "SA13" || resVarAddr.componentName == "SA23" ) ) );
}
