May-03-2019, 03:50 PM
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]