Changeset a4280bd in sasmodels for sasmodels/kernel_iq.c


Ignore:
Timestamp:
Jul 25, 2016 11:54:30 PM (8 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
2f5c6d4
Parents:
2c74c11
Message:

restructure magnetic models to use less code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/kernel_iq.c

    r7b7da6b ra4280bd  
    3434 
    3535 
    36 #ifdef MAGNETIC 
     36#if defined(MAGNETIC) && NUM_MAGNETIC>0 
     37 
    3738// Return value restricted between low and high 
    3839static double clip(double value, double low, double high) 
     
    4748//     ud * (m_sigma_y + 1j*m_sigma_z); 
    4849//     du * (m_sigma_y - 1j*m_sigma_z); 
    49 static void spins(double in_spin, double out_spin, 
    50     double *uu, double *dd, double *ud, double *du) 
     50static void set_spins(double in_spin, double out_spin, double spins[4]) 
    5151{ 
    5252  in_spin = clip(in_spin, 0.0, 1.0); 
    5353  out_spin = clip(out_spin, 0.0, 1.0); 
    54   *uu = sqrt(sqrt(in_spin * out_spin)); 
    55   *dd = sqrt(sqrt((1.0-in_spin) * (1.0-out_spin))); 
    56   *ud = sqrt(sqrt(in_spin * (1.0-out_spin))); 
    57   *du = sqrt(sqrt((1.0-in_spin) * out_spin)); 
     54  spins[0] = sqrt(sqrt((1.0-in_spin) * (1.0-out_spin))); // dd 
     55  spins[1] = sqrt(sqrt((1.0-in_spin) * out_spin));       // du 
     56  spins[2] = sqrt(sqrt(in_spin * (1.0-out_spin)));       // ud 
     57  spins[3] = sqrt(sqrt(in_spin * out_spin));             // uu 
     58} 
     59 
     60static double mag_sld(double qx, double qy, double p, 
     61                       double mx, double my, double sld) 
     62{ 
     63    const double perp = qy*mx - qx*my; 
     64    return sld + perp*p; 
    5865} 
    5966 
     
    7885  double *pvec = (double *)&local_values; 
    7986 
    80 #ifdef MAGNETIC 
     87#if defined(MAGNETIC) && NUM_MAGNETIC>0 
    8188  // Location of the sld parameters in the parameter pvec. 
    8289  // These parameters are updated with the effective sld due to magnetism. 
     90  #if NUM_MAGNETIC > 3 
    8391  const int32_t slds[] = { MAGNETIC_PARS }; 
    84  
    85   const double up_frac_i = values[NPARS+2]; 
    86   const double up_frac_f = values[NPARS+3]; 
    87   const double up_angle = values[NPARS+4]; 
    88   #define MX(_k) (values[NPARS+5+3*_k]) 
    89   #define MY(_k) (values[NPARS+6+3*_k]) 
    90   #define MZ(_k) (values[NPARS+7+3*_k]) 
     92  #endif 
    9193 
    9294  // TODO: could precompute these outside of the kernel. 
    9395  // Interpret polarization cross section. 
    94   double uu, dd, ud, du; 
     96  //     up_frac_i = values[NUM_PARS+2]; 
     97  //     up_frac_f = values[NUM_PARS+3]; 
     98  //     up_angle = values[NUM_PARS+4]; 
     99  double spins[4]; 
    95100  double cos_mspin, sin_mspin; 
    96   spins(up_frac_i, up_frac_f, &uu, &dd, &ud, &du); 
    97   SINCOS(-up_angle*M_PI_180, sin_mspin, cos_mspin); 
     101  set_spins(values[NUM_PARS+2], values[NUM_PARS+3], spins); 
     102  SINCOS(-values[NUM_PARS+4]*M_PI_180, sin_mspin, cos_mspin); 
    98103#endif // MAGNETIC 
    99104 
     
    104109  #pragma omp parallel for 
    105110  #endif 
    106   for (int i=0; i < NPARS; i++) { 
     111  for (int i=0; i < NUM_PARS; i++) { 
    107112    pvec[i] = values[2+i]; 
    108113//printf("p%d = %g\n",i, pvec[i]); 
     
    228233#endif 
    229234 
    230 //printf("step:%d of %d, pars:",step,pd_stop); for (int i=0; i < NPARS; i++) printf("p%d=%g ",i, pvec[i]); printf("\n"); 
     235//printf("step:%d of %d, pars:",step,pd_stop); for (int i=0; i < NUM_PARS; i++) printf("p%d=%g ",i, pvec[i]); printf("\n"); 
    231236//printf("sphcor: %g\n", spherical_correction); 
    232237 
     
    247252        #endif 
    248253        for (int q_index=0; q_index<nq; q_index++) { 
    249 #ifdef MAGNETIC 
     254#if defined(MAGNETIC) && NUM_MAGNETIC > 0 
    250255          const double qx = q[2*q_index]; 
    251256          const double qy = q[2*q_index+1]; 
     
    253258 
    254259          // Constant across orientation, polydispersity for given qx, qy 
    255           double px, py, pz; 
     260          double scattering = 0.0; 
     261          // TODO: what is the magnetic scattering at q=0 
    256262          if (qsq > 1.e-16) { 
    257             px = (qy*cos_mspin + qx*sin_mspin)/qsq; 
    258             py = (qy*sin_mspin - qx*cos_mspin)/qsq; 
    259             pz = 1.0; 
    260           } else { 
    261             px = py = pz = 0.0; 
    262           } 
    263  
    264           double scattering = 0.0; 
    265           if (uu > 1.e-8) { 
    266             for (int sk=0; sk<NUM_MAGNETIC; sk++) { 
    267                 const double perp = (qy*MX(sk) - qx*MY(sk)); 
    268                 pvec[slds[sk]] = (values[slds[sk]+2] - perp*px)*uu; 
     263            double p[4]; 
     264            p[0] = (qy*cos_mspin + qx*sin_mspin)/qsq; 
     265            p[3] = -p[0]; 
     266            p[1] = p[2] = (qy*sin_mspin - qx*cos_mspin)/qsq; 
     267 
     268            for (int index=0; index<4; index++) { 
     269              const double xs = spins[index]; 
     270              if (xs > 1.e-8) { 
     271                const int spin_flip = (index==1) || (index==2); 
     272                const double pk = p[index]; 
     273                for (int axis=0; axis<=spin_flip; axis++) { 
     274                  #define M1 NUM_PARS+5 
     275                  #define M2 NUM_PARS+8 
     276                  #define M3 NUM_PARS+13 
     277                  #define SLD(_M_offset, _sld_offset) \ 
     278                      pvec[_sld_offset] = xs * (axis \ 
     279                      ? (index==1 ? -values[_M_offset+2] : values[_M_offset+2]) \ 
     280                      : mag_sld(qx, qy, pk, values[_M_offset], values[_M_offset+1], \ 
     281                                (spin_flip ? 0.0 : values[_sld_offset+2]))) 
     282                  #if NUM_MAGNETIC==1 
     283                      SLD(M1, MAGNETIC_PAR1); 
     284                  #elif NUM_MAGNETIC==2 
     285                      SLD(M1, MAGNETIC_PAR1); 
     286                      SLD(M2, MAGNETIC_PAR2); 
     287                  #elif NUM_MAGNETIC==3 
     288                      SLD(M1, MAGNETIC_PAR1); 
     289                      SLD(M2, MAGNETIC_PAR2); 
     290                      SLD(M3, MAGNETIC_PAR3); 
     291                  #else 
     292                  for (int sk=0; sk<NUM_MAGNETIC; sk++) { 
     293                      SLD(M1+3*sk, slds[sk]); 
     294                  } 
     295                  #endif 
     296                  scattering += CALL_IQ(q, q_index, local_values); 
     297                } 
     298              } 
    269299            } 
    270             scattering += CALL_IQ(q, q_index, local_values); 
    271           } 
    272           if (dd > 1.e-8){ 
    273             for (int sk=0; sk<NUM_MAGNETIC; sk++) { 
    274                 const double perp = (qy*MX(sk) - qx*MY(sk)); 
    275                 pvec[slds[sk]] = (values[slds[sk]+2] + perp*px)*dd; 
    276             } 
    277             scattering += CALL_IQ(q, q_index, local_values); 
    278           } 
    279           if (ud > 1.e-8){ 
    280             for (int sk=0; sk<NUM_MAGNETIC; sk++) { 
    281                 const double perp = (qy*MX(sk) - qx*MY(sk)); 
    282                 pvec[slds[sk]] = perp*py*ud; 
    283             } 
    284             scattering += CALL_IQ(q, q_index, local_values); 
    285             for (int sk=0; sk<NUM_MAGNETIC; sk++) { 
    286                 pvec[slds[sk]] = MZ(sk)*pz*ud; 
    287             } 
    288             scattering += CALL_IQ(q, q_index, local_values); 
    289           } 
    290           if (du > 1.e-8) { 
    291             for (int sk=0; sk<NUM_MAGNETIC; sk++) { 
    292                 const double perp = (qy*MX(sk) - qx*MY(sk)); 
    293                 pvec[slds[sk]] = perp*py*du; 
    294             } 
    295             scattering += CALL_IQ(q, q_index, local_values); 
    296             for (int sk=0; sk<NUM_MAGNETIC; sk++) { 
    297                 pvec[slds[sk]] = -MZ(sk)*pz*du; 
    298             } 
    299             scattering += CALL_IQ(q, q_index, local_values); 
    300300          } 
    301301#else  // !MAGNETIC 
Note: See TracChangeset for help on using the changeset viewer.