<commit_before><commit_msg>Script to find left state that produces a (shock) wave of given Mach number given the initial right state<commit_after>"""
Given a (relativistic) Mach number and the initial data for the unshocked fluid on the right, this script will find the left state.
"""

import eos_defns
import SR1d
import numpy as np
from scipy.optimize import brentq

def find_left(q_r, M=1., gamma=5./3., K=1.):
    """
    Finds the left wave for a given Mach number and right state.

    Returns the left hand state.
    """

    # calculate sound speed
    # for gamma law, h * c_s**2 = 2 gamma * p / rho
    rho_r = q_r.rho
    p_r = K * rho_r**gamma
    h = 1. + p_r * gamma / (rho_r * (gamma - 1.))
    c_s = np.sqrt(2. * gamma * p_r / (rho_r * h))

    print('c_s = {}'.format(c_s))

    # Limits for p_star guess. If M < 1, this should be less than the rhs pressure;
    # if M > 1, should be higher
    if M < 1.:
        p_star_lims = [1.e-5 * p_r, p_r]
    else:
        p_star_lims = [p_r, 1.e4 * p_r]

    def find_p_star(p_star_guess, q_r, c_s):
        # wavenumber is 2 as nonlinear rhs wave
        wave_r = SR1d.Wave(q_r, p_star_guess, 2)

        # get wave speed
        v_s = wave_r.wave_speed[0]

        # calculate Mach number
        M_star = (v_s / np.sqrt(1. - v_s**2)) / (c_s / np.sqrt(1. - c_s**2))

        return M_star - M

    p_star = brentq(find_p_star, p_star_lims[0], p_star_lims[1], args=(q_r, c_s))

    wave_r = SR1d.Wave(q_r, p_star, 2)

    return wave_r.q_l

if __name__ == "__main__":

    # standard 5/3 gamma law EoS
    gamma = 5./3.
    K = 1.
    eos = eos_defns.eos_gamma_law(gamma)

    # right state
    rho_r, v_r, vt_r = (0.1, 0.0, 0.0)
    eps_r = K * rho_r**(gamma - 1.) / (gamma - 1.)

    # initialise right state
    q_r = SR1d.State(rho_r, v_r, vt_r, eps_r, eos, label="R")

    q_l = find_left(q_r, M=4., gamma=gamma, K=K)
    print('left state [rho,  v,  vt,  eps] =  {}'.format(q_l.prim()))
{'fraction_non_alphanumeric': 0.13190184049079753, 'fraction_numerical': 0.022494887525562373, 'mean_word_length': 2.4393673110720564, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 4, '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': '2712164', 'n_tokens_mistral': 850, 'n_tokens_neox': 797, 'n_words': 236}