Skip to content
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

Docstring all the things #202

Merged
merged 20 commits into from
Oct 24, 2023
Merged
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
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@
"mypy-type-checker.reportingScope": "workspace",
"pylint.severity": {
"refactor": "Information"
}
},
"python.analysis.typeCheckingMode": "basic",
}
80 changes: 48 additions & 32 deletions anastruct/basic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import collections.abc
from typing import Any, Sequence, Tuple

import numpy as np
Expand All @@ -13,71 +12,88 @@


def find_nearest(array: np.ndarray, value: float) -> Tuple[float, int]:
"""Find the nearest value in an array

Args:
array (np.ndarray): array to search within
value (float): value to search for

Returns:
Tuple[float, int]: Nearest value, index
"""
:param array: (numpy array object)
:param value: (float) value searched for
:return: (tuple) nearest value, index
"""

# Subtract the value of the value's in the array. Make the values absolute.
# The lowest value is the nearest.
index = (np.abs(array - value)).argmin()
index: int = (np.abs(array - value)).argmin()
return array[index], index


def integrate_array(y: np.ndarray, dx: float) -> np.ndarray:
"""
integrate array y * dx
"""Integrate an array y*dx using the trapezoidal rule

Args:
y (np.ndarray): Array to integrate
dx (float): Step size

Returns:
np.ndarray: Integrated array
"""
return np.cumsum(y) * dx # type: ignore


class FEMException(Exception):
def __init__(self, type_: str, message: str):
"""Exception for FEM

Args:
type_ (str): Type of exception
message (str): Message for exception
"""
self.type = type_
self.message = message


def arg_to_list(arg: Any, n: int) -> list:
"""Convert an argument to a list of length n

Args:
arg (Any): Argument to convert
n (int): Length of list to create

Returns:
list: List of length n
"""
if isinstance(arg, Sequence) and not isinstance(arg, str) and len(arg) == n:
return list(arg)
if isinstance(arg, Sequence) and not isinstance(arg, str) and len(arg) == 1:
return [arg[0] for _ in range(n)]
return [arg for _ in range(n)]


def args_to_lists(*args: list) -> list:
arg_lists = []
for arg in args:
if isinstance(arg, collections.abc.Iterable) and not isinstance(arg, str):
arg_lists.append(arg)
else:
arg_lists.append([arg])
lengths = list(map(len, arg_lists))
n = max(lengths)
if n == 1:
return arg_lists

args_return = []
for arg, l in zip(arg_lists, lengths):
if l == n:
args_return.append(arg)
else:
args_return.append([arg[0] for _ in range(n)])
return args_return


def rotation_matrix(angle: float) -> np.ndarray:
"""
"""Create a 2x2 rotation matrix

:param angle: (flt) angle in radians
:return: rotated euclidean xy matrix
Args:
angle (float): Angle in radians

Returns:
np.ndarray: 2x2 Euclidean rotation matrix
"""
s = np.sin(angle)
c = np.cos(angle)
return np.array([[c, -s], [s, c]])


def rotate_xy(a: np.ndarray, angle: float) -> np.ndarray:
"""Rotate a 2D matrix around the origin

Args:
a (np.ndarray): Matrix to rotate
angle (float): Angle in radians

Returns:
np.ndarray: Rotated matrix
"""
b = np.array(a)
b[:, 0] -= a[0, 0]
b[:, 1] -= a[0, 1]
Expand Down
22 changes: 16 additions & 6 deletions anastruct/cython/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@


def converge(lhs: float, rhs: float) -> float:
"""
Determine convergence factor.
"""Determine convergence factor

Args:
lhs (float): The left-hand side of the equation
rhs (float): The right-hand side of the equation

:param lhs: (flt)
:param rhs: (flt)
:param div: (flt)
:return: multiplication factor (flt) ((lhs / rhs) - 1) / div + 1
Returns:
float: Convergence factor ((lhs / rhs) - 1) / div + 1
"""
lhs = abs(lhs)
rhs = abs(rhs)
Expand All @@ -18,6 +19,15 @@ def converge(lhs: float, rhs: float) -> float:


def angle_x_axis(delta_x: float, delta_z: float) -> float:
"""Determine the angle of the element with the global x-axis

Args:
delta_x (float): Element length in the x-direction
delta_z (float): Element length in the z-direction

Returns:
float: Angle of the element with the global x-axis
"""
# dot product v_x = [1, 0] ; v = [delta_x, delta_z]
# dot product = 1 * delta_x + 0 * delta_z -> delta_x
ai = math.acos(delta_x / math.sqrt(delta_x**2 + delta_z**2))
Expand Down
Loading