Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from mpewsey/v1.1.0
Browse files Browse the repository at this point in the history
Add polygon
  • Loading branch information
mpewsey authored Jan 26, 2019
2 parents 0998262 + 42cee19 commit a8019eb
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 19 deletions.
1 change: 1 addition & 0 deletions docs/releases/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Release Notes v1.1.0
====================

* Updated package for compatibility with Python 2.7.
* Added polygon functions.
5 changes: 5 additions & 0 deletions examples/polygon_ex1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# polygon_ex1.py
from xsect import polygon_points, plot_section

points = polygon_points(5, 1, 0.1)
plot_section(points, title='Polygon')
15 changes: 15 additions & 0 deletions xsect/calc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@
round_summary
Polygon Functions
=================
The following functions may be used to calculate the cross sectional properties
for solid or thin walled polygon sections.
.. plot:: ../examples/polygon_ex1.py
.. autosummary::
:toctree: generated/
polygon_points
polygon_summary
Angle Functions
===============
The following functions may be used to calculate cross sectional properties
Expand Down Expand Up @@ -165,5 +179,6 @@
from .double_angle import *
from .i_beam import *
from .multi import *
from .polygon import *
from .round import *
from .t_beam import *
11 changes: 11 additions & 0 deletions xsect/calc/_plt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This module configures matplotlib

import os
import matplotlib

if 'DISPLAY' not in os.environ:
matplotlib.use('Agg')

import matplotlib.pyplot as plt

__all__ = ['plt']
6 changes: 1 addition & 5 deletions xsect/calc/boundary.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
from __future__ import division
import os
import numpy as np
import matplotlib
if os.environ.get('DISPLAY', None) is None:
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from ._plt import plt

__all__ = [
'rotate2',
Expand Down
3 changes: 0 additions & 3 deletions xsect/calc/boundary_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ def test_area():
points = sample_angles(rand)
sol = np.array([area(x) for x in points])

print(sol)
print(a)

assert approx(sol, 0.01) == a


Expand Down
12 changes: 6 additions & 6 deletions xsect/calc/cross_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ def __repr__(self):
s = ['{}={!r}'.format(k, getattr(self, k)) for k in attrs]
return '{}({})'.format(type(self).__name__, ', '.join(s))

@staticmethod
def from_points(name, add, subtract=[], is_round=False,
@classmethod
def from_points(cls, name, add, subtract=[], is_round=False,
include_meta=True, **kwargs):
"""
Initializes a cross section from boundary points.
Expand Down Expand Up @@ -132,16 +132,16 @@ def from_points(name, add, subtract=[], is_round=False,
odict['is_round'] = is_round
odict.update(kwargs)

xsect = CrossSection(**odict)
xsect = cls(**odict)

if not include_meta:
xsect.meta.clear()

return xsect


@staticmethod
def from_aisc(name, metric=False, version=None, include_meta=True):
@classmethod
def from_aisc(cls, name, metric=False, version=None, include_meta=True):
"""
Initializes a cross section from the properties in the AISC database.
Expand Down Expand Up @@ -172,7 +172,7 @@ def from_aisc(name, metric=False, version=None, include_meta=True):
if odict['type'] == 'PIPE':
odict['is_round'] = True

xsect = CrossSection(**odict)
xsect = cls(**odict)

if not include_meta:
xsect.meta.clear()
Expand Down
6 changes: 1 addition & 5 deletions xsect/calc/multi.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
from __future__ import division
import os
import numpy as np
import matplotlib
if os.environ.get('DISPLAY', None) is None:
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from ._plt import plt
from .boundary import TOL, area, centroid, inertias, close_points, rotate2

__all__ = [
Expand Down
66 changes: 66 additions & 0 deletions xsect/calc/polygon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from __future__ import division
import numpy as np
from .boundary import close_points, section_summary

__all__ = ['polygon_points', 'polygon_summary']


def polygon_points(n, radius, thickness=None, is_inscribed=True):
"""
Returns an array of boundary points for a polygon.
Parameters
----------
n : int
The number of sides to the polygon.
radius : float
The radius of the polygon.
thickness : float
The thickness of the wall. If None, the cross section will be assumed
to be solid.
is_inscribed : bool
If True, an inscribed polygon will be generated for the specified
radius. Otherwise, a circumscribed polygon will be generated.
"""
c = np.cos(np.pi/n)

if is_inscribed:
ro = radius
else:
ro = radius / c

ang = np.linspace(0.5*np.pi, 2.5*np.pi, n+1)
ang = np.column_stack([np.cos(ang), np.sin(ang)])
points = ro * ang

if thickness is not None:
if is_inscribed:
ri = ro - thickness / c
else:
ri = (radius - thickness) / c

p = ri * ang[::-1]
points = np.concatenate([points, p])

return close_points(points)


def polygon_summary(n, radius, thickness=None, is_inscribed=True):
"""
Returns a dictionary with a summary of polygon section properties.
Parameters
----------
n : int
The number of sides to the polygon.
radius : float
The radius of the polygon.
thickness : float
The thickness of the wall. If None, the cross section will be assumed
to be solid.
is_inscribed : bool
If True, an inscribed polygon will be generated for the specified
radius. Otherwise, a circumscribed polygon will be generated.
"""
p = polygon_points(n, radius, thickness, is_inscribed)
return section_summary(p)
21 changes: 21 additions & 0 deletions xsect/calc/polygon_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest
import numpy as np
from .polygon import *


def test_polygon_summary():
# Inscribed
p = polygon_summary(6, 1, is_inscribed=True)
assert pytest.approx(p['area']) == 2.5980762113533

p = polygon_summary(6, 1, 0.1, is_inscribed=True)
a = 2.5980762113533 - 2.0327172275047
assert pytest.approx(p['area']) == a

# Circumscribed
p = polygon_summary(6, 1, is_inscribed=False)
assert pytest.approx(p['area']) == 3.4641016151378

p = polygon_summary(6, 1, 0.1, is_inscribed=False)
a = 3.4641016151378 - 2.8059223082616
assert pytest.approx(p['area']) == a

0 comments on commit a8019eb

Please sign in to comment.