+int crt(t_moldyn *moldyn,t_mdrun *mdrun) {
+
+ t_stage *stage;
+ t_crt_params *crtp;
+
+ int fd;
+ char line[128];
+ char *wptr;
+ int acount;
+ int ret;
+ void *ptr;
+
+ t_atom *atom;
+ t_3dvec disp;
+ double frac;
+ int i;
+
+ stage=mdrun->stage.current->data;
+ crtp=stage->params;
+
+ acount=0;
+
+ /* initial stuff */
+
+ if(crtp->count==0) {
+ printf(" crt init\n");
+ // read final positions, constraints and do the alloc
+ fd=open(crtp->file,O_RDONLY);
+ if(fd<0) {
+ perror("[mdrun] FATAL reading constraints file");
+ return fd;
+ }
+ while(1) {
+ ret=get_line(fd,line,128);
+ // check for end of file
+ if(ret<=0) {
+ printf(" read %d atom positions\n",acount);
+ if(acount!=moldyn->count)
+ printf(" atom count mismatch!!!\n");
+ printf("\n");
+ break;
+ }
+ // ignore # lines and \n
+ if((line[0]=='#')|(ret==1))
+ continue;
+ // allocate new memory
+ ptr=realloc(crtp->r_fin,(acount+1)*sizeof(t_3dvec));
+ if(ptr==NULL) {
+ perror("[mdrun] FATAL realloc crt positions");
+ return -1;
+ }
+ crtp->r_fin=ptr;
+ ptr=realloc(constraints,(acount+1)*3*sizeof(u8));
+ if(ptr==NULL) {
+ perror("[mdrun] FATAL realloc crt constraints");
+ return -1;
+ }
+ constraints=ptr;
+ // ignore type
+ wptr=strtok(line," \t");
+ // read x y z
+ wptr=strtok(NULL," \t");
+ crtp->r_fin[acount].x=atof(wptr);
+ wptr=strtok(NULL," \t");
+ crtp->r_fin[acount].y=atof(wptr);
+ wptr=strtok(NULL," \t");
+ crtp->r_fin[acount].z=atof(wptr);
+ // read constraints
+ wptr=strtok(NULL," \t");
+ constraints[3*acount]=atoi(wptr);
+ wptr=strtok(NULL," \t");
+ constraints[3*acount+1]=atoi(wptr);
+ wptr=strtok(NULL," \t");
+ constraints[3*acount+2]=atoi(wptr);
+ // done reading
+ acount+=1;
+ }
+ close(fd);
+ // allocate trafo angles
+ trafo_angle=malloc(acount*2*sizeof(double));
+ if(trafo_angle==NULL) {
+ perror("[mdrun] FATAL alloc trafo angles");
+ return -1;
+ }
+ // set crt mode
+ crtt=crtp->type;
+ }
+
+ /* write a save file s-crt_xofy.save */
+ snprintf(line,128,"%s/s-crt_%03dof%03d.save",
+ moldyn->vlsdir,crtp->count,crtp->steps);
+ fd=open(line,O_WRONLY|O_TRUNC|O_CREAT,S_IRUSR|S_IWUSR);
+ if(fd<0) perror("[mdrun] crt save fd open");
+ else {
+ write(fd,moldyn,sizeof(t_moldyn));
+ write(fd,moldyn->atom,
+ moldyn->count*sizeof(t_atom));
+ }
+ close(fd);
+ /* visualize atoms */
+ visual_atoms(moldyn);
+
+ /* output energy */
+ printf(" crt energy: %d - %f\n\n",
+ crtp->count,(moldyn->ekin+moldyn->energy)/EV);
+
+ /* crt routines: calculate displacement + set individual constraints */
+
+ printf(" crt step %d of %d in total\n\n",crtp->count+1,crtp->steps);
+
+ if((crtp->type==1)|(crtp->count==0))
+ printf(" crt angle update\n\n");
+
+ for(i=0;i<moldyn->count;i++) {
+ // calc displacements
+ atom=moldyn->atom;
+ v3_sub(&disp,&(crtp->r_fin[i]),&(atom[i].r));
+ // angles
+ if((crtp->type==1)|(crtp->count==0)) {
+ trafo_angle[2*i]=atan2(disp.x,disp.y);
+ trafo_angle[2*i+1]=-atan2(disp.z,
+ sqrt(disp.x*disp.x+disp.y*disp.y));
+ }
+ // move atoms
+ frac=1.0/(crtp->steps-crtp->count);
+ v3_scale(&disp,&disp,frac);
+ v3_add(&(atom[i].r),&(atom[i].r),&disp);
+ }
+
+ return 0;
+}
+