#ifndef NONLINCF_H
#define NONLINCF_H


#include <fem.hpp>
#include <solve.hpp>
#include <ngstd.hpp>
#include <vector>
#include <cmath>
#include <memory>
#include <iostream>
#include <cmath>
#include <cstring>


#include "../basicClasses/ngData_class.h"
#include "../basicClasses/magCurve.h"


using namespace ngstd;
using namespace ngfem;
using namespace ngsolve;
using namespace ngcomp;

// #define _Pi 3.141592653589793

template <unsigned int DIM>
std::pair<unsigned int, unsigned int> findNearestNeighbour(const vector<vector<MappedIntegrationPoint<DIM, DIM>> > & mips, const BaseMappedIntegrationPoint& mip);

template <typename T, unsigned int DIM>
class nonLinCF:public ngScalar_class<T, DIM>
{ 
            
    public:
        nonLinCF(shared_ptr<MeshAccess>& mesh, IntegrationRule& intrule, 
                        const shared_ptr<CoefficientFunction>& ptrInput, 
                        vector<double> H_KL, 
                        vector<double> B_KL, 
                        const std::string cFieldIn="H", const std::string cFieldOut="B", 
                        const shared_ptr<CoefficientFunction>& ptrMask=nullptr, unsigned int order=1);


        ~nonLinCF() = default;



        bool HasConverged(double aMeanError, double aMaxError);

        void Update();
        
        void UpdateEnergyDensity();
        const shared_ptr<ngScalar_class<T, DIM>> GetEnergyDensity();
        const shared_ptr<ngScalar_class<T, DIM>> GetIterationError();



        unsigned int getOrder() const;
        KL GetKL();

        shared_ptr<CoefficientFunction> GetInputCF() const;
        void SetInputCF(shared_ptr<CoefficientFunction>) ;

      

    private:
        

        shared_ptr<CoefficientFunction> ptrInput;
        
        const std::string sFieldIn;        
        const std::string sFieldOut;

        using ngScalar_class<T, DIM>::data;
        using ngScalar_class<T, DIM>::mips;

        // data
        shared_ptr<ngScalar_class<double, DIM>> ptrError;
        shared_ptr<ngScalar_class<T, DIM>> ptrEnergyDens;

        
        std::shared_ptr<KL>  oKL;


        double meanError;
        double maxError;

};

#endif /* NONLINCF_H */