Merging two reactors

66 views
Skip to first unread message

Mahir Husain Salmani

unread,
Mar 15, 2025, 1:24:42 PM3/15/25
to Cantera Users' Group
Dear Cantera Community
I am trying to merge two reactors, and than the resultant reactor's volume is to be varied according to some function. se the following part of code;
if reactor_lean is not None and reactor_rich is not None and abs(angle - merge_angle) < 0.5:
        # Temperature before merging
        print(f"Before merging: T_lean = {reactor_lean.T}, T_rich = {reactor_rich.T}")
        # Perform merging logic here
        # Compute total mass
        mass_lean = reactor_lean.mass
        mass_rich = reactor_rich.mass
        total_mass = mass_lean + mass_rich
   
        # Compute internal energy for both reactors
        U_lean = mass_lean * reactor_lean.thermo.u
        U_rich = mass_rich * reactor_rich.thermo.u
        U_combined = U_lean + U_rich  # Total energy after merging

        # Compute specific heat at constant volume (cv) for both gases
        cv_lean = reactor_lean.thermo.cv_mass
        cv_rich = reactor_rich.thermo.cv_mass
        cv_combined = (cv_lean * reactor_lean.mass + cv_rich * reactor_rich.mass) / total_mass

        # Compute final temperature using energy conservation
       
        T_combined = -U_combined / (total_mass * cv_combined )

        MW_lean = reactor_lean.thermo.mean_molecular_weight  # Mean molecular weight
        MW_rich = reactor_rich.thermo.mean_molecular_weight
        n_lean = mass_lean / MW_lean  # Total moles in lean reactor
        n_rich = mass_rich / MW_rich  # Total moles in rich reactor
        n_total = n_lean + n_rich  # Total moles after merging

        # Compute weighted species mass fractions
        X_lean = reactor_lean.thermo.X
        X_rich = reactor_rich.thermo.X
        X_combined = (n_lean * X_lean + n_rich * X_rich) / n_total
        X_combined /= np.sum(X_combined)  # Normalize to ensure sum(X) = 1
       
        # Compute total reactor volume
        V_combined = reactor_lean.volume + reactor_rich.volume

        print(f"Volume at merge angle: {V_combined:.7f}")
        print(f"composition of rich at merge angle: {reactor_rich.thermo.Y}")
        print(f"composition of lean at merge angle: {reactor_lean.thermo.Y}")
        print(f"[MERGING] Merging reactors at {angle:.3f}° CAD | Merged Volume: {V_combined:.6f} m³| Merged_mass: {total_mass:.6f} kg")

        # Calculate density of combined reactor
        density_combined = (total_mass / V_combined)
   
        # Create a new gas state with merged properties
        gas_combined = ct.Solution("skeletal.yaml")
        gas_combined.UVX = U_combined, V_combined, X_combined

        # Initialize new combined reactor
        reactor_combined = ct.IdealGasReactor(gas_combined, name="Combined Reactor")
        reactor_combined.volume = V_combined
       
        # Reservoirs for lean & rich reactors
        reservoir_lean = ct.Reservoir(ct.Solution('skeletal.yaml'))
        reservoir_lean.thermo.TPX = reactor_lean.T, reactor_lean.thermo.P, reactor_lean.thermo.X
        reservoir_rich = ct.Reservoir(ct.Solution('skeletal.yaml'))
        reservoir_rich.thermo.TPX = reactor_rich.T, reactor_rich.thermo.P, reactor_rich.thermo.X

        # Define controlled mass flow rates
        mdot_lean_2 = max(reactor_lean.mass / dt, 1e-6) # kg/s
        mdot_rich_2 = max(reactor_rich.mass / dt, 1e-6) # kg/s

        # Create mass flow controllers from combined reservoir to lean and rich reactors
        mfc_lean_2 = ct.MassFlowController(reservoir_lean, reactor_combined)
        mfc_rich_2 = ct.MassFlowController(reservoir_rich, reactor_combined)

        # Mass flow through MFC
        mfc_lean_2.mass_flow_rate = mdot_lean_2
        mfc_rich_2.mass_flow_rate = mdot_rich_2
        sim.advance(sim.time + 1e-8)

        wall_combined = ct.Wall(left=reactor_combined, right=air_env, A=1.0)
        new_volume_combined = volume_function_combined(angle)  

        wall_combined.velocity = (new_volume_combined - reactor_combined.volume) / dt
        reactor_combined.volume = new_volume_combined

        reactor_lean = None
        reactor_rich = None
       
        print(f"[AFTER MERGE] Merged: T={reactor.T}, P={reactor.thermo.P}, V={reactor.volume}")
        # Update simulation with new reactor
        sim = ct.ReactorNet([reactor_combined])

The resultant reactor's is not working, i mean i am getting error.
i tried several vays to define gas available in cantera, like gas_combined.TPX, gas_combined.TDY, but always give error.

error is as following;
[MERGING] Merging reactors at 90.752° CAD | Merged Volume: 0.000229 m³| Merged_mass: 0.002071 kg
[AFTER MERGE] Merged: T=350.8460162287109, P=162484.97609090764, V=0.00022559879003928844
Step 270: Time = 0.030111111111110988
[WARNING][rank 0][D:\a\_temp\sundials-7.1.1\src\cvodes\cvodes.c:3482][CVode] Internal t = 0.0184816 and h = 1.48992e-18 are such that t + h = t on the next step. The solver will continue anyway.
[WARNING][rank 0][D:\a\_temp\sundials-7.1.1\src\cvodes\cvodes.c:3482][CVode] Internal t = 0.0184816 and h = 1.48992e-18 are such that t + h = t on the next step. The solver will continue anyway.
[WARNING][rank 0][D:\a\_temp\sundials-7.1.1\src\cvodes\cvodes.c:3482][CVode] Internal t = 0.0184816 and h = 1.48992e-18 are such that t + h = t on the next step. The solver will continue anyway.
[WARNING][rank 0][D:\a\_temp\sundials-7.1.1\src\cvodes\cvodes.c:3482][CVode] Internal t = 0.0184816 and h = 1.48992e-18 are such that t + h = t on the next step. The solver will continue anyway.
[WARNING][rank 0][D:\a\_temp\sundials-7.1.1\src\cvodes\cvodes.c:3482][CVode] Internal t = 0.0184816 and h = 1.48992e-18 are such that t + h = t on the next step. The solver will continue anyway.
[WARNING][rank 0][D:\a\_temp\sundials-7.1.1\src\cvodes\cvodes.c:3482][CVode] Internal t = 0.0184816 and h = 1.48992e-18 are such that t + h = t on the next step. The solver will continue anyway.
[WARNING][rank 0][D:\a\_temp\sundials-7.1.1\src\cvodes\cvodes.c:3482][CVode] Internal t = 0.0184816 and h = 1.02822e-18 are such that t + h = t on the next step. The solver will continue anyway.
[WARNING][rank 0][D:\a\_temp\sundials-7.1.1\src\cvodes\cvodes.c:3482][CVode] Internal t = 0.0184816 and h = 1.02822e-18 are such that t + h = t on the next step. The solver will continue anyway.
[WARNING][rank 0][D:\a\_temp\sundials-7.1.1\src\cvodes\cvodes.c:3482][CVode] Internal t = 0.0184816 and h = 1.02822e-18 are such that t + h = t on the next step. The solver will continue anyway.
[WARNING][rank 0][D:\a\_temp\sundials-7.1.1\src\cvodes\cvodes.c:3482][CVode] Internal t = 0.0184816 and h = 1.02822e-18 are such that t + h = t on the next step. The solver will continue anyway.
[WARNING][rank 0][D:\a\_temp\sundials-7.1.1\src\cvodes\cvodes.c:3487][CVode] The above warning has been issued mxhnil times and will not be issued again for this problem.
[ERROR][rank 0][D:\a\_temp\sundials-7.1.1\src\cvodes\cvodes.c:8032][cvHandleFailure] At t = 0.0184816 and h = 7.64897e-27, the corrector convergence test failed repeatedly or with |h| = hmin.
Traceback (most recent call last):
  File "d:\current_affairs\Gate\simulation_files\trying.py", line 336, in <module>
    sim.advance(time[i])
  File "reactor.pyx", line 1587, in cantera.reactor.ReactorNet.advance
cantera._utils.CanteraError:
*******************************************************************************
CanteraError thrown by CVodesIntegrator::integrate:
CVodes error encountered. Error code: -4
cvHandleFailure: At t = 0.0184816 and h = 7.64897e-27, the corrector convergence test failed repeatedly or with |h| = hmin.

Components with largest weighted error estimates:
71: 1389900509668364.2
5: 46386111756217.53
52: 16899217687495.691
63: -3372372136581.9126
59: 2716682749272.4604
67: 855164423795.3235
65: 71652313798.7172
17: 36286642677.84419
53: -20930244299.61406
37: 16750972456.600834
*******************************************************************************
how can i get out of it?
Thanks 
Reply all
Reply to author
Forward
0 new messages