<commit_before>#!/usr/bin/python3

# With Cython
import pyximport; pyximport.install()
from tdma import TDMA_solve

# Without Cython
#from tdma_simple import TDMA_solve

from numpy import empty, linspace, ones, zeros
import numpy as np
import matplotlib.pyplot as plt

# Size of main computation area
L_X = 1
L_Y = 1
X_DENSITY = 1000
Y_DENSITY = 1000
# Boundary conditions
T_NORTH = 2.0
T_SOUTH = 2.0
T_EAST = 2.0
T_WEST = 2.0
# Additional boundary conditions
BOX_SIZE_1 = [0.2, 0.2]     # box size for 1st type boundary
BOX_COORDS_2 = [[0.3, 0.3], [0.7, 0.7]]
T_2 = 3.5     # used in 2nd and 4th method (temp along y axis)
T_1 = 2.5     # used in 1st and 4th method (temp along x axis)
# Iterative parameters
EPS = 1e-4
TAU = 0.001
ITER_NUM = 800
# Very small number for lambdas
L_EPS = 1e-20

# Precalculate useful constants
BOX_1 = [BOX_SIZE_1[0]/L_X * X_DENSITY, BOX_SIZE_1[1]/L_Y * Y_DENSITY]
BOX_2 = [[BOX_COORDS_2[0][0]/L_X*X_DENSITY, BOX_COORDS_2[0][1]/L_Y*Y_DENSITY],
         [BOX_COORDS_2[1][0]/L_X*X_DENSITY, BOX_COORDS_2[1][1]/L_Y*Y_DENSITY]]

def set_initial_conditions(boundary_type):
    """Set initial conditions for solver."""
    x_grid = linspace(0, L_X, X_DENSITY)
    y_grid = linspace(0, L_Y, Y_DENSITY)
    T_field = ones([X_DENSITY, Y_DENSITY])
    # set boundary conditions
    T_field[:, -1] = T_NORTH
    T_field[:, 0] = T_SOUTH
    T_field[0, :] = T_WEST
    T_field[-1, :] = T_EAST
    if boundary_type == 1:
        T_field[:BOX_1[0], :BOX_1[1]] = T_1

    # set raw lambdas
    lambdas_raw = ones([X_DENSITY, Y_DENSITY])
    if boundary_type == 1:
        lambdas_raw[:BOX_1[0], :BOX_1[1]] = 1e20
    elif boundary_type == 2:
        lambdas_raw[BOX_2[0][0]:BOX_2[1][0], BOX_2[0][1]:BOX_2[1][1]] = 1e15
    elif boundary_type in [3, 4]:
        lambdas_raw[:BOX_1[0], :BOX_1[1]] = 0

    # heat sources
    s_c = zeros([X_DENSITY, Y_DENSITY])
    s_p = zeros([X_DENSITY, Y_DENSITY])
    if boundary_type == 2:
        s_c[BOX_2[0][0]:BOX_2[1][0], BOX_2[0][1]:BOX_2[1][1]] = 1e20 * T_2
        s_p[BOX_2[0][0]:BOX_2[1][0], BOX_2[0][1]:BOX_2[1][1]] = -1e20
    elif boundary_type == 4:
        s_p[BOX_1[0], :BOX_1[1]] = -1e20
        s_p[:BOX_1[0], BOX_1[1]] = -1e20
        s_c[BOX_1[0], :BOX_1[1]] = 1e20 * T_1
        s_c[:BOX_1[0], BOX_1[1]] = 1e20 * T_2

    # calculate lambdas +/-
    lambdas_x = empty([X_DENSITY - 1, Y_DENSITY - 2])
    lambdas_y = empty([X_DENSITY - 2, Y_DENSITY - 1])
    lambdas_x = ((2 * lambdas_raw[1:, 1:-1] * lambdas_raw[:-1, 1:-1] + L_EPS) /
                 (lambdas_raw[1:, 1:-1] + lambdas_raw[:-1, 1:-1] + L_EPS))
    lambdas_y = ((2 * lambdas_raw[1:-1, 1:] * lambdas_raw[1:-1, :-1] + L_EPS) /
                 (lambdas_raw[1:-1, 1:] + lambdas_raw[1:-1, :-1] + L_EPS))

    return x_grid, y_grid, lambdas_x, lambdas_y, T_field, s_c, s_p

def accuracy_acquired(T_next, T_prev):
    """Estimate difference between last 2 iterations."""
    return ((T_next.ravel().min() - T_prev.ravel().min()) / T_next.ravel().min(),
            (T_next.ravel().max() - T_prev.ravel().max()) / T_next.ravel().max())

def pretty_plot(T_field):
    """Make a pretty heatmap plot."""
    plt.imshow(T_field.T, origin='lower', interpolation='none')
    plt.show()

def solver(boundary_type):
    """Entry point for solving."""
    x_grid, y_grid, lambdas_x, lambdas_y, T_field_prev, s_c, s_p = set_initial_conditions(boundary_type)
    T_field_next = T_field_prev.copy()          # for n+1 step
    T_field_halfnext = T_field_prev.copy()      # for n+1/2 step<commit_msg>Added nonzero q_w for 3rd boundary type<commit_after>#!/usr/bin/python3

# With Cython
import pyximport; pyximport.install()
from tdma import TDMA_solve

# Without Cython
#from tdma_simple import TDMA_solve

from numpy import empty, linspace, ones, zeros
import numpy as np
import matplotlib.pyplot as plt

# Size of main computation area
L_X = 1
L_Y = 1
X_DENSITY = 100
Y_DENSITY = 100
# Boundary conditions
T_NORTH = 2.0
T_SOUTH = 2.0
T_EAST = 2.0
T_WEST = 2.0
# Additional boundary conditions
BOX_SIZE_1 = [0.2, 0.2]     # box size for 1st type boundary
BOX_COORDS_2 = [[0.3, 0.3], [0.7, 0.7]]
T_2 = 3.5     # used in 2nd and 4th method (temp along y axis)
T_1 = 2.5     # used in 1st and 4th method (temp along x axis)
Q_W = 10     # sort of thermal flux (actually, \frac{A}{\Delta v} coefficient required)
# Iterative parameters
EPS = 1e-4
TAU = 0.001
ITER_NUM = 200
# Very small number for lambdas
L_EPS = 1e-20

# Precalculate useful constants
BOX_1 = [BOX_SIZE_1[0]/L_X * X_DENSITY, BOX_SIZE_1[1]/L_Y * Y_DENSITY]
BOX_2 = [[BOX_COORDS_2[0][0]/L_X*X_DENSITY, BOX_COORDS_2[0][1]/L_Y*Y_DENSITY],
         [BOX_COORDS_2[1][0]/L_X*X_DENSITY, BOX_COORDS_2[1][1]/L_Y*Y_DENSITY]]

def set_initial_conditions(boundary_type):
    """Set initial conditions for solver."""
    x_grid = linspace(0, L_X, X_DENSITY)
    y_grid = linspace(0, L_Y, Y_DENSITY)
    T_field = ones([X_DENSITY, Y_DENSITY])
    # set boundary conditions
    T_field[:, -1] = T_NORTH
    T_field[:, 0] = T_SOUTH
    T_field[0, :] = T_WEST
    T_field[-1, :] = T_EAST
    if boundary_type == 1:
        T_field[:BOX_1[0], :BOX_1[1]] = T_1

    # set raw lambdas
    lambdas_raw = ones([X_DENSITY, Y_DENSITY])
    if boundary_type == 1:
        lambdas_raw[:BOX_1[0], :BOX_1[1]] = 1e20
    elif boundary_type == 2:
        lambdas_raw[BOX_2[0][0]:BOX_2[1][0], BOX_2[0][1]:BOX_2[1][1]] = 1e15
    elif boundary_type in [3, 4]:
        lambdas_raw[:BOX_1[0], :BOX_1[1]] = 0

    # heat sources
    s_c = zeros([X_DENSITY, Y_DENSITY])
    s_p = zeros([X_DENSITY, Y_DENSITY])
    if boundary_type == 2:
        s_c[BOX_2[0][0]:BOX_2[1][0], BOX_2[0][1]:BOX_2[1][1]] = 1e20 * T_2
        s_p[BOX_2[0][0]:BOX_2[1][0], BOX_2[0][1]:BOX_2[1][1]] = -1e20
    elif boundary_type == 3:
        s_c[BOX_1[0], :BOX_1[1]] = -Q_W
        s_c[:BOX_1[0], BOX_1[1]] = -Q_W
    elif boundary_type == 4:
        s_p[BOX_1[0], :BOX_1[1]] = -1e20
        s_p[:BOX_1[0], BOX_1[1]] = -1e20
        s_c[BOX_1[0], :BOX_1[1]] = 1e20 * T_1
        s_c[:BOX_1[0], BOX_1[1]] = 1e20 * T_2

    # calculate lambdas +/-
    lambdas_x = empty([X_DENSITY - 1, Y_DENSITY - 2])
    lambdas_y = empty([X_DENSITY - 2, Y_DENSITY - 1])
    lambdas_x = ((2 * lambdas_raw[1:, 1:-1] * lambdas_raw[:-1, 1:-1] + L_EPS) /
                 (lambdas_raw[1:, 1:-1] + lambdas_raw[:-1, 1:-1] + L_EPS))
    lambdas_y = ((2 * lambdas_raw[1:-1, 1:] * lambdas_raw[1:-1, :-1] + L_EPS) /
                 (lambdas_raw[1:-1, 1:] + lambdas_raw[1:-1, :-1] + L_EPS))

    return x_grid, y_grid, lambdas_x, lambdas_y, T_field, s_c, s_p

def accuracy_acquired(T_next, T_prev):
    """Estimate difference between last 2 iterations."""
    return ((T_next.ravel().min() - T_prev.ravel().min()) / T_next.ravel().min(),
            (T_next.ravel().max() - T_prev.ravel().max()) / T_next.ravel().max())

def pretty_plot(T_field):
    """Make a pretty heatmap plot."""
    plt.imshow(T_field.T, origin='lower', interpolation='none')
    plt.colorbar()
    plt.show()

def solver(boundary_type):
    """Entry point for solving."""
    x_grid, y_grid, lambdas_x, lambdas_y, T_field_prev, s_c, s_p = set_initial_conditions(boundary_type)
    T_field_next = T_field_prev.copy()          # for n+1 step
    T_field_halfnext = T_field_prev.copy()      # for n+1/2 step
{'fraction_non_alphanumeric': 0.16070937585922462, 'fraction_numerical': 0.06021446246906791, 'mean_word_length': 3.3615107913669067, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '430616', 'n_tokens_mistral': 3412, 'n_tokens_neox': 3201, 'n_words': 658}