/*
writeout.c

write oth.out
*/

#include "oth.h"

void writeout(FILE *fp)
{
	size_t num = 0;

	num += fwrite(Title,       sizeof(char),      256, fp);
	num += fwrite(&Nx,         sizeof(int),         1, fp);
	num += fwrite(&Ny,         sizeof(int),         1, fp);
	num += fwrite(&Nz,         sizeof(int),         1, fp);
	num += fwrite(&NE,         sizeof(int64_t),     1, fp);
	num += fwrite(&NH,         sizeof(int64_t),     1, fp);
	num += fwrite(&NEXI,       sizeof(int64_t),     1, fp);
	num += fwrite(&NEXJ,       sizeof(int64_t),     1, fp);
	num += fwrite(&NEXK,       sizeof(int64_t),     1, fp);
	num += fwrite(&NEX0,       sizeof(int64_t),     1, fp);
	num += fwrite(&NEYI,       sizeof(int64_t),     1, fp);
	num += fwrite(&NEYJ,       sizeof(int64_t),     1, fp);
	num += fwrite(&NEYK,       sizeof(int64_t),     1, fp);
	num += fwrite(&NEY0,       sizeof(int64_t),     1, fp);
	num += fwrite(&NEZI,       sizeof(int64_t),     1, fp);
	num += fwrite(&NEZJ,       sizeof(int64_t),     1, fp);
	num += fwrite(&NEZK,       sizeof(int64_t),     1, fp);
	num += fwrite(&NEZ0,       sizeof(int64_t),     1, fp);
	num += fwrite(&NHXI,       sizeof(int64_t),     1, fp);
	num += fwrite(&NHXJ,       sizeof(int64_t),     1, fp);
	num += fwrite(&NHXK,       sizeof(int64_t),     1, fp);
	num += fwrite(&NHX0,       sizeof(int64_t),     1, fp);
	num += fwrite(&NHYI,       sizeof(int64_t),     1, fp);
	num += fwrite(&NHYJ,       sizeof(int64_t),     1, fp);
	num += fwrite(&NHYK,       sizeof(int64_t),     1, fp);
	num += fwrite(&NHY0,       sizeof(int64_t),     1, fp);
	num += fwrite(&NHZI,       sizeof(int64_t),     1, fp);
	num += fwrite(&NHZJ,       sizeof(int64_t),     1, fp);
	num += fwrite(&NHZK,       sizeof(int64_t),     1, fp);
	num += fwrite(&NHZ0,       sizeof(int64_t),     1, fp);
	num += fwrite(&NMaterial,  sizeof(int),         1, fp);
	num += fwrite(&NFeed,      sizeof(int),         1, fp);
	num += fwrite(&IPlanewave, sizeof(int),         1, fp);
	num += fwrite(&NPoint,     sizeof(int),         1, fp);
	num += fwrite(&NFrequency, sizeof(int),         1, fp);
	num += fwrite(&Planewave,  sizeof(planewave_t), 1, fp);
	num += fwrite(&Pbc.xpbc,   sizeof(int),         1, fp);
	num += fwrite(&Pbc.ypbc,   sizeof(int),         1, fp);
	num += fwrite(&Pbc.zpbc,   sizeof(int),         1, fp);
	num += fwrite(&NGline,     sizeof(int),         1, fp);

	num += fwrite(Xn,          sizeof(double),      Nx + 1,                 fp);
	num += fwrite(Yn,          sizeof(double),      Ny + 1,                 fp);
	num += fwrite(Zn,          sizeof(double),      Nz + 1,                 fp);
	num += fwrite(Xc,          sizeof(double),      Nx,                     fp);
	num += fwrite(Yc,          sizeof(double),      Ny,                     fp);
	num += fwrite(Zc,          sizeof(double),      Nz,                     fp);
	num += fwrite(DXn,         sizeof(double),      Nx + 1,                 fp);
	num += fwrite(DYn,         sizeof(double),      Ny + 1,                 fp);
	num += fwrite(DZn,         sizeof(double),      Nz + 1,                 fp);
	num += fwrite(DXc,         sizeof(double),      Nx,                     fp);
	num += fwrite(DYc,         sizeof(double),      Ny,                     fp);
	num += fwrite(DZc,         sizeof(double),      Nz,                     fp);
	num += fwrite(Material,    sizeof(material_t),  NFrequency * NMaterial, fp);
	num += fwrite(Feed,        sizeof(feed_t),      NFeed,                  fp);
	num += fwrite(Point,       sizeof(point_t),     NPoint,                 fp);
	num += fwrite(Frequency,   sizeof(double),      NFrequency,             fp);
	num += fwrite(NIter,       sizeof(int),         NFrequency,             fp);
	num += fwrite(Zin,         sizeof(d_complex_t), NFrequency * NFeed,     fp);
	num += fwrite(Ref,         sizeof(double),      NFrequency * NFeed,     fp);
	num += fwrite(Spara,       sizeof(d_complex_t), NFrequency * NPoint,    fp);
	num += fwrite(PLoss,       sizeof(double),      NFrequency,             fp);
	num += fwrite(Gline,       sizeof(double),      NGline * 2 * 3,         fp);

	for (int ifreq = 0; ifreq < NFrequency; ifreq++) {
		num += fwrite(Residual[ifreq], sizeof(double), NIter[ifreq], fp);
		num += fwrite(Eaverage[ifreq], sizeof(double), NIter[ifreq], fp);
	}

	num += fwrite(mE, sizeof(int), NE, fp);
	num += fwrite(mH, sizeof(int), NH, fp);
	//printf("%zd %zd %d\n", NE, NH, 3 * Nx * Ny * Nz);

	for (int ifreq = 0; ifreq < NFrequency; ifreq++) {
		for (int i = 0; i < Nx + 0; i++) {
			for (int j = 0; j < Ny + 1; j++) {
				num += fwrite(Ex[ifreq][i][j], sizeof(f_complex_t), Nz + 1, fp);
			}
		}
		for (int i = 0; i < Nx + 1; i++) {
			for (int j = 0; j < Ny + 0; j++) {
				num += fwrite(Ey[ifreq][i][j], sizeof(f_complex_t), Nz + 1, fp);
			}
		}
		for (int i = 0; i < Nx + 1; i++) {
			for (int j = 0; j < Ny + 1; j++) {
				num += fwrite(Ez[ifreq][i][j], sizeof(f_complex_t), Nz + 0, fp);
			}
		}
	}

	fwrite(&num, sizeof(size_t), 1, fp);
}
