0d933b682719062a51d374f1c22ca62fdf9d1841
[physik/posic.git] / potentials / harmonic_oscillator.c
1 /*
2  * harmonic_oscillator.c - harmonic oscillator potential
3  *
4  * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
5  *
6  */
7
8 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <math.h>
17
18 #include "../moldyn.h"
19 #include "../math/math.h"
20 //#include "harmonic_oscillator.h"
21
22 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
23
24         t_ho_params *params;
25         t_3dvec force,distance;
26         double d,f;
27         double sc,equi_dist;
28
29         params=moldyn->pot2b_params;
30         sc=params->spring_constant;
31         equi_dist=params->equilibrium_distance;
32
33         if(ai<aj) return 0;
34
35         v3_sub(&distance,&(aj->r),&(ai->r));
36
37         if(bc) check_per_bound(moldyn,&distance);
38         d=v3_norm(&distance);
39         if(d<=moldyn->cutoff) {
40                 moldyn->energy+=(0.5*sc*(d-equi_dist)*(d-equi_dist));
41                 /* f = -grad E; grad r_ij = -1 1/r_ij distance */
42                 f=sc*(1.0-equi_dist/d);
43                 v3_scale(&force,&distance,f);
44                 v3_add(&(ai->f),&(ai->f),&force);
45                 virial_calc(ai,&force,&distance);
46                 virial_calc(aj,&force,&distance); /* f and d signe switched */
47                 v3_scale(&force,&distance,-f);
48                 v3_add(&(aj->f),&(aj->f),&force);
49         }
50
51         return 0;
52 }
53