0

I have the following C function that is a part of a code written in C.

void pusher(int part, int ntime,long double x[3], long double v[3], long double E[3], long double B[3], long double Vth, long double c2, long double x_new[3], long double v_new[3]){

   long double x_mid[3];
   long double upart_new[3];

    // half time-step for Euler step
     long double dth = 0.5*dt;
    
    // position x_{i+1/2} at time t_{i+1/2} + dt/2 (1st-order Euler scheme)
    for(int i=0;i<3;i++){
        x_mid[i] = x[i] + v[i]*dth;
    }
 
   // TO DO: Get E, B at new position 

   /* I want to add the Cpp function at this point in the code. */
   
  // interpolate_fields();
    

   // 'Kick' particle (update velocity) based on the chosen integrator
    switch(pusher_name) {
    case 1 :
        Vay(v, E, B, c2, upart_new);
        break;
        
    case 0  :
        Boris_relativ(v, E, B, c2, upart_new);
        break; 
             
    }

    long double gL_new = gamma_u(upart_new, c2);
    // full position update at t_{i+1} = t_{i+1/2} + dt/2 = t_i + dt
   for(int i=0;i<3;i++){
        v_new[i]  = upart_new[i]/gL_new; 
        x_new[i]  = x_mid[i] + v_new[i]*dth;
        
   }

}

I also have the following Cpp function that I would like to include at the indicated point in the code. To avoid including the Cpp code in the question, I have uploaded the file here:

https://www.dropbox.com/s/vbsxme9vozci4tz/ba_interp3.cpp?dl=0

Is this possible?

Any suggestions for how I could do this?

jokerp
  • 157
  • 1
  • 8
  • Are you compiling in C or C++? – RoQuOTriX Sep 24 '21 at 08:45
  • I am using my terminal to compile in C. – jokerp Sep 24 '21 at 08:46
  • 4
    Since placing source on external sites is frowned upon, please show the relevant parts of that C++ function directly in your question. Then we can look whether it is a static function, or a class member method, and suggest solutions. – the busybee Sep 24 '21 at 08:46
  • @jokerp The key points to lookup for that scenario are _name mangling_ and linker directives. You can control the name mangling used by a c++ compiler using the `extern "C" { ... }` blocks. – πάντα ῥεῖ Sep 24 '21 at 08:56
  • The referenced code seems to be a MEX function for Matlab which calls one of a set of C or C++ functions. I don't see a function `interpolate_fields()`. Most functions look like valid C functions, but with Matlab specific data types. Some functions implement Matlab specific interface code or string argument parsing. Maybe you can simply extract the function you need and compile it as a C function. Please [edit] your question to add clarification. Please show all relevant code in your question. – Bodo Sep 24 '21 at 08:59
  • I would advise you to compile your C++ function seperatly using a C++ compiler to a statically linkable library and then use that from C++. More info here : https://stackoverflow.com/questions/199418/using-c-library-in-c-code – Pepijn Kramer Sep 24 '21 at 09:40
  • I have the impression that in your case you don't need to call a C++ function from C code because the code looks like valid C. If the supposedly duplicate question does not help to solve the problem, you can improve your question by adding all relevant code, some explanation what exactly you want to achieve, how your code is built and why it's different from the linked question. Then your question might get reopened. – Bodo Sep 24 '21 at 11:04

0 Answers0