new src file layout (warning: doesnt compile by now!)
[physik/posic.git] / potentials / harmonic_oscillator.c
diff --git a/potentials/harmonic_oscillator.c b/potentials/harmonic_oscillator.c
new file mode 100644 (file)
index 0000000..0d933b6
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * harmonic_oscillator.c - harmonic oscillator potential
+ *
+ * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
+ *
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <math.h>
+
+#include "../moldyn.h"
+#include "../math/math.h"
+//#include "harmonic_oscillator.h"
+
+int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
+
+       t_ho_params *params;
+       t_3dvec force,distance;
+       double d,f;
+       double sc,equi_dist;
+
+       params=moldyn->pot2b_params;
+       sc=params->spring_constant;
+       equi_dist=params->equilibrium_distance;
+
+       if(ai<aj) return 0;
+
+       v3_sub(&distance,&(aj->r),&(ai->r));
+
+       if(bc) check_per_bound(moldyn,&distance);
+       d=v3_norm(&distance);
+       if(d<=moldyn->cutoff) {
+               moldyn->energy+=(0.5*sc*(d-equi_dist)*(d-equi_dist));
+               /* f = -grad E; grad r_ij = -1 1/r_ij distance */
+               f=sc*(1.0-equi_dist/d);
+               v3_scale(&force,&distance,f);
+               v3_add(&(ai->f),&(ai->f),&force);
+               virial_calc(ai,&force,&distance);
+               virial_calc(aj,&force,&distance); /* f and d signe switched */
+               v3_scale(&force,&distance,-f);
+               v3_add(&(aj->f),&(aj->f),&force);
+       }
+
+       return 0;
+}
+