<commit_before>#Name: solvers0D.py
#Author: <NAME>
#Purpose: Implement several basic ODE methods 

#Import needed modules
import numpy as np

class Solvers0D(object):
    
    def __init__(self,func,func_params,rka_err=1e-6,safety_1=0.9,safety_2=1.1,safety_3=4.0):
        """Initialize 0D Solvers class."""
        
        if type(func).__name__ != 'function':
            raise TypeError("Input function is not of type function.") 
        else:
            self.func = func
        self.func_params = func_params
        #save adaptive stepper parameters
        self.rka_err = rka_err
        self.safety_1 = safety_1
        self.safety_2 = safety_2
        self.safety_3 = safety_3
        
        
    def euler_solve(self,state,time,tau):
        """Use first order Euler method to step equation forward in time."""
        
        #Step forward in time
        derivs = self.func(state,time,tau,**self.func_params)
        
        #Update parameters
        new_state = np.zeros(len(state))
        if len(derivs) != len(state):
            raise ValueError("Dimension mismatch between state and derivs vectors.")
        for i in range(len(derivs)):
            new_state[i] = state[i] + derivs[i]*tau
            
        return new_state
        
        
    def rk4_solve(self,state,time,tau):
        """Use fourth-order Runge-Kutta solver to step equation forward in time."""
        
        #Initialize different time and timestep lists
        tau_temp = [.5*tau]*3 + [tau]
        time_temp = [time] + [time+.5*tau]*2 + [time+tau]
        
        initial_state = state
        f_rk = []
        
        #Calculate RK f_1--f_4 functions
        for i in range(len(tau_temp)):
            f_rk.append(self.func(state,time_temp[i],tau_temp[i],**self.func_params))
            if len(f_rk[-1]) != len(state):
                raise ValueError("Dimension mismatch between state and derivs vectors.")
            for j in range(len(f_rk[-1])):
                state[j] += f_rk[-1]*tau_temp[i]
                
        #Update state vector
        for i in range(len(initial_state)):
            state[i] = initial_state[i] + 1.0/6.0*tau*(f_rk[0][i] + f_rk[3][i] + 2.0*(f_rk[1][i] + f_rk[2][i]))
            
        return state
        
        
    #def adaptive_timestep(self,)<commit_msg>bug fixes in RK4 solver<commit_after>#Name: solvers0D.py
#Author: <NAME>
#Purpose: Implement several basic ODE methods 

#Import needed modules
import numpy as np

class Solvers0D(object):
    
    def __init__(self,func,func_params,rka_err=1e-6,safety_1=0.9,safety_2=1.1,safety_3=4.0):
        """Initialize 0D Solvers class."""
        
        if type(func).__name__ != 'function':
            raise TypeError("Input function is not of type function.") 
        else:
            self.func = func
        self.func_params = func_params
        #save adaptive stepper parameters
        self.rka_err = rka_err
        self.safety_1 = safety_1
        self.safety_2 = safety_2
        self.safety_3 = safety_3
        
        
    def euler_solve(self,state,time,tau):
        """Use first order Euler method to step equation forward in time."""
        
        #Step forward in time
        derivs = self.func(state,time,tau,**self.func_params)
        
        #Update parameters
        new_state = np.zeros(len(state))
        if len(derivs) != len(state):
            raise ValueError("Dimension mismatch between state and derivs vectors.")
        for i in range(len(derivs)):
            new_state[i] = state[i] + derivs[i]*tau
            
        return new_state
        
        
    def rk4_solve(self,state,time,tau):
        """Use fourth-order Runge-Kutta solver to step equation forward in time."""
        
        #Initialize different time and timestep lists
        tau_temp = [.5*tau]*3 + [tau]
        time_temp = [time] + [time+.5*tau]*2 + [time+tau]
        
        initial_state = state
        f_rk = []
        
        #Calculate RK f_1--f_4 functions
        for i in range(len(tau_temp)):
            f_rk.append(self.func(state,time_temp[i],tau_temp[i],**self.func_params))
            
            if len(f_rk[-1]) != len(state):
                raise ValueError("Dimension mismatch between state and derivs vectors.")
                
            for j in range(len(f_rk[-1])):
                new_state[j] = state[j] + f_rk[-1][j]*tau_temp[i]
                
            state = new_state
                
        #Update state vector
        for i in range(len(initial_state)):
            new_state[i] = initial_state[i] + 1.0/6.0*tau*(f_rk[0][i] + f_rk[3][i] + 2.0*(f_rk[1][i] + f_rk[2][i]))
            
        return new_state
        
        
    #def adaptive_timestep(self,)
{'fraction_non_alphanumeric': 0.1151246303337558, 'fraction_numerical': 0.017110266159695818, 'mean_word_length': 1.9873817034700316, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7267133', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1502, 'n_words': 340}