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

Added function to get block info #43

Merged
merged 12 commits into from
Aug 11, 2021
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.mod
*.o
*.so
*.out

# auto generated files
src/libcgns_utils-f2pywrappers2.f90
Expand Down
62 changes: 48 additions & 14 deletions cgnsutilities/cgnsutilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ def __init__(self):
self.name = "domain"
self.cellDim = 3

@staticmethod
ewu63 marked this conversation as resolved.
Show resolved Hide resolved
def getBlockCellsNodes(blk):
blockCells = (blk.dims[0] - 1) * (blk.dims[1] - 1) * (blk.dims[2] - 1)
blockNodes = blk.dims[0] * blk.dims[1] * blk.dims[2]

return blockCells, blockNodes

def getTotalCellsNodes(self):
"""
Returns the total number of Cells and Nodes in the grid.
Expand All @@ -65,8 +72,9 @@ def getTotalCellsNodes(self):
totalCells = 0
totalNodes = 0
for blk in self.blocks:
totalCells += (blk.dims[0] - 1) * (blk.dims[1] - 1) * (blk.dims[2] - 1)
totalNodes += blk.dims[0] * blk.dims[1] * blk.dims[2]
blockCells, blockNodes = self.getBlockCellsNodes(blk)
totalCells += blockCells
totalNodes += blockNodes

return totalCells, totalNodes

Expand Down Expand Up @@ -127,26 +135,52 @@ def printInfo(self):
print("Wall Boundary Cells:", boundaryCells)
print("Wall Boundary Nodes:", boundaryNodes)

def printBlockInfo(self):
"""Print some information on each block to screen.
This info can be helpful assessing overset meshes"""
def getBlockInfo(self):
ewu63 marked this conversation as resolved.
Show resolved Hide resolved
"""Get the number of nodes, number of cells, BCs, and
the dimensions for each block. This info can be helpful
for assessing overset meshes."""

totalCells = 0
totalNodes = 0
counter = 1
allBlocksInfo = {}

for blk in self.blocks:
nCells = (blk.dims[0] - 1) * (blk.dims[1] - 1) * (blk.dims[2] - 1)
nNodes = blk.dims[0] * blk.dims[1] * blk.dims[2]
print("Block Number:", counter)
print("Number of Cells:", nCells)
print("Number of Nodes:", nNodes)
print("Block dimensions:", list(blk.dims))
blockInfo = {}
nCells, nNodes = self.getBlockCellsNodes(blk)
blockInfo["nCells"] = nCells
blockInfo["nNodes"] = nNodes
blockInfo["dims"] = list(blk.dims)
blockInfo["BCs"] = [boco.type for boco in blk.bocos]
allBlocksInfo[f"{counter}"] = blockInfo
totalCells += nCells
totalNodes += nNodes
counter += 1
print("Total Zones:", len(self.blocks))
print("Total Cells:", totalCells)
print("Total Nodes:", totalNodes)

allBlocksInfo["totalZones"] = len(self.blocks)
allBlocksInfo["totalCells"] = totalCells
allBlocksInfo["totalNodes"] = totalNodes

return allBlocksInfo

def printBlockInfo(self):
"""Print the number of nodes, number of cells, and
the dimensions for each block. This info can be helpful
for assessing overset meshes."""

allBlocksInfo = self.getBlockInfo()

for i in range(len(self.blocks)):
blockNumber = str(i + 1)
print("Block Number:", blockNumber)
blockInfo = allBlocksInfo[blockNumber]
print("Number of Cells:", blockInfo["nCells"])
print("Number of Nodes:", blockInfo["nNodes"])
print("Block dimensions:", blockInfo["dims"])

print("Total Zones:", allBlocksInfo["totalZones"])
print("Total Cells:", allBlocksInfo["totalCells"])
print("Total Nodes:", allBlocksInfo["totalNodes"])

def addBlock(self, blk):

Expand Down
184 changes: 184 additions & 0 deletions tests/ref/blockInfo.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
{
"Block info": {
"1": {
"BCs": [
22,
7,
16
],
"dims": [
{
"__ndarray__": 19,
"dtype": "int32",
"shape": []
},
{
"__ndarray__": 19,
"dtype": "int32",
"shape": []
},
{
"__ndarray__": 21,
"dtype": "int32",
"shape": []
}
],
"nCells": {
"__ndarray__": 6480,
"dtype": "int64",
"shape": []
},
"nNodes": {
"__ndarray__": 7581,
"dtype": "int32",
"shape": []
}
},
"2": {
"BCs": [
22,
7,
16
],
"dims": [
{
"__ndarray__": 3,
"dtype": "int32",
"shape": []
},
{
"__ndarray__": 19,
"dtype": "int32",
"shape": []
},
{
"__ndarray__": 21,
"dtype": "int32",
"shape": []
}
],
"nCells": {
"__ndarray__": 720,
"dtype": "int64",
"shape": []
},
"nNodes": {
"__ndarray__": 1197,
"dtype": "int32",
"shape": []
}
},
"3": {
"BCs": [
22,
7,
16
],
"dims": [
{
"__ndarray__": 19,
"dtype": "int32",
"shape": []
},
{
"__ndarray__": 19,
"dtype": "int32",
"shape": []
},
{
"__ndarray__": 21,
"dtype": "int32",
"shape": []
}
],
"nCells": {
"__ndarray__": 6480,
"dtype": "int64",
"shape": []
},
"nNodes": {
"__ndarray__": 7581,
"dtype": "int32",
"shape": []
}
},
"4": {
"BCs": [
22,
7,
16
],
"dims": [
{
"__ndarray__": 3,
"dtype": "int32",
"shape": []
},
{
"__ndarray__": 19,
"dtype": "int32",
"shape": []
},
{
"__ndarray__": 21,
"dtype": "int32",
"shape": []
}
],
"nCells": {
"__ndarray__": 720,
"dtype": "int64",
"shape": []
},
"nNodes": {
"__ndarray__": 1197,
"dtype": "int32",
"shape": []
}
},
"5": {
"BCs": [
22,
7
],
"dims": [
{
"__ndarray__": 3,
"dtype": "int32",
"shape": []
},
{
"__ndarray__": 19,
"dtype": "int32",
"shape": []
},
{
"__ndarray__": 21,
"dtype": "int32",
"shape": []
}
],
"nCells": {
"__ndarray__": 720,
"dtype": "int64",
"shape": []
},
"nNodes": {
"__ndarray__": 1197,
"dtype": "int32",
"shape": []
}
},
"totalCells": {
"__ndarray__": 15120,
"dtype": "int64",
"shape": []
},
"totalNodes": {
"__ndarray__": 18753,
"dtype": "int64",
"shape": []
},
"totalZones": 5
}
}
12 changes: 12 additions & 0 deletions tests/ref/totalCellsNodes.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Total cells": {
"__ndarray__": 15120,
"dtype": "int64",
"shape": []
},
"Total nodes": {
"__ndarray__": 18753,
"dtype": "int64",
"shape": []
}
}
12 changes: 12 additions & 0 deletions tests/ref/wallCellsNodes.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Wall cells": {
"__ndarray__": 756,
"dtype": "int64",
"shape": []
},
"Wall nodes": {
"__ndarray__": 893,
"dtype": "int64",
"shape": []
}
}
32 changes: 26 additions & 6 deletions tests/test_cgnsutilities.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import subprocess
import unittest
from baseclasses import BaseRegTest
ewu63 marked this conversation as resolved.
Show resolved Hide resolved
from cgnsutilities.cgnsutilities import readGrid, BC

baseDir = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -10,15 +11,34 @@ class TestGrid(unittest.TestCase):
def setUp(self):
self.grid = readGrid(os.path.abspath(os.path.join(baseDir, "../examples/717_wl_L2.cgns")))

def test_getTotalCellsNodes(self):
def test_getTotalCellsNodes(self, train=False):
totalCells, totalNodes = self.grid.getTotalCellsNodes()
self.assertEqual(totalCells, 15120)
self.assertEqual(totalNodes, 18753)
refFile = os.path.join(baseDir, "ref", "totalCellsNodes.ref")
with BaseRegTest(refFile, train=train) as handler:
handler.root_add_val("Total cells", totalCells, tol=0)
handler.root_add_val("Total nodes", totalNodes, tol=0)

def test_getWallCellsNodes(self):
def train_getTotalCellsNodes(self):
self.test_getTotalCellsNodes(train=True)

def test_getWallCellsNodes(self, train=False):
Copy link
Collaborator

@joanibal joanibal Jul 30, 2021

Choose a reason for hiding this comment

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

I'm not a fan of this pattern. I'd rather have just one test, but that seem like a bigger issue than just this PR

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it makes sense to have separate tests for each function because then it is easy to see what is broken if the test fails. I don't like having a separate train function and ref file for each test though.

nWallCells, nWallNodes = self.grid.getWallCellsNodes()
self.assertEqual(nWallCells, 756)
self.assertEqual(nWallNodes, 893)
refFile = os.path.join(baseDir, "ref", "wallCellsNodes.ref")
with BaseRegTest(refFile, train=train) as handler:
handler.root_add_val("Wall cells", nWallCells, tol=0)
handler.root_add_val("Wall nodes", nWallNodes, tol=0)

def train_getWallCellsNodes(self):
self.test_getWallCellsNodes(train=True)

def test_getBlockInfo(self, train=False):
blockInfo = self.grid.getBlockInfo()
refFile = os.path.join(baseDir, "ref", "blockInfo.ref")
with BaseRegTest(refFile, train=train) as handler:
handler.root_add_dict("Block info", blockInfo, tol=0)

def train_getBlockInfo(self):
self.test_getBlockInfo(train=True)

def test_overwriteFamilies(self):
# Find a specific BC and overwrite the family
Expand Down