Python Forum
Convert C code to python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert C code to python
#1
The following C code is used to stabilize temperature profiles in lakes, and I´m trying to convert it to python, but there is something wrong with my code (below), can somebody take a look at this problem, thank you:

void fix_ profile (
                   int n, //number of points in the profile( starting at 0)
                   double T[], // temperature profile
                   double area [ ] // ’ profile ’ with lake area at each level
                   )
{
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// variables declaration
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
int i,j;
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// checking profile from bottom to surface
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
for (i = n ; i > 0 ; i−−)
  {
    f o r (j = 0 ; j < i ; j ++)
      {
        if (T[ i ] > T[ j ])
          {
             change_profile(T,i,j,area); // stabilization function
             break ;
          }
       }
   }
   return;
}


void change_profile(
                    double T[ ] , // temperature profile
                    int i, // instability point
                    int inst, //instability point found
                    double area [ ] //lake area at level i
                    )
{
double A, // área*temp that must be preserved
   An, // área*temp after stabilization −−> to check 
   B, // sum of areas
   Tconst; // average temperature of stabilized points
int
   j,
   jmin, // point previous to the region of constant temperature 
   jmin0, // first point that will be changed
   jmax, // point after the region of constant temperature 
   jmax0, // last point that will be changed
   min, // jmin = inst [i] = 0
   max; // jmax = i = N
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// defining jmin0
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
   jmin = inst−1;
   if ( jmin < 0 )
    {
       jmin = 0 ;
       min = 1 ;
       jmin0 = jmin ;
     }
    else
     {
        min = 0 ;
        jmin0 = i n s t ;
      }
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// defining jmax0
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
    jmax = i + 1 ;
    if ( jmax > N )
     {
        jmax = N ;
        max = 1 ;
        jmax0 = jmax ;
     }
    else
     {
       max = 0 ;
       jmax0 = i ;
     }
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// calculating the weighted average between temperature and area
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
    A = 0.0;
    B = 0.0;
    for (j = jmin ; j <= jmax0 ; j ++)
     {
       A += T[j]*area[j];
       B += area[j];
     }

     Tconst = (A)/B;
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// replacing temperature in the stabilized area
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
      for (j = jmin ; j <= jmax0 ; j ++ ) {
          T[ j ] = Tconst ;
      }
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// recalculating the value of T[j]* area[j] for cheking
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
      An = 0.0;
      for ( j = jmin ; j <= jmax0 ; j ++ ) {
          An += T[j]*a r e a [j];
      }
} 
My code

def fix_profile(N,T,area):
    
    for i in reversed(range(N)):
        for j in range(N-1):
            if T[i]>T[j]:
                change_profile(T,i,j,area,N)
                break
    return T        
 
def change_profile(T,i,inst,area,N):
        
    jmin=inst-1
    if jmin < 0:
        jmin = 0
        min = 1
        jmin0 = jmin 
    else:
        min = 0
        jmin0 = inst
    
    jmax = i+1
    if jmax > N:
        jmax = N
        max = 1
        jmax0 = jmax
    else:
        max = 0
        jmax0 = i

    A = 0.0
    B = 0.0
    for j in range(jmin,jmax0):
        A += T[j]*area[j]
        B += area[j]
        Tconst = A/B
    
    for j in range(jmin,jmax0): 
        T[j] = Tconst
    
    An = 0.0
    for j in range(jmin,jmax0):
        An +=  T[j] * area[j]
Reply
#2
Please let us know what is wrong, show how the function fix_profile is called and what arguments are used to call it.
what you expect the results to be compared to what you are actually getting, and any errors received in error tags

Note: min and max are reserved builtin words

Edit: At the moment i can see that change_profile does not return anything and in fix_profile when change_profile is called, if there was a return value it would not be stored to a variable.
Reply
#3
I don´t know how to edit my question,

Nlayers = 10
TempLake = [10,10,100,10,10,10,10,10,10,10]  this list gets changed in the output, the values must decrease in the list, the value 100 must be changed to the output bellow ( weighted average of TempLake values with area)
area = [100,100,100,100,100,100,100,100,100,100]               
out = fix_profile(Nlayers,TempLake, area)
print out
I should get:
TempLake = [40,40,40,10,10,10,10,10,10,10] the profile is stable...

instead of the result i´m getting
Output:
TempLake = [10,10,100,10,10,10,10,10,10,10]
the value 100 was removed and the result is a weighted average: instead of [10, 10, 100 ...I need to have [40, 40 ,40
Reply
#4
Look carefully at those last 3 loops in your C program. They go from jmin to jmax0 inclusive, by the conditional j <= jmax0. This means that if jmin is 5, and jmax0 is 10, it will iterate 6 times, with the last iteration being 10. Now look at your Python code, you are using range(jmin, jmax0). In the case that jmin is 5 and jmax0 is 10, this will make the loop iterate 5 times only. You could fix it up by changing your range(jmin,jmax0) to range(jmin,jmax0+1) to make it behave like the C code, in all 3 of those loops.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code to convert executable file to doc or pdf Micmilli2 3 2,965 Oct-11-2018, 10:15 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020