Skip to content

520 feature request set n decimals as abstract method to latex formula #522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/prompts/casus_formula.prompt.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ class Form6Dot10abNStrengthReductionFactor(Formula):

return output

def latex(self) -> LatexFormula:
def latex(self, n: int = 3) -> LatexFormula:
"""Returns LatexFormula object for formula 6.10a/bN."""
_equation: str = r"\begin{cases} 0.600 & \text{if } f_{ck} \leq 60 MPa \\ \max\left(0.9 - \frac{f_{ck}}{200}, 0.5\right) & \text{if } f_{ck} > 60 MPa \end{cases}"
_numeric_equation: str = latex_replace_symbols(
_equation,
{
"f_{ck}": f"{self.f_ck:.3f}",
"f_{ck}": f"{self.f_ck:.{n}f}",
},
False,
)
return LatexFormula(
return_symbol=r"\nu_{1}",
result=f"{self:.3f}",
result=f"{self:.{n}f}",
equation=_equation,
numeric_equation=_numeric_equation,
comparison_operator_label="=",
Expand Down
6 changes: 3 additions & 3 deletions .github/prompts/comparison_formula.prompt.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ class Form5Dot38aCheckRelativeSlendernessRatio(Formula):

return (lambda_y / lambda_z <= 2) and (lambda_z / lambda_y <= 2)

def latex(self) -> LatexFormula:
def latex(self, n: int = 3) -> LatexFormula:
"""Returns LatexFormula object for formula 5.38a."""
_equation: str = r"\left( \frac{\lambda_{y}}{\lambda_{z}} \leq 2 \text{ and } \frac{\lambda_{z}}{\lambda_{y}} \leq 2 \right)"
_numeric_equation: str = latex_replace_symbols(
_equation,
{
"lambda_y": f"{self.lambda_y:.3f}",
"lambda_z": f"{self.lambda_z:.3f}",
"lambda_y": f"{self.lambda_y:.{n}f}",
"lambda_z": f"{self.lambda_z:.{n}f}",
},
False,
)
Expand Down
10 changes: 5 additions & 5 deletions .github/prompts/equation_formula.prompt.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,21 @@ class Form6Dot41W1Rectangular(Formula):

return (c_1**2) / 2 + c_1 * c_2 + 4 * c_2 * d + 16 * d**2 + 2 * np.pi * d * c_1

def latex(self) -> LatexFormula:
def latex(self, n: int = 3) -> LatexFormula:
"""Returns LatexFormula object for formula 5.41."""
_equation: str = r"\frac{c_1^2}{2} + c_1 \cdot c_2 + 4 \cdot c_2 \cdot d + 16 \cdot d^2 + 2 \cdot \pi \cdot d \cdot c_1"
_numeric_equation: str = latex_replace_symbols(
_equation,
{
r"c_1": f"{self.c_1:.3f}",
r"c_2": f"{self.c_2:.3f}",
r"d": f"{self.d:.3f}",
r"c_1": f"{self.c_1:.{n}f}",
r"c_2": f"{self.c_2:.{n}f}",
r"d": f"{self.d:.{n}f}",
},
False,
)
return LatexFormula(
return_symbol=r"W_1",
result=f"{self:.3f}",
result=f"{self:.{n}f}",
equation=_equation,
numeric_equation=_numeric_equation,
comparison_operator_label="=",
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,5 @@ cython_debug/
.vscode/

# ENV file
.ENV
.ENV
/local/
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,17 @@ def value(self) -> MM:
self.minimum_cover_with_regard_to_casting_surface(),
)

def latex(self) -> str:
def latex(self, n: int = 1) -> str:
"""Returns the lateX string representation for Nominal concrete cover check."""
min_surface = self.minimum_cover_with_regard_to_casting_surface()
return r"\newline~".join(
[
f"Nominal concrete cover according to art. 4.4.1 from NEN-EN 1992-1-1{self.constants.CODE_SUFFIX}:",
latex_max_curly_brackets(
r"Nominal concrete cover according to art. 4.4.1 (c_{nom})",
"Minimum cover with regard to casting surface according to art. 4.4.1.3 (4)",
),
f"= {latex_max_curly_brackets(self.c_nom().latex().result, self.minimum_cover_with_regard_to_casting_surface())} = {self.value()} mm",
f"= {latex_max_curly_brackets(self.c_nom().latex().result, min_surface)} = {self.value():.{n}f} mm",
"",
"Where:",
f"\\hspace{{4ex}}{self.c_nom().latex().return_symbol} = {self.c_nom().latex().equation.replace('min', 'min,total')}"
Expand Down
1 change: 1 addition & 0 deletions blueprints/codes/cur/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""CUR guidelines."""
7 changes: 7 additions & 0 deletions blueprints/codes/cur/cur_228/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""CUR-228 package."""

from blueprints.type_alias import M

CUR_228 = "CUR 228"

R_0: M = 0.3
80 changes: 80 additions & 0 deletions blueprints/codes/cur/cur_228/formula_2_21.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""Contains formula 2.21 from CUR 228."""

from blueprints.codes.cur.cur_228 import CUR_228, R_0
from blueprints.codes.formula import Formula
from blueprints.codes.latex_formula import LatexFormula
from blueprints.type_alias import DIMENSIONLESS, KN_M3, KPA, M
from blueprints.validations import raise_if_less_or_equal_to_zero, raise_if_negative


class Form2Dot21ModulusHorizontalSubgrade(Formula):
"""Representation of equation 2.21 CUR 228."""

source_document = CUR_228
label = "2.21"
n_decimals: int = 2

def __init__(self, r: M, e_p: KPA, alpha: DIMENSIONLESS) -> None:
"""Calculates the modulus of horizontal subgrade reaction (k_h) using Menard stiffness for r >= 0.3 m.

Parameters
----------
r: M
The radius of a foundation pile [m]:
r >= 0.3 m
e_p: KPA
Elastic modulus of Ménard [kPa]:
e_p ≈ beta * q_c
beta: DIMENSIONLESS
Dependent on soil type [-]: (table 2.1)
q_c: KPA
Cone resistance [kPa]
alpha: DIMENSIONLESS
Factor dependent on soil type [-]: (table 2.1)
"""
super().__init__()
self.r = float(r)
self.e_p = float(e_p)
self.alpha = float(alpha)

@staticmethod
def _evaluate(r: M, e_p: KPA, alpha: DIMENSIONLESS) -> KN_M3:
"""Evaluates the formula, for more information see the __init__ method."""
r_0 = R_0
raise_if_negative(e_p=e_p)
raise_if_less_or_equal_to_zero(r=r, alpha=alpha)

if r >= r_0:
return 3 * e_p / (1.3 * r_0 * (2.65 * r / r_0) ** alpha + alpha * r)
msg = "Radius is smaller than 0.3m, use: Eq2Dot21ModulusHorizontalSubgrade"
raise ValueError(msg)

def latex(self, n: int = 3) -> LatexFormula:
"""Latex representation of the full equation including result.

Parameters
----------
n: int
Number of decimals to round the result to

Returns
-------
LatexFormula
Latex representation of the equation

"""
n = self.n_decimals

return LatexFormula(
return_symbol="k_{h}",
equation=r"\frac{1}{3 \cdot E_{p}} \cdot "
r"\left[1.3 \cdot R_{0} "
r"\left( 2.65 \frac{R}{R_0}\right)^\alpha"
r" + \alpha \cdot R \right]",
numeric_equation=rf"\frac{{1}}{{3 \cdot {self.e_p:.{n}}}} \cdot"
rf"\left[1.3 \cdot {R_0:.{n}} "
rf"\left( 2.65 \cdot \frac{{{self.r:.{n}}}}{{{R_0:.{n}}}}\right)^{{{self.alpha:.{n}f}}}"
rf"+ {self.alpha:.{n}} \cdot {self.r:.{n}}\right]",
result=f"{self:.{n}f}",
unit="kN/m^3",
)
67 changes: 67 additions & 0 deletions blueprints/codes/cur/cur_228/formula_2_22.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""Contains formula 2.22 from CUR 228."""

from blueprints.codes.cur.cur_228 import CUR_228, R_0
from blueprints.codes.formula import Formula
from blueprints.codes.latex_formula import LatexFormula
from blueprints.type_alias import DIMENSIONLESS, KN_M3, KPA, M
from blueprints.validations import raise_if_less_or_equal_to_zero, raise_if_negative


class Form2Dot22ModulusHorizontalSubgrade(Formula):
"""Representation of equation 2.22 CUR 228."""

source_document = CUR_228
label = "2.22"
n_decimals: int = 2

def __init__(self, r: M, e_p: KPA, alpha: DIMENSIONLESS) -> None:
"""Calculates the modulus of horizontal subgrade reaction (k_h) using Menard stiffness for r < 0.3 m.

Parameters
----------
r: M
The radius of a foundation pile [m]:
r < 0.3 m
e_p: KPA
Elastic modulus of Ménard [kPa]:
e_p ≈ beta * q_c
beta: DIMENSIONLESS
Dependent on soil type [-]: (table 2.1)
q_c: KPA
Cone resistance [kPa]
alpha: DIMENSIONLESS
Factor dependent on soil type [-]: (table 2.1)
"""
super().__init__()
self.r = r
self.e_p = e_p
self.alpha = alpha

@staticmethod
def _evaluate(r: M, e_p: KPA, alpha: DIMENSIONLESS) -> KN_M3:
"""Return the Menard stiffness k_h when r < 0.3 m [kN/m3]."""
raise_if_negative(e_p=e_p)
raise_if_less_or_equal_to_zero(r=r, alpha=alpha)
if r < R_0:
return e_p / 2 / r / ((4 * 2.65**alpha + 3 * alpha) / 18)
msg = "Radius is equal to- or larger than 0.3m, use: Eq2Dot21ModulusHorizontalSubgrade"
raise ValueError(msg)

def latex(self, n: int = 3) -> LatexFormula:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing is preventing a user from submitting a negative value. maybe its wice to always pick the larger value of 0 and n?

"""Latex representation of the full equation including result.

Returns
-------
LatexFormula
Latex representation of the equation

"""
n = self.n_decimals
return LatexFormula(
return_symbol=r"k_{h}",
result=f"{self:.{n}f}",
equation=r"\frac{2 \cdot R}{E_{p}} \cdot \frac{4 \cdot 2.65^{\alpha} + 3 \alpha}{18}",
numeric_equation=rf"\frac{{2 \cdot {self.r:.{n}f}}}{{{self.e_p:.{n}f}}} \cdot \frac{{4 \cdot 2.65^{{{self.alpha:.{n}f}}} + 3 \cdot "
rf"{self.alpha:.{n}f}}}{{18}}",
unit="kN/m^3",
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ def _evaluate(
raise ValueError("b must be less than or equal to 1.5 * a")
return 1.13 * a * np.sqrt(b / a)

def latex(self) -> LatexFormula:
def latex(self, n: int = 3) -> LatexFormula:
"""Returns LatexFormula object for formula 1.0.1."""
return LatexFormula(
return_symbol=r"D_{eq}",
result=f"{self:.3f}",
result=f"{self:.{n}f}",
equation=r"1.13 \cdot a \cdot \sqrt{\frac{min(b, 1.5 \cdot a)}{a}}",
numeric_equation=rf"1.13 \cdot {self.a} \cdot \sqrt{latex_fraction(numerator=self.b, denominator=self.a)}",
comparison_operator_label="=",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ def _evaluate(
raise_if_negative(gamma_f=gamma_f)
return gamma_f * f_rep

def latex(self) -> LatexFormula:
def latex(self, n: int = 2) -> LatexFormula:
"""Returns LatexFormula object for formula 2.1a."""
return LatexFormula(
return_symbol=r"F_d",
result=f"{self:.2f}",
result=f"{self:.{n}f}",
equation=r"\gamma_F \cdot F_{rep}",
numeric_equation=rf"{self.gamma_f:.2f} \cdot {self.f_rep:.2f}",
numeric_equation=rf"{self.gamma_f:.{n}f} \cdot {self.f_rep:.{n}f}",
comparison_operator_label="=",
)
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ def _evaluate(
raise_if_negative(psi=psi)
return psi * f_k

def latex(self) -> LatexFormula:
def latex(self, n: int = 2) -> LatexFormula:
"""Returns LatexFormula object for formula 2.1b."""
return LatexFormula(
return_symbol=r"F_{rep}",
result=f"{self:.2f}",
result=f"{self:.{n}f}",
equation=r"\psi \cdot F_k",
numeric_equation=rf"{self.psi:.2f} \cdot {self.f_k:.2f}",
numeric_equation=rf"{self.psi:.{n}f} \cdot {self.f_k:.{n}f}",
comparison_operator_label="=",
)
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ def _evaluate(
raise_if_less_or_equal_to_zero(gamma_m=gamma_m)
return x_k / gamma_m

def latex(self) -> LatexFormula:
def latex(self, n: int = 3) -> LatexFormula:
"""Returns LatexFormula object for formula 2.2."""
numerator = f"{self.x_k:.3f}"
denominator = f"{self.gamma_m:.3f}"
numerator = f"{self.x_k:.{n}f}"
denominator = f"{self.gamma_m:.{n}f}"
return LatexFormula(
return_symbol="X_d",
result=f"{self:.3f}",
result=f"{self:.{n}f}",
equation=r"\frac{X_{k}}{\gamma_M}",
numeric_equation=f"{latex_fraction(numerator=numerator, denominator=denominator)}",
comparison_operator_label="=",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ def __str__(self) -> str:
"""Returns a string representation of the formula."""
return self.latex().complete

def latex(self) -> LatexFormula:
def latex(self, n: int = 2) -> LatexFormula:
"""Returns LatexFormula object for formula 2.4."""
return LatexFormula(
return_symbol="",
equation="E_{dst;d} \\leq E_{stb;d} + T_d",
result="OK" if self.__bool__() else "\\text{Not OK}",
numeric_equation=f"{self.e_dst_d:.2f} \\leq {self.e_stb_d:.2f} + {self.t_d:.2f} \\to {self.e_dst_d:.2f} \\leq "
f"{self.e_stb_d + self.t_d:.2f}",
numeric_equation=f"{self.e_dst_d:.{n}f} \\leq {self.e_stb_d:.{n}f} + {self.t_d:.{n}f} \\to {self.e_dst_d:.{n}f} \\leq "
f"{self.e_stb_d + self.t_d:.{n}f}",
comparison_operator_label="\\to",
)
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ def _evaluate(
raise_if_negative(beta_cc_t=beta_cc_t, f_cm=f_cm)
return beta_cc_t * f_cm

def latex(self) -> LatexFormula:
def latex(self, n: int = 3) -> LatexFormula:
"""Returns LatexFormula object for formula 3.1."""
return LatexFormula(
return_symbol=r"f_{cm}(t)",
result=f"{self:.3f}",
result=f"{self:.{n}f}",
equation=r"\beta_{cc}(t) \cdot f_{cm}",
numeric_equation=rf"{self.beta_cc_t:.3f} \cdot {self.f_cm:.3f}",
numeric_equation=rf"{self.beta_cc_t:.{n}f} \cdot {self.f_cm:.{n}f}",
comparison_operator_label="=",
)
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ def _evaluate(
raise ValueError(f"Invalid h_0: {h_0}. h_0 cannot be negative or zero")
return (t - t_s) / ((t - t_s) + 0.04 * np.sqrt(h_0**3))

def latex(self) -> LatexFormula:
def latex(self, n: int = 2) -> LatexFormula:
"""Returns LatexFormula object for formula 3.10 formula."""
return LatexFormula(
return_symbol=r"\beta_{ds}(t,t_s)",
result=f"{self:.3f}",
result=f"{self:.{n}f}",
equation=r"\frac{(t - t_s)}{(t - t_s) + 0.04 \sqrt{h_0^3}}",
numeric_equation=rf"\frac{{({self.t:.2f} - {self.t_s:.2f})}}{{({self.t:.2f} - {self.t_s:.2f}) + 0.04 \sqrt{{{self.h_0:.2f}^3}}}}",
numeric_equation=rf"\frac{{({self.t:.{n}f} - {self.t_s:.{n}f})}}{{({self.t:.{n}f} - {self.t_s:.{n}f}) + "
rf"0.04 \sqrt{{{self.h_0:.{n}f}^3}}}}",
comparison_operator_label="=",
)

Expand Down Expand Up @@ -110,12 +111,12 @@ def _evaluate(
raise ValueError(f"Invalid u: {u}. u cannot be negative or zero")
return 2 * a_c / u

def latex(self) -> LatexFormula:
def latex(self, n: int = 2) -> LatexFormula:
"""Returns LatexFormula object for formula 3.10 subformula."""
return LatexFormula(
return_symbol=r"h_0",
result=f"{self:.2f}",
result=f"{self:.{n}f}",
equation=r"2 \cdot A_c / u",
numeric_equation=rf"2 \cdot {self.a_c:.2f} / {self.u:.2f}",
numeric_equation=rf"2 \cdot {self.a_c:.{n}f} / {self.u:.{n}f}",
comparison_operator_label="=",
)
Loading
Loading