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

    TRAVIS - Trajectory Analyzer and Visualizer

    http://www.travis-analyzer.de/

    Copyright (c) 2009-2026 Martin Brehm
                  2012-2017 Martin Thomas
                  2016-2020 Sascha Gehrke

    Please cite:  J. Chem. Phys. 2020, 152 (16), 164105.         (DOI 10.1063/5.0005078 )
                  J. Chem. Inf. Model. 2011, 51 (8), 2007-2023.  (DOI 10.1021/ci200217w )

    This file was written by Martin Brehm.

    ---------------------------------------------------------------------------

    This program 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.

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

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


#ifndef MICROHET2_H
#define MICROHET2_H


// This must always be the first include directive
#include "config.h"

#include "timestep.h"
#include "atomgroup.h"

#include <vector>
#include <queue>
#include <string>



#if ((__cplusplus >= 201103L) && defined(__GNUC__) && (__GNUC__ >= 6)) || (defined(_MSC_VER) && _MSC_VER >= 1900)

	#include <thread>
	#include <chrono>

	#define MICROHET_PARALLEL

#endif



class CMicroHet2Observation {
public:

	CMicroHet2Observation() {
	}

	~CMicroHet2Observation() {
	}

	void Parse();

	std::vector<int> m_iaAtoms;
	std::vector<double> m_faAtomRadii;
	std::vector<double> m_faAtomMasses;
	std::string m_sName;
	int m_iID;
	double m_fUniformDensity;
	double m_fDFEmptyCounter;

	C2DF *m_pCDF;
	C2DF *m_pCDFCount;
	std::vector<double> m_faDFEmpty;

	bool m_bMassDensity;
};



class CMicroHet2Thread {
public:

	CMicroHet2Thread()
		#ifdef MICROHET_PARALLEL
			: m_oThreadMutex(), m_oLock( m_oThreadMutex, std::defer_lock )
		#endif
	{
	}

	#ifdef MICROHET_PARALLEL
		bool DequeqeTimeStep( const CTimeStep **ts );
	#endif

	int m_iID;
	std::vector<std::vector<double> > m_faaTemp;
	std::vector<std::vector<double> > m_faaTempCount;
	std::queue<const CTimeStep*> m_qTimeSteps;
	bool m_bWaiting;

	#ifdef MICROHET_PARALLEL
		std::thread *m_pThread;
		mutable std::mutex m_oThreadMutex;
		std::unique_lock<std::mutex> m_oLock;
		std::condition_variable m_oCondition;
	#endif
};



class CMicroHet2 {
public:

	CMicroHet2() {
		#ifdef MICROHET_PARALLEL
			m_iThreads = 1;
			m_bThreadsFinished = false;
		#endif
	}

	~CMicroHet2() {
	}

	void Parse();

	void Create();

	#ifdef MICROHET_PARALLEL
		void ThreadMain( CMicroHet2Thread *thr );
	#endif

	void ThreadWork( CMicroHet2Thread *thr, const CTimeStep *ts );

	void ProcessStep( CTimeStep *ts );

	void Finish();

	std::vector<CMicroHet2Observation*> m_oaObservations;

	int m_iRadiusCount;
	int m_iSampling;
	double m_fRadiusMin;
	double m_fRadiusMax;

	bool m_bMassDensity;

	std::vector<C2DF*> m_paCrossCDF;

	std::vector<CMicroHet2Thread*> m_oaThreads;

	#ifdef MICROHET_PARALLEL
		int m_iThreads;
		mutable std::mutex m_oGlobalMutex;
		bool m_bThreadsFinished;
	#endif

};




#endif




