#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
+#include <sys/time.h>
+#include <time.h>
#include <math.h>
#include "moldyn.h"
memset(moldyn,0,sizeof(t_moldyn));
+ moldyn->argc=argc;
+ moldyn->args=argv;
+
rand_init(&(moldyn->random),NULL,1);
moldyn->random.status|=RAND_STAT_VERBOSE;
atom[ret].brand=brand;
atom[ret].tag=count+ret;
check_per_bound(moldyn,&(atom[ret].r));
+ atom[ret].r_0=atom[ret].r;
}
/* update total system mass */
return ret;
}
+int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
+ t_3dvec *r,t_3dvec *v) {
+
+ t_atom *atom;
+ void *ptr;
+ int count;
+
+ atom=moldyn->atom;
+ count=(moldyn->count)++;
+
+ ptr=realloc(atom,(count+1)*sizeof(t_atom));
+ if(!ptr) {
+ perror("[moldyn] realloc (add atom)");
+ return -1;
+ }
+ moldyn->atom=ptr;
+
+ atom=moldyn->atom;
+ atom[count].r=*r;
+ atom[count].v=*v;
+ atom[count].element=element;
+ atom[count].mass=mass;
+ atom[count].brand=brand;
+ atom[count].tag=count;
+ atom[count].attr=attr;
+ check_per_bound(moldyn,&(atom[count].r));
+ atom[count].r_0=atom[count].r;
+
+ /* update total system mass */
+ total_mass_calc(moldyn);
+
+ return 0;
+}
+
+int del_atom(t_moldyn *moldyn,int tag) {
+
+ t_atom *new,*old;
+ int cnt;
+
+ old=moldyn->atom;
+
+ new=(t_atom *)malloc((moldyn->count-1)*sizeof(t_atom));
+ if(!new) {
+ perror("[moldyn]malloc (del atom)");
+ return -1;
+ }
+
+ for(cnt=0;cnt<tag;cnt++)
+ new[cnt]=old[cnt];
+
+ for(cnt=tag+1;cnt<moldyn->count;cnt++) {
+ new[cnt-1]=old[cnt];
+ new[cnt-1].tag=cnt-1;
+ }
+
+ moldyn->count-=1;
+ moldyn->atom=new;
+
+ free(old);
+
+ return 0;
+}
+
/* cubic init */
int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
return count;
}
-int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
- t_3dvec *r,t_3dvec *v) {
-
- t_atom *atom;
- void *ptr;
- int count;
-
- atom=moldyn->atom;
- count=(moldyn->count)++;
-
- ptr=realloc(atom,(count+1)*sizeof(t_atom));
- if(!ptr) {
- perror("[moldyn] realloc (add atom)");
- return -1;
- }
- moldyn->atom=ptr;
-
- atom=moldyn->atom;
- atom[count].r=*r;
- atom[count].v=*v;
- atom[count].element=element;
- atom[count].mass=mass;
- atom[count].brand=brand;
- atom[count].tag=count;
- atom[count].attr=attr;
-
- /* update total system mass */
- total_mass_calc(moldyn);
-
- return 0;
-}
-
int destroy_atoms(t_moldyn *moldyn) {
if(moldyn->atom) free(moldyn->atom);
return p;
}
-double pressure_calc(t_moldyn *moldyn) {
+double virial_sum(t_moldyn *moldyn) {
int i;
double v;
t_virial *virial;
+ /* virial (sum over atom virials) */
+ v=0.0;
+ for(i=0;i<moldyn->count;i++) {
+ virial=&(moldyn->atom[i].virial);
+ v+=(virial->xx+virial->yy+virial->zz);
+ }
+ moldyn->virial=v;
+
+ /* global virial (absolute coordinates) */
+ virial=&(moldyn->gvir);
+ moldyn->gv=virial->xx+virial->yy+virial->zz;
+
+ return moldyn->virial;
+}
+
+double pressure_calc(t_moldyn *moldyn) {
+
/*
* PV = NkT + <W>
* with W = 1/3 sum_i f_i r_i (- skipped!)
* => P = (2 Ekin + virial) / (3V)
*/
- v=0.0;
- for(i=0;i<moldyn->count;i++) {
- virial=&(moldyn->atom[i].virial);
- v+=(virial->xx+virial->yy+virial->zz);
- }
+ /* assume up to date virial & up to date kinetic energy */
- /* virial sum and average virial */
- if(moldyn->total_steps>=moldyn->avg_skip) {
- moldyn->virial_sum+=v;
- moldyn->virial_avg=moldyn->virial_sum/
- (moldyn->total_steps+1-moldyn->avg_skip);
- moldyn->p=2.0*moldyn->k_avg+moldyn->virial_avg;
- moldyn->p/=(3.0*moldyn->volume);
- moldyn->p_sum+=moldyn->p;
- moldyn->p_avg=moldyn->p_sum/
- (moldyn->total_steps+1-moldyn->avg_skip);
- }
+ /* pressure (atom virials) */
+ moldyn->p=2.0*moldyn->ekin+moldyn->virial;
+ moldyn->p/=(3.0*moldyn->volume);
- /* pressure from 'absolute coordinates' virial */
- virial=&(moldyn->virial);
- v=virial->xx+virial->yy+virial->zz;
- moldyn->gp=2.0*moldyn->ekin+v;
+ /* pressure (absolute coordinates) */
+ moldyn->gp=2.0*moldyn->ekin+moldyn->gv;
moldyn->gp/=(3.0*moldyn->volume);
- if(moldyn->total_steps>=moldyn->avg_skip) {
- moldyn->gp_sum+=moldyn->gp;
- moldyn->gp_avg=moldyn->gp_sum/
- (moldyn->total_steps+1-moldyn->avg_skip);
- }
return moldyn->p;
}
if(moldyn->total_steps<moldyn->avg_skip)
return 0;
+ int denom=moldyn->total_steps+1-moldyn->avg_skip;
+
/* assume up to date energies, temperature, pressure etc */
/* kinetic energy */
moldyn->k_sum+=moldyn->ekin;
moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
- moldyn->k_avg=moldyn->k_sum/(moldyn->total_steps+1-moldyn->avg_skip);
- moldyn->k2_avg=moldyn->k2_sum/(moldyn->total_steps+1-moldyn->avg_skip);
+ moldyn->k_avg=moldyn->k_sum/denom;
+ moldyn->k2_avg=moldyn->k2_sum/denom;
moldyn->dk2_avg=moldyn->k2_avg-(moldyn->k_avg*moldyn->k_avg);
/* potential energy */
moldyn->v_sum+=moldyn->energy;
moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
- moldyn->v_avg=moldyn->v_sum/(moldyn->total_steps+1-moldyn->avg_skip);
- moldyn->v2_avg=moldyn->v2_sum/(moldyn->total_steps+1-moldyn->avg_skip);
+ moldyn->v_avg=moldyn->v_sum/denom;
+ moldyn->v2_avg=moldyn->v2_sum/denom;
moldyn->dv2_avg=moldyn->v2_avg-(moldyn->v_avg*moldyn->v_avg);
/* temperature */
moldyn->t_sum+=moldyn->t;
- moldyn->t_avg=moldyn->t_sum/(moldyn->total_steps+1-moldyn->avg_skip);
+ moldyn->t_avg=moldyn->t_sum/denom;
/* virial */
-
+ moldyn->virial_sum+=moldyn->virial;
+ moldyn->virial_avg=moldyn->virial_sum/denom;
+ moldyn->gv_sum+=moldyn->gv;
+ moldyn->gv_avg=moldyn->gv_sum/denom;
/* pressure */
-
+ moldyn->p_sum+=moldyn->p;
+ moldyn->p_avg=moldyn->p_sum/denom;
+ moldyn->gp_sum+=moldyn->gp;
+ moldyn->gp_avg=moldyn->gp_sum/denom;
return 0;
}
atom=moldyn->atom;
moldyn->ekin=0.0;
- for(i=0;i<moldyn->count;i++)
- moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
+ for(i=0;i<moldyn->count;i++) {
+ atom[i].ekin=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
+ moldyn->ekin+=atom[i].ekin;
+ }
return moldyn->ekin;
}
char dir[128];
double ds;
double energy_scale;
+ struct timeval t1,t2;
//double tp;
sched=&(moldyn->schedule);
moldyn->tau_square=moldyn->tau*moldyn->tau;
moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
- /* energy scaling factor */
- energy_scale=moldyn->count*EV;
+ /* get current time */
+ gettimeofday(&t1,NULL);
/* calculate initial forces */
potential_force_calc(moldyn);
printf("[moldyn] integration start, go get a coffee ...\n");
/* executing the schedule */
- for(sched->count=0;sched->count<sched->total_sched;sched->count++) {
+ sched->count=0;
+ while(sched->count<sched->total_sched) {
/* setting amount of runs and finite time step size */
moldyn->tau=sched->tau[sched->count];
moldyn->tau_square=moldyn->tau*moldyn->tau;
moldyn->time_steps=sched->runs[sched->count];
+ /* energy scaling factor (might change!) */
+ energy_scale=moldyn->count*EV;
+
/* integration according to schedule */
for(i=0;i<moldyn->time_steps;i++) {
/* calculate kinetic energy, temperature and pressure */
e_kin_calc(moldyn);
temperature_calc(moldyn);
+ virial_sum(moldyn);
pressure_calc(moldyn);
average_and_fluctuation_calc(moldyn);
/* check for log & visualization */
if(e) {
- if(!(i%e))
+ if(!(moldyn->total_steps%e))
dprintf(moldyn->efd,
"%f %f %f %f\n",
moldyn->time,moldyn->ekin/energy_scale,
get_total_energy(moldyn)/energy_scale);
}
if(m) {
- if(!(i%m)) {
+ if(!(moldyn->total_steps%m)) {
momentum=get_total_p(moldyn);
dprintf(moldyn->mfd,
"%f %f %f %f %f\n",moldyn->time,
}
}
if(p) {
- if(!(i%p)) {
+ if(!(moldyn->total_steps%p)) {
dprintf(moldyn->pfd,
"%f %f %f %f %f\n",moldyn->time,
moldyn->p/BAR,moldyn->p_avg/BAR,
}
}
if(t) {
- if(!(i%t)) {
+ if(!(moldyn->total_steps%t)) {
dprintf(moldyn->tfd,
"%f %f %f\n",
moldyn->time,moldyn->t,moldyn->t_avg);
}
}
if(s) {
- if(!(i%s)) {
+ if(!(moldyn->total_steps%s)) {
snprintf(dir,128,"%s/s-%07.f.save",
moldyn->vlsdir,moldyn->time);
- fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT);
+ fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT,
+ S_IRUSR|S_IWUSR);
if(fd<0) perror("[moldyn] save fd open");
else {
write(fd,moldyn,sizeof(t_moldyn));
}
}
if(v) {
- if(!(i%v)) {
+ if(!(moldyn->total_steps%v)) {
visual_atoms(&(moldyn->vis),moldyn->time,
moldyn->atom,moldyn->count);
}
}
/* display progress */
- if(!(i%10)) {
- printf("\rsched: %d, steps: %d, T: %f, P: %f %f V: %f",
- sched->count,i,
- moldyn->t_avg,
- //moldyn->p_avg/BAR,
- moldyn->p/BAR,
- moldyn->gp_avg/BAR,
- moldyn->volume);
- fflush(stdout);
+ if(!(moldyn->total_steps%10)) {
+ /* get current time */
+ gettimeofday(&t2,NULL);
+
+ printf("\rsched:%d, steps:%d, T:%3.1f/%3.1f P:%4.1f/%4.1f V:%6.1f (%d)",
+ sched->count,i,
+ moldyn->t,moldyn->t_avg,
+ moldyn->p_avg/BAR,moldyn->gp_avg/BAR,
+ moldyn->volume,
+ (int)(t2.tv_sec-t1.tv_sec));
+ fflush(stdout);
+
+ /* copy over time */
+ t1=t2;
}
/* increase absolute time */
}
/* check for hooks */
- if(sched->count+1<sched->total_sched)
- if(sched->hook) {
- printf("\n ## schedule hook %d/%d start ##\n",
- sched->count+1,sched->total_sched);
- sched->hook(moldyn,sched->hook_params);
- printf(" ## schedule hook end ##\n");
- }
+ if(sched->hook) {
+ printf("\n ## schedule hook %d/%d start ##\n",
+ sched->count+1,sched->total_sched-1);
+ sched->hook(moldyn,sched->hook_params);
+ printf(" ## schedule hook end ##\n");
+ }
+
+ /* increase the schedule counter */
+ sched->count+=1;
}
moldyn->energy=0.0;
/* reset global virial */
- memset(&(moldyn->virial),0,sizeof(t_virial));
+ memset(&(moldyn->gvir),0,sizeof(t_virial));
/* reset force, site energy and virial of every atom */
for(i=0;i<count;i++) {
/* calculate global virial */
for(i=0;i<count;i++) {
- moldyn->virial.xx+=moldyn->atom[i].r.x*moldyn->atom[i].f.x;
- moldyn->virial.yy+=moldyn->atom[i].r.y*moldyn->atom[i].f.y;
- moldyn->virial.zz+=moldyn->atom[i].r.z*moldyn->atom[i].f.z;
- moldyn->virial.xy+=moldyn->atom[i].r.y*moldyn->atom[i].f.x;
- moldyn->virial.xz+=moldyn->atom[i].r.z*moldyn->atom[i].f.x;
- moldyn->virial.yz+=moldyn->atom[i].r.z*moldyn->atom[i].f.y;
+ moldyn->gvir.xx+=moldyn->atom[i].r.x*moldyn->atom[i].f.x;
+ moldyn->gvir.yy+=moldyn->atom[i].r.y*moldyn->atom[i].f.y;
+ moldyn->gvir.zz+=moldyn->atom[i].r.z*moldyn->atom[i].f.z;
+ moldyn->gvir.xy+=moldyn->atom[i].r.y*moldyn->atom[i].f.x;
+ moldyn->gvir.xz+=moldyn->atom[i].r.z*moldyn->atom[i].f.x;
+ moldyn->gvir.yz+=moldyn->atom[i].r.z*moldyn->atom[i].f.y;
}
return 0;
return 0;
}
+/*
+ * restore function
+ */
+
+int moldyn_load(t_moldyn *moldyn) {
+
+ // later ...
+
+ return 0;
+}
+
/*
* post processing functions
*/
}
}
+int analyze_bonds(t_moldyn *moldyn) {
+
+
+
+
+ return 0;
+}
+