Skip to content
Advertisement

How to use while loop to check difference between calculation output , convergence calculation

I am writing a code that carry outs the calculation:

def elastic_buckling_pressure(corroded_wall_thickness,
                              external_radius_mm,
                              lambda_value,
                              n_value,
                              youngs_modulus_mpa,
                              poissons_ratio):
    """
    """
    section_1 = (1 / ((n_value ** 2 + 0.5 * (lambda_value ** 2)) - 1)
                 ) * ((youngs_modulus_mpa * corroded_wall_thickness) / external_radius_mm)
    section_2_part_1 = ((n_value**2 + lambda_value**2 - 1)**2 / (12 *(1 - poissons_ratio**2))) * (
            corroded_wall_thickness / external_radius_mm)
    section_2_part_2 = lambda_value**4 / (n_value**2 + lambda_value**2)**2
    section_2 = section_2_part_1 + section_2_part_2
    calculated_pel_value = section_1 * section_2
    return calculated_pel_value

I want to pass incremental n_value value starting from 2 then +0.10 each time it iterates.

For the iteration I am writing while loop counter however, codes does not work.

def iteration_calculation(corroded_wall_thickness,
                          external_radius_mm,
                          lambda_value,
                          n_value,
                          youngs_modulus_mpa,
                          poissons_ratio):
    epsilon = 0.00001
    count = 0
    max_iteration = 1000
    beta = 0.001
    while count < max_iteration:
        calculated_pel = elastic_buckling_pressure(corroded_wall_thickness=corroded_wall_thickness,
                                                   external_radius_mm=external_radius_mm,
                                                   lambda_value=lambda_value,
                                                   n_value=n_value,
                                                   youngs_modulus_mpa=youngs_modulus_mpa,
                                                   poissons_ratio=poissons_ratio)
        changed_n_value = n_value + 0.10
        changed_calculated_pel = elastic_buckling_pressure(corroded_wall_thickness=corroded_wall_thickness,
                                                           external_radius_mm=external_radius_mm,
                                                           lambda_value=lambda_value,
                                                           n_value=changed_n_value,
                                                           youngs_modulus_mpa=youngs_modulus_mpa,
                                                           poissons_ratio=poissons_ratio)
        change_value = changed_calculated_pel - calculated_pel
        if abs(change_value) < epsilon:
            beta = calculated_pel
            break
        else:
            beta = changed_calculated_pel
        count += 1
    value_calculated = beta * 1
    return value_calculated
training dataset
'corroded_wall_thickness': np.array([10]),
'external_radius_mm': np.array([250]),
'lambda_value': np.array([0.5]),
'n_value': np.array([2]),
'youngs_modulus_mpa': np.array([350000]),
'poissons_ratio': np.array([0.33])

However the code does not iterate and I am kind of struggling how to create the iterative loop. I want to achieve difference between calculated_pel and changed_calculated_pel at minimum as stated by epsilon value 0.00001. once that is achieved, code should break and give the converged value. However, lack of my coding ability has created further issues. So, today I have come to you for help. Any suggestion would be appreciated.

Thank You !

Advertisement

Answer

For anyone who might stumble in similar situation: I have managed to solve the issue.

                                                                                                                         
def iteration_calculation(corroded_wall_thickness,                                                                        
                          external_radius_mm,                                                                             
                          lambda_value,                                                                                   
                          youngs_modulus_mpa,                                                                             
                          poissons_ratio):                                                                                
    list_value = {}                                                                                                       
    epsilon = 25                                                                                                          
    count = 0                                                                                                             
    max_iteration = 100000                                                                                                                                                                                                          
    min_value = float("inf")                                                                                              
    last_value = float("inf")                                                                                             
    changed_n_value = 2                                                                                                   
    while count < max_iteration:                                                                                         
                                                                                                                         
        new_value = elastic_buckling_pressure(corroded_wall_thickness=corroded_wall_thickness,                            
                                               external_radius_mm=external_radius_mm,                                    
                                               lambda_value=lambda_value,                                                
                                               n_value=changed_n_value,                                                  
                                               youngs_modulus_mpa=youngs_modulus_mpa,                                    
                                              poissons_ratio=poissons_ratio)                                             
        list_value[changed_n_value] = new_value                                                                          
        if abs(last_value - new_value) < epsilon:                                                                        
            break                                                                                                        
        last_value = new_value                                                                                           
                                                                                                                         
                                                                                                                         
        changed_n_value +=0.10                                                                                           
                                                                                                                         
        if new_value < min_value:                                                                                        
            min_value = new_value                                                                                        
        count+=1                                                                                                         
    return min_value, list_value                                                                                         
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement