Skip to content

Commit fda27a8

Browse files
authored
Made pi_p optional in RocketSolver_solve (#36)
* made pi_p optional in RocketSolver_solve
1 parent a39a26a commit fda27a8

File tree

9 files changed

+721
-221
lines changed

9 files changed

+721
-221
lines changed

.gitignore

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ docs/doctrees/
55
docs/doxygen/
66
docs/html/
77
docs/source/_build/
8-
docs/*.aux
9-
docs/*.log
8+
*.aux
9+
*.log
10+
*.toc
11+
*.out
1012
extern/gfe
1113
extern/install
1214
*.swp
@@ -19,15 +21,20 @@ extern/install
1921
*.pdf
2022
*.bat
2123
*.lib
24+
*.dylib
25+
*.so
26+
*.dll
27+
*.pyc
28+
*.pyo
29+
*.pyd
30+
*.egg-info/
2231
!sampleThe/*.inp
2332
!test/main_interface/*.inp
2433
!test/main_interface/reference_output/*.out
2534
__pycache__/
2635
*.py[cod]
2736
*$py.class
28-
source/bind/excel/*.dylib
29-
source/bind/excel/*.so
30-
source/bind/excel/*.dll
3137
source/bind/excel/~$*.xlsm
3238
source/bind/excel/*.lib
3339
source/bind/python/samples
40+
source/bind/python/cea/samples/*.tex

source/bind/c/bindc.F90

Lines changed: 491 additions & 183 deletions
Large diffs are not rendered by default.

source/bind/c/cea.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ extern "C"
432432
cea_rocket_solution soln,
433433
const cea_array weights,
434434
const cea_real pc,
435+
// Optional: ignored when n_pi_p == 0 (pi_p may be NULL)
435436
const cea_array pi_p,
436437
const cea_int n_pi_p,
437438
const cea_array subar,
@@ -449,6 +450,7 @@ extern "C"
449450
cea_rocket_solution soln,
450451
const cea_array weights,
451452
const cea_real pc,
453+
// Optional: ignored when n_pi_p == 0 (pi_p may be NULL)
452454
const cea_array pi_p,
453455
const cea_int n_pi_p,
454456
const cea_array subar,

source/bind/python/CEA.pyx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,7 +1830,7 @@ cdef class RocketSolver:
18301830
Raises
18311831
------
18321832
ValueError
1833-
If exit conditions not specified, or conflicting parameters provided
1833+
If conflicting parameters are provided
18341834
"""
18351835
cdef cea_err ierr
18361836
cdef int nweights = <int>len(weights)
@@ -1880,12 +1880,15 @@ cdef class RocketSolver:
18801880
else:
18811881
raise ValueError("RocketSolver.solve: supar must be a list, np.ndarray, or float")
18821882

1883-
if ((npi_p + nsubar + nsupar) == 0):
1884-
raise ValueError("Exit condition not specified.")
1885-
1886-
cdef cea_array pi_p_c = <cea_array>malloc(npi_p * sizeof(double))
1887-
cdef cea_array subar_c = <cea_array>malloc(nsubar * sizeof(double))
1888-
cdef cea_array supar_c = <cea_array>malloc(nsupar * sizeof(double))
1883+
cdef cea_array pi_p_c = NULL
1884+
cdef cea_array subar_c = NULL
1885+
cdef cea_array supar_c = NULL
1886+
if npi_p > 0:
1887+
pi_p_c = <cea_array>malloc(npi_p * sizeof(double))
1888+
if nsubar > 0:
1889+
subar_c = <cea_array>malloc(nsubar * sizeof(double))
1890+
if nsupar > 0:
1891+
supar_c = <cea_array>malloc(nsupar * sizeof(double))
18891892
if (npi_p > 0 and pi_p_c == NULL) or (nsubar > 0 and subar_c == NULL) or (nsupar > 0 and supar_c == NULL):
18901893
free(wts)
18911894
if pi_p_c != NULL:

source/bind/python/cea_def.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,14 @@ cdef extern from "cea.h":
298298
cpdef cea_err cea_rocket_solver_create_with_options(cea_rocket_solver *solver, const cea_mixture products,
299299
const cea_solver_opts opts)
300300
cpdef cea_err cea_rocket_solver_destroy(cea_rocket_solver *solver)
301+
# pi_p is optional when n_pi_p == 0.
301302
cpdef cea_err cea_rocket_solver_solve_iac(const cea_rocket_solver solver, cea_rocket_solution soln,
302303
const cea_array weights, const cea_real pc, const cea_array pi_p,
303304
const cea_int n_pi_p, const cea_array subar, const cea_int nsubar,
304305
const cea_array supar, const cea_int nsupar, const cea_int n_frz,
305306
const cea_real hc_or_tc, const cea_bool use_hc,
306307
const cea_real tc_est, const cea_bool use_tc_est)
308+
# pi_p is optional when n_pi_p == 0.
307309
cpdef cea_err cea_rocket_solver_solve_fac(const cea_rocket_solver solver, cea_rocket_solution soln,
308310
const cea_array weights, const cea_real pc, const cea_array pi_p,
309311
const cea_int n_pi_p, const cea_array subar, const cea_int nsubar,

source/bind/python/tests/test_rocket_smoke.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,29 @@ def test_rocket_solver_smoke():
3131
check=False,
3232
)
3333
assert result.returncode == 0, result.stderr + result.stdout
34+
35+
36+
@pytest.mark.smoke
37+
def test_rocket_solver_smoke_without_exit_conditions():
38+
code = textwrap.dedent(
39+
"""
40+
import numpy as np
41+
import cea
42+
43+
reactants = ["H2", "O2"]
44+
reactants_mix = cea.Mixture(reactants)
45+
products_mix = cea.Mixture(reactants, products_from_reactants=True)
46+
weights = reactants_mix.moles_to_weights(np.array([2.0, 1.0], dtype=np.float64))
47+
solver = cea.RocketSolver(products_mix, reactants=reactants_mix)
48+
soln = cea.RocketSolution(solver)
49+
50+
solver.solve(soln, weights, pc=10.0, tc=3000.0)
51+
"""
52+
)
53+
result = subprocess.run(
54+
[sys.executable, "-c", code],
55+
capture_output=True,
56+
text=True,
57+
check=False,
58+
)
59+
assert result.returncode == 0, result.stderr + result.stdout

source/bind/python/tests/test_validation.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ def test_eqsolver_rejects_non_mixture_reactants(cea_module):
1616
cea_module.EqSolver(products, reactants=["H2", "O2"])
1717

1818

19-
def test_rocket_solver_requires_exit_condition(cea_module):
20-
reactants_mix, products_mix, weights = _basic_reactants(cea_module)
21-
solver = cea_module.RocketSolver(products_mix, reactants=reactants_mix)
22-
soln = cea_module.RocketSolution(solver)
23-
with pytest.raises(ValueError):
24-
solver.solve(soln, weights, pc=10.0, tc=3000.0)
25-
26-
2719
def test_rocket_solver_rejects_conflicting_hc_tc(cea_module):
2820
reactants_mix, products_mix, weights = _basic_reactants(cea_module)
2921
solver = cea_module.RocketSolver(products_mix, reactants=reactants_mix)

source/rocket.f90

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ subroutine RocketSolver_solve_iac(self, soln, reactant_weights, pc, pi_p, subar,
783783
type(RocketSolution), intent(inout) :: soln
784784
real(dp), intent(in) :: reactant_weights(:)
785785
real(dp), intent(in) :: pc ! Chamber pressure [Pa]
786-
real(dp), intent(in) :: pi_p(:) ! Ratio of chamber pressure to exit pressure [unitless]
786+
real(dp), intent(in), optional :: pi_p(:) ! Ratio of chamber pressure to exit pressure [unitless]
787787
real(dp), intent(in), optional :: subar(:) ! Subsonic area ratio [unitless]
788788
real(dp), intent(in), optional :: supar(:) ! Supersonic area ratio [unitless]
789789
integer, intent(in), optional :: n_frz ! Station where the composition should be frozen
@@ -806,7 +806,8 @@ subroutine RocketSolver_solve_iac(self, soln, reactant_weights, pc, pi_p, subar,
806806
call log_debug("Starting rocket IAC solve")
807807

808808
! Set the total number of evaluation points
809-
num_pts = 2 + size(pi_p)
809+
num_pts = 2 ! infinity + throat
810+
if (present(pi_p)) num_pts = num_pts + size(pi_p)
810811
if (present(subar)) num_pts = num_pts + size(subar)
811812
if (present(supar)) num_pts = num_pts + size(supar)
812813

@@ -893,14 +894,19 @@ subroutine RocketSolver_solve_iac(self, soln, reactant_weights, pc, pi_p, subar,
893894
! -----------------------------------------------
894895
! Exit conditions: pressure ratio
895896
! -----------------------------------------------
896-
idx = 3
897+
if (present(pi_p)) then
898+
idx = 3
897899

898-
if (frozen .and. idx > n_frz_) then
899-
call self%solve_pi_p_frozen(soln, idx, n_frz, pc, pi_p, h_inf, 1)
900+
if (frozen .and. idx > n_frz_) then
901+
call self%solve_pi_p_frozen(soln, idx, n_frz, pc, pi_p, h_inf, 1)
902+
else
903+
call self%solve_pi_p(soln, idx, pc, pi_p, h_inf, state1, reactant_weights)
904+
end if
905+
if (.not. soln%converged) return
900906
else
901-
call self%solve_pi_p(soln, idx, pc, pi_p, h_inf, state1, reactant_weights)
907+
! If pi_p not present, idx stays at 3 for subsequent sections
908+
idx = 3
902909
end if
903-
if (.not. soln%converged) return
904910

905911
! -----------------------------------------------
906912
! Exit conditions: subsonic area ratio
@@ -947,7 +953,7 @@ subroutine RocketSolver_solve_fac(self, soln, reactant_weights, pc, pi_p, subar,
947953
type(RocketSolution), intent(inout) :: soln
948954
real(dp), intent(in) :: reactant_weights(:)
949955
real(dp), intent(in) :: pc ! Injector pressure [Pa]
950-
real(dp), intent(in) :: pi_p(:) ! Ratio of chamber pressure to exit pressure [unitless]
956+
real(dp), intent(in), optional :: pi_p(:) ! Ratio of chamber pressure to exit pressure [unitless]
951957
real(dp), intent(in), optional :: subar(:) ! Subsonic area ratio [unitless]
952958
real(dp), intent(in), optional :: supar(:) ! Supersonic area ratio [unitless]
953959
real(dp), intent(in), optional :: ac_at ! Contraction ratio: ratio of finite chamber area to throat area, Ac/At (FAC only) [unitless]
@@ -994,7 +1000,8 @@ subroutine RocketSolver_solve_fac(self, soln, reactant_weights, pc, pi_p, subar,
9941000
call log_debug("Starting rocket FAC solve")
9951001

9961002
! Set the total number of evaluation points
997-
num_pts = 4 + size(pi_p)
1003+
num_pts = 4 ! injector + infinity + combustor + throat
1004+
if (present(pi_p)) num_pts = num_pts + size(pi_p)
9981005
if (present(subar)) num_pts = num_pts + size(subar)
9991006
if (present(supar)) num_pts = num_pts + size(supar)
10001007

@@ -1206,14 +1213,19 @@ subroutine RocketSolver_solve_fac(self, soln, reactant_weights, pc, pi_p, subar,
12061213
! -----------------------------------------------
12071214
! Exit conditions: pressure ratio
12081215
! -----------------------------------------------
1209-
idx = 5
1216+
if (present(pi_p)) then
1217+
idx = 5
12101218

1211-
if (frozen .and. idx > n_frz_) then
1212-
call self%solve_pi_p_frozen(soln, idx, n_frz_, pc, pi_p, h_inj, 2)
1219+
if (frozen .and. idx > n_frz_) then
1220+
call self%solve_pi_p_frozen(soln, idx, n_frz_, pc, pi_p, h_inj, 2)
1221+
else
1222+
call self%solve_pi_p(soln, idx, pc, pi_p, h_inj, S_ref, reactant_weights)
1223+
end if
1224+
if (.not. soln%converged) return
12131225
else
1214-
call self%solve_pi_p(soln, idx, pc, pi_p, h_inj, S_ref, reactant_weights)
1226+
! If pi_p not present, idx stays at 5 for subsequent sections
1227+
idx = 5
12151228
end if
1216-
if (.not. soln%converged) return
12171229

12181230
! -----------------------------------------------
12191231
! Exit conditions: subsonic area ratio
@@ -1264,7 +1276,7 @@ function RocketSolver_solve(self, reactant_weights, pc, pi_p, fac, subar, supar,
12641276
class(RocketSolver), intent(in) :: self
12651277
real(dp), intent(in) :: reactant_weights(:) !
12661278
real(dp), intent(in) :: pc ! Chamber pressure [bar]
1267-
real(dp), intent(in) :: pi_p(:) ! Ratio of chamber pressure to exit pressure [unitless]
1279+
real(dp), intent(in), optional :: pi_p(:) ! Ratio of chamber pressure to exit pressure [unitless]
12681280
logical, intent(in), optional :: fac ! Finite-area combustor flag
12691281
real(dp), intent(in), optional :: subar(:) ! Subsonic area ratio [unitless]
12701282
real(dp), intent(in), optional :: supar(:) ! Supersonic area ratio [unitless]
@@ -1289,10 +1301,11 @@ function RocketSolver_solve(self, reactant_weights, pc, pi_p, fac, subar, supar,
12891301

12901302
! Call either the FAC or IAC solver
12911303
if (fac_) then
1292-
call self%solve_fac(soln, reactant_weights, pc, pi_p, subar, supar, ac_at, mdot, n_frz, tc_est, hc, tc)
1304+
call self%solve_fac(soln, reactant_weights, pc, pi_p=pi_p, subar=subar, supar=supar, &
1305+
ac_at=ac_at, mdot=mdot, n_frz=n_frz, tc_est=tc_est, hc=hc, tc=tc)
12931306
else ! IAC
1294-
call self%solve_iac(soln, reactant_weights, pc, pi_p, subar=subar, supar=supar, n_frz=n_frz, &
1295-
tc_est=tc_est, hc=hc, tc=tc)
1307+
call self%solve_iac(soln, reactant_weights, pc, pi_p=pi_p, subar=subar, supar=supar, &
1308+
n_frz=n_frz, tc_est=tc_est, hc=hc, tc=tc)
12961309
end if
12971310

12981311
end function

0 commit comments

Comments
 (0)