cuser_form56.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // compile: gcc -shared -Wall -fPIC `/cluster/user/armin/bin/modeller9.10/bin/mod9.10 --cflags --libs` `pkg-config --cflags glib-2.0` -I/usr/include/python2.4 cuser_form56.c cuser_form56_wrap.c -o _cuser_form56.so -lm
  2. // this is a reimplemtation of MODELLER's multiple Gaussian distance restraints
  3. // there is an additional parameter W which sets the weight for all restraints explicity (instead of the physical_schedule in python file)
  4. #include <glib.h>
  5. #include "modeller.h"
  6. #include "cuser_form56.h"
  7. #include <stdio.h>
  8. #include <math.h>
  9. #include <stdlib.h>
  10. static const float RT = 6.0;
  11. static const float W = 1;
  12. float get_p(float stdev, float x, float delta)
  13. {
  14. return exp(-0.5 * delta * delta / (stdev*stdev)) / x;
  15. }
  16. ///////////////////////
  17. /* Evaluate the form */
  18. ///////////////////////
  19. static int myform_eval(void *data, const float *feat, const int *iftyp,
  20. const int *modal, int n_feat, const float *pcsr,
  21. int n_pcsr, gboolean deriv, float *fderv, float *val)
  22. {
  23. const int numComp = *modal;
  24. double w[numComp];
  25. double mean[numComp];
  26. double sd[numComp];
  27. double p[numComp];
  28. double v[numComp];
  29. int i;
  30. // fill in values from rsr file
  31. for (i=0; i<numComp; i++) {
  32. w[i] = pcsr[i];
  33. mean[i] = pcsr[numComp+i];
  34. sd[i] = pcsr[2*numComp+i];
  35. }
  36. // trick to ensure robust calculation of derivative
  37. double rgauss1 = 10;
  38. double rgauss2 = 200;
  39. for (i=0; i<numComp; i++) {
  40. // calculate normalized deviation
  41. v[i] = (feat[0] - mean[i])/sd[i];
  42. // ignore violations that are too large
  43. if (v[i] > rgauss2) {
  44. v[i] = rgauss2 - 1E-10;
  45. }
  46. if (v[i] > rgauss1 && v[i] <= rgauss2) {
  47. double M = 37; // maximal exponent for exp
  48. double A = (rgauss2 - M) / (M * (rgauss2 - rgauss1));
  49. double B = (rgauss2 / M) * ((M - rgauss1) / (rgauss2 - rgauss1));
  50. v[i] = v[i] / (A*abs(v[i]) + B);
  51. }
  52. }
  53. double sum = 0;
  54. for (i=0; i<numComp; i++) {
  55. p[i] = 1.0 / (sd[i]*sqrt(2*M_PI)) * exp( -0.5*v[i]*v[i] );
  56. sum += w[i] * p[i];
  57. }
  58. *val = W * RT * (-1) * log( sum );
  59. if (deriv) {
  60. double dfh = 0;
  61. for (i=0; i<numComp; i++) {
  62. dfh += w[i]*p[i] * (v[i] / sd[i]);
  63. }
  64. fderv[0] = W * RT * (1.0/sum) * dfh;
  65. }
  66. return 0;
  67. }
  68. // 1 ///////////////////////////////////
  69. /* Evaluate the minimum violation*/
  70. ///////////////////////////////////
  71. static int myform_vmin(void *data, const float *feat, const int *iftyp,
  72. const int *modal, int n_feat, const float *pcsr,
  73. int n_pcsr, float *val)
  74. {
  75. *val = 100;
  76. return 0;
  77. }
  78. // 2 /////////////////////////////////
  79. /* Evaluate the heavy violation*/
  80. /////////////////////////////////
  81. static int myform_vheavy(void *data, const float *feat, const int *iftyp,
  82. const int *modal, int n_feat, const float *pcsr,
  83. int n_pcsr, float *val)
  84. {
  85. *val = 100;
  86. return 0;
  87. }
  88. // 3 ////////////////////////////////////////////
  89. /* Evaluate the relative minimum violation*/
  90. ////////////////////////////////////////////
  91. static int myform_rvmin(void *data, const float *feat, const int *iftyp,
  92. const int *modal, int n_feat, const float *pcsr,
  93. int n_pcsr, float *val)
  94. {
  95. *val = 100;
  96. return 0;
  97. }
  98. // 4 //////////////////////////////////////////
  99. /* Evaluate the relative heavy violation*/
  100. //////////////////////////////////////////
  101. static int myform_rvheavy(void *data, const float *feat, const int *iftyp,
  102. const int *modal, int n_feat, const float *pcsr,
  103. int n_pcsr, float *val)
  104. {
  105. *val = 0;
  106. return 0;
  107. }
  108. //////////////////////////////////////
  109. /* Evaluate the minimum feature mean*/
  110. //////////////////////////////////////
  111. static int myform_minmean(void *data, const float *feat, const int *iftyp,
  112. const int *modal, int n_feat, const float *pcsr,
  113. int n_pcsr, float *val)
  114. {
  115. *val = 100;
  116. return 0;
  117. }
  118. ////////////////////////////////////
  119. /* Evaluate the heavy feature mean*/
  120. ////////////////////////////////////
  121. static int myform_heavymean(void *data, const float *feat, const int *iftyp,
  122. const int *modal, int n_feat, const float *pcsr,
  123. int n_pcsr, float *val)
  124. {
  125. *val = 100;
  126. return 0;
  127. }
  128. ////////////////////////////////////////////////////
  129. /* Create the new form, and return its identifier */
  130. ////////////////////////////////////////////////////
  131. int myform_create(void)
  132. {
  133. return mod_user_form_new(myform_eval, NULL, myform_vmin, NULL, myform_vheavy,
  134. NULL, myform_rvmin, NULL, myform_rvheavy, NULL,
  135. myform_minmean, NULL, myform_heavymean, NULL);
  136. }