Skip to content

Conversation

@xiao312
Copy link
Collaborator

@xiao312 xiao312 commented Jun 24, 2025

Summary

This pull request addresses the issue of repetitive initialization in the Cantera Reactor by calling syncState().

Performance Test

Versions Tested

  • DeepFlame Version:

    • Commit 0d4e5ee
    • Commit v1.3 94c068d
  • Test Environment:

    • Local machine
  • Example Used:

    • deepflame-dev/examples/dfLowMachFoam/notorch/twoD_HIT_flame/CH4
    • Dynamic load balancing disabled

Logs

Time = 1e-06
========Time Spent in diffenet parts========
loop Time                    = 21.7963 s
other Time                   = 1e-06 s
rho Equations                = 0.010316 s
U Equations                  = 0.194424 s
Y Equations                  = 2.65603 s
E Equations                  = 0.18264 s
p Equations                  = 0.531852 s
chemistry correctThermo      = 0.907214 s
turbulence correct           = 2.6e-05 s
combustion correct(in Y)     = 17.2706 s
percentage of chemistry      = 79.2364 %
percentage of rho/U/Y/E      = 13.963 %
Time = 1e-06
========Time Spent in diffenet parts========
loop Time                    = 22.8885 s
other Time                   = 0 s
rho Equations                = 0.010008 s
U Equations                  = 0.184964 s
Y Equations                  = 2.60104 s
E Equations                  = 0.159949 s
p Equations                  = 0.495936 s
chemistry correctThermo      = 0.802762 s
turbulence correct           = 2e-05 s
combustion correct(in Y)     = 18.6158 s
percentage of chemistry      = 81.3325 %
percentage of rho/U/Y/E      = 12.9146 %

Python

import cantera as ct
import numpy as np
import time

# Assuming mech and data are defined
gas = ct.Solution(mech)
# Create Reactor and ReactorNet
r = ct.Reactor(gas, name='R1', energy='off')
sim = ct.ReactorNet([r])
sim.rtol, sim.atol = 1e-6, 1e-10

time_steps = []  # List to store time for each iteration

start_time = time.time()  # Start timer
# Repeat the entire process 100 times
for _ in range(10000):
    # Initialize the state
    npstate = data[0, :11]  # Assuming data is defined and has sufficient rows
    T_old, P_old, Y_old = npstate[0], npstate[1], npstate[2:]
    
    gas.TPY = T_old, P_old, Y_old
    res_1st = [T_old, P_old] + list(gas.Y) + list(gas.partial_molar_enthalpies / gas.molecular_weights)

    # Measure time for advancing the simulation
    r.syncState()
    sim.reinitialize()    
    sim.advance(time_step)
    sim.initial_time = 0.0
    

    # Get new state
    new_TPY = [gas.T, gas.P] + list(gas.Y) + list(gas.partial_molar_enthalpies / gas.molecular_weights)
    res_1st += new_TPY
    
elapsed_time = time.time() - start_time  # Calculate elapsed time
# Print average time after all iterations
print("Elapsed times for each iteration:", elapsed_time)
# print("Average time per iteration:", np.mean(time_steps))



# Assuming mech and data are defined
gas = ct.Solution(mech)

time_steps = []  # List to store time for each iteration

start_time = time.time()  # Start timer
# Repeat the entire process 100 times
for _ in range(10000):
    # Initialize the state
    npstate = data[0, :11]  # Assuming data is defined and has sufficient rows
    T_old, P_old, Y_old = npstate[0], npstate[1], npstate[2:]
    
    gas.TPY = T_old, P_old, Y_old
    res_1st = [T_old, P_old] + list(gas.Y) + list(gas.partial_molar_enthalpies / gas.molecular_weights)

    # Create Reactor and ReactorNet
    r = ct.Reactor(gas, name='R1', energy='off')
    sim = ct.ReactorNet([r])
    sim.rtol, sim.atol = 1e-6, 1e-10

    # Measure time for advancing the simulation
    
    sim.advance(time_step)
    

    # Get new state
    new_TPY = [gas.T, gas.P] + list(gas.Y) + list(gas.partial_molar_enthalpies / gas.molecular_weights)
    res_1st += new_TPY
    
elapsed_time = time.time() - start_time  # Calculate elapsed time
# Print average time after all iterations
print("Elapsed times for each iteration:", elapsed_time)
# print("Average time per iteration:", np.mean(time_steps))

Results:

Elapsed times for each iteration: 1.2987749576568604
Elapsed times for each iteration: 1.4612696170806885

Conclusions

An approximate 8% to 10% speedup could be expected, although this improvement may diminish with larger mesh sizes.

@xiao312 xiao312 requested a review from pkuLmq June 24, 2025 03:05
@pkuLmq pkuLmq merged commit 52b8a91 into deepmodeling:master Jun 26, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants