#include "stdafx.h"
#include "math.h"
#include "UnsDis.h"

#include "UnsDisDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
IMPLEMENT_SERIAL (CLayer,CObject,1);

CLayer::CLayer() {stn=1.0f;kd=0.0f;conin=0.0f;};

CLayer::CLayer (float z1,float z2,int n,float thets,float thetr,float nn,float al,float ks)
{
	zbeg=z1;zend=z2;nel=n;
	ths=thets;thr=thetr;wn=nn;alpha=al;ksat=ks;
}

float CLayer::Kuns (float psi)
{
	float se,am,pw1,pw2;
	if (psi<0.0f)
	{
		se=Se (psi);
		am=1.0f-1.0f/wn;
		pw1=1.0f-(float)pow(se,1.0f/am);
		pw2=1.0f-(float)pow(pw1,am);
		return (ksat*sqrt(se)*pw2*pw2);
	}
	return (ksat);
}

float CLayer::Se(float psi)
{
	float am,pw1,pw2;
	if (psi<0.0f)
	{
		am=1.0f/wn-1.0f;
		pw1=pow(fabs(alpha*psi),wn);
		pw2=pow (1.0f+pw1,am);
		return (pw2);
	}
	return(1.0f);
}

float CLayer::Theta (float psi)
{
	float tks;
	if (psi<0.0) tks=thr+(ths-thr)*Se(psi);
	else tks=ths;
//	if (tks>efpo) tks=efpo;
	return (tks);
}

float CLayer::Capac(float psi)
{
	float a,b,pw1,m;
	if (psi<0.0)
	{
		m=1.0f/wn-1.0f;
		a=fabs(alpha*psi);pw1=1.0f+pow(a,wn);
		b=pow(pw1,m-1.0f);
//	pw1=m*wn*pow(aw,wn)*pow(psi,n-1)*b;
//	return(pw1);
		pw1=pow(a,wn-1.0f);
		return (alpha*b*(ths-thr)*(wn-1.0f)*pw1);
	}
	return (0.0f);
}

void CLayer::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		ar << zbeg << zend << nel << alpha << wn << ths << thr << ksat << disp << ro << kd << stn << potin << conin; 
	}
	else
	{
		ar >> zbeg >> zend >> nel >> alpha >> wn >> ths >> thr >> ksat >> disp >> ro >> kd >> stn >> potin >> conin; 
	}
}

/////////////////////////////////////////////////////////////////////////////
IMPLEMENT_SERIAL (CTimeStep,CObject,1);

CTimeStep::CTimeStep() {};

CTimeStep::CTimeStep(float tim)
{
	time=tim;
	int i=0;
	while (i<Results.GetSize())
	{
		delete Results.GetAt(i++);
	}	                     
	Results.RemoveAll();
}

CTimeStep::~CTimeStep() 
{
	int i=0;
	while (i<Results.GetSize())
	{
		delete Results.GetAt(i++);
	}	                     
	Results.RemoveAll();
}

CResult* CTimeStep::GetResult(int i)
{
	return (CResult*)Results.GetAt(i);
}

void CTimeStep::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		ar << time; 
	}
	else
	{
		ar >> time; 
	}
	Results.Serialize(ar);
}

/////////////////////////////////////////////////////////////////////////////
IMPLEMENT_SERIAL (CResult,CObject,1);

CResult::CResult() {};

CResult::CResult(float ps)
{
	psi=ps;theta=conc=cons=0.0f;
}

void CResult::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		ar << psi << theta << conc << cons; 
	}
	else
	{
		ar >> psi >> theta >> conc >> cons; 
	}
}

/////////////////////////////////////////////////////////////////////////////
IMPLEMENT_SERIAL (CElement,CObject,1);

CElement::CElement() {nlay=0;};

CElement::CElement(float zp1, float zp2)
{
	z1=zp1;z2=zp2;nlay=-1;
}

float CElement::leng()
{
	return z2-z1;
}

void CElement::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		ar << z1 << z2 << nlay; 
	}
	else
	{
		ar >> z1 >> z2 >> nlay; 
	}
}
