#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

/* ヘッダ用の構造体 */
typedef struct{
  double lx, ly, lz;
  double lmax;
  double lmin;
  double alpha, beta, gamma;
  double M[3][3];
  double M_[3][3];
  int atoms;
  int step;
  double volume;
  double rho;
}HEAD;

typedef struct{
  int id, type, type_new;
  char elem[4];
  double x, y, z;
}DATA;

typedef struct{
  int n;
  int m;
  double *x;
  double **y;
  double xmin, xmax, dx;
}DATASET;

typedef struct{
  int types; /* 原子タイプの数 */
  double *ci;    /* 原子分率 */
  double *rho;   /* 原子タイプ毎の数密度 */
  char **elem;    /* 原子タイプ毎の元素 */
  int *ids;
  int **pairid;  /* [0][0]->0 [0][1]->1 [1][0]->1 [1][1]->2... */
  double **XRDcoeff; /* X線散乱因子 */
  double *NDcoeff; /* 中性子散乱長 */
  int pairs;
  double *cicj;
  char **elemij;
  int **typeij;
  double **nij;
}TYPE;


typedef struct{
  char   filetype[1024];  /* lammps or xyz */
  double SQmin;
  double SQmax;
  double SQdel;
  double GRmin;
  double GRmax;
  double GRdel;
  char   infile[1024];
  char   outbase[1024];
  char   gr_file[1030];
  char   sq_file[1030];
  char   cn_file[1030];
  int    total_step;
}COMMAND;

int SetArgment(int argc, char **argv, COMMAND *com);
int ReadHeadLammpstrj(FILE *fp, HEAD *head);
int ReadDataLammpstrj(FILE *fp, HEAD *head, DATA *data);
int ReadHeadXYZ(FILE *fp, HEAD *head);
int ReadDataXYZ(FILE *fp, HEAD *head, DATA *data);

/* /\* 関数定義 *\/ */
void SetXRDCoeff(TYPE *type, DATASET *sq);
void SetNDCoeff(TYPE *type, DATASET *sq);
void Error();
void GetBaseName(char *infile, char *base);
void InitializeType(HEAD *head, DATA *data, TYPE *type);
void OutputGr(COMMAND *com, HEAD *head, TYPE *type, DATASET *gr);
void OutputCN(COMMAND *com, HEAD *head, TYPE *type, DATASET *cn);
void OutputSQ(COMMAND *com, HEAD *head, TYPE *type, DATASET *sq);
void CalcSq(HEAD *head, TYPE *type, DATASET *gr, DATASET *sq);
void OutputCoeff(COMMAND *com, HEAD *head, TYPE *type, DATASET *sq);
