Commit 21ba47e7 authored by John Tourtellott's avatar John Tourtellott
Browse files

Migrate Omega3P test from smtk source to this repo

Change from mesh session to vtk session, since that is the default
for modelbuilder
parent da77cbc3
cmake_minimum_required(VERSION 3.12)
project(ace3p-extension VERSION 1.0)
project(ace3p-extensions VERSION 1.0)
option(BUILD_SHARED_LIBS "Build CMB using shared libraries" ON)
......@@ -19,12 +19,12 @@ if (ENABLE_TESTING)
include(TestingMacros)
endif()
# Nlohmann json
find_package(nlohmann_json REQUIRED)
# Find smtk
find_package(smtk REQUIRED)
# Nlohmann json
# find_package(nlohmann_json REQUIRED)
if(WIN32 AND MSVC)
#setup windows exception handling so we can compile properly with boost
#enabled
......@@ -101,3 +101,8 @@ else()
DESTINATION "${SIMULATION_WORKFLOWS_ROOT}/ACE3P"
)
endif ()
if (ENABLE_TESTING)
enable_testing()
add_subdirectory(testing)
endif()
add_subdirectory(python)
set(ace3p_python_tests)
if (SMTK_ENABLE_VTK_SUPPORT)
list(APPEND ace3p_python_tests
omega3p_test1
)
endif()
set(smtk_pythonpath ${smtk_DIR}/${SMTK_PYTHONPATH})
if(NOT IS_ABSOLUTE ${smtk_pythonpath})
get_filename_component(smtk_pythonpath
${PROJECT_BINARY_DIR}/${smtk_DIR}/${SMTK_PYTHON_MODULEDIR} ABSOLUTE)
endif()
set(pyenv
${PROJECT_BINARY_DIR}
${smtk_pythonpath}
$ENV{PYTHONPATH})
if (WIN32)
string(REPLACE ";" "\;" pyenv "${pyenv}")
else ()
string(REPLACE ";" ":" pyenv "${pyenv}")
endif()
set(pathenv)
if (WIN32)
set(pathenv
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
$ENV{PATH})
string(REPLACE ";" "\;" pathenv "${pathenv}")
endif()
foreach (test ${ace3p_python_tests})
add_test(
NAME ${test}_py
COMMAND "${PYTHON_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/${test}.py"
--data-dir=${PROJECT_SOURCE_DIR}/data
--temp-dir=${CMAKE_BINARY_DIR}/Testing/Temporary
--src-dir=${CMAKE_SOURCE_DIR}
)
set_tests_properties("${test}_py"
PROPERTIES
ENVIRONMENT "PYTHONPATH=${pyenv}"
LABELS "ACE3P Simulation"
)
if (pathenv)
set_property(TEST "${test}_py" APPEND
PROPERTY
ENVIRONMENT "PATH=${pathenv}"
)
endif ()
endforeach()
#=============================================================================
#
# Copyright (c) Kitware, Inc.
# All rights reserved.
# See LICENSE.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even
# the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the above copyright notice for more information.
#
#=============================================================================
import os
import shutil
import sys
import unittest
import smtk
import smtk.attribute
import smtk.io
import smtk.operation
import smtk.resource
import smtk.session.vtk
import smtk.testing
import util
OPERATION_SUCCEEDED = int(smtk.operation.Operation.SUCCEEDED) # 3
MODEL_NAME = 'pillbox4'
OUTPUT_FOLDER = 'export_{}'.format(MODEL_NAME)
class Omega3PTest1(smtk.testing.TestCase):
def setUp(self):
# Make sure last test result is removed
export_folder = os.path.join(smtk.testing.TEMP_DIR, OUTPUT_FOLDER)
if os.path.exists(export_folder):
shutil.rmtree(export_folder)
# Initialize smtk managers
self.res_manager = smtk.resource.Manager.create()
self.op_manager = smtk.operation.Manager.create()
smtk.attribute.Registrar.registerTo(self.res_manager)
smtk.attribute.Registrar.registerTo(self.op_manager)
smtk.session.vtk.Registrar.registerTo(self.res_manager)
smtk.session.vtk.Registrar.registerTo(self.op_manager)
smtk.operation.Registrar.registerTo(self.op_manager)
self.op_manager.registerResourceManager(self.res_manager)
def tearDown(self):
self.res_manager = None
self.op_manager = None
def import_resource(self, path):
import_op = self.op_manager.createOperation(
'smtk::operation::ImportResource')
import_op.parameters().find('filename').setValue(path)
import_result = import_op.operate()
import_outcome = import_result.findInt('outcome').value(0)
self.assertEqual(import_outcome, OPERATION_SUCCEEDED)
resource = import_result.find('resource').value()
self.assertIsNotNone(resource)
return resource
def import_python_op(self, path):
import_op = self.op_manager.createOperation(
'smtk::operation::ImportPythonOperation')
self.assertIsNotNone(import_op)
import_op.parameters().find('filename').setValue(path)
import_result = import_op.operate()
import_outcome = import_result.findInt('outcome').value(0)
self.assertEqual(import_outcome, OPERATION_SUCCEEDED)
op_unique_name = import_result.findString("unique_name").value()
op = self.op_manager.createOperation(op_unique_name)
return op
def test_pillbox4(self):
# Minimal test case
gen_filename = '{}.gen'.format(MODEL_NAME)
gen_path = os.path.join(
smtk.testing.SOURCE_DIR, 'data', 'model', '3d', 'genesis', gen_filename)
resource = self.import_resource(gen_path)
model_resource = smtk.model.Resource.CastTo(resource)
self.assertIsNotNone(model_resource)
# Set the model location in lieu of writing it to the file system
model_filename = '{}.smtk'.format(MODEL_NAME)
model_location = os.path.join(smtk.testing.TEMP_DIR, model_filename)
model_resource.setLocation(model_location)
# Load attribute template
sbt_path = os.path.join(
smtk.testing.SOURCE_DIR, 'simulation-workflows', 'ACE3P.sbt')
self.assertTrue(os.path.exists(sbt_path))
att_resource = self.import_resource(sbt_path)
self.assertIsNotNone(att_resource)
# Associate model to attribute resource
self.assertTrue(att_resource.associate(model_resource))
# Create attributes for instanced views
util.create_instanced_atts(att_resource)
# Populate simulation attributes
bc_lookup = {
1: 'Electric',
2: 'Electric'
}
bc_default = 'Exterior'
uuids = model_resource.entitiesMatchingFlags(smtk.model.FACE, True)
for uuid in uuids:
# print('UUID {}'.format(uuid))
prop_list = model_resource.integerProperty(uuid, 'pedigree id')
face_id = prop_list[0]
# print('Face ID {}'.format(face_id))
bc_type = bc_lookup.get(face_id, bc_default)
bc_att = att_resource.createAttribute(bc_type)
self.assertIsNotNone(bc_att)
self.assertTrue(bc_att.associateEntity(uuid))
finfo_att = att_resource.findAttributes('FrequencyInfo')[0]
num_item = finfo_att.findInt('NumEigenvalues')
self.assertTrue(num_item.setValue(3))
# Get export operator
py_path = os.path.join(
smtk.testing.SOURCE_DIR, 'simulation-workflows', 'ACE3P.py')
self.assertTrue(os.path.exists(py_path))
export_op = self.import_python_op(py_path)
self.assertIsNotNone(export_op)
# Setup export
params_att = export_op.parameters()
att_item = params_att.findResource("attributes")
att_item.setValue(att_resource)
model_item = params_att.findResource("model")
model_item.setValue(model_resource)
analysis_item = params_att.findString('Analysis')
analysis_item.setValue('omega3p')
# Set MeshFile to the input (genesis) file
file_item = params_att.findFile('MeshFile')
file_item.setValue(gen_path)
# Set the output items
export_path = os.path.join(smtk.testing.TEMP_DIR, OUTPUT_FOLDER)
dir_item = analysis_item.findChild(
'OutputFolder', smtk.attribute.SearchStyle.IMMEDIATE_ACTIVE)
dir_item.setValue(export_path)
prefix_item = analysis_item.findChild(
'OutputFilePrefix', smtk.attribute.SearchStyle.IMMEDIATE_ACTIVE)
prefix_item.setValue(MODEL_NAME)
# Run the operator
result = export_op.operate()
outcome = result.findInt('outcome')
self.assertEqual(outcome.value(), OPERATION_SUCCEEDED)
# Check that expected file was created
expected_filename = '{}.omega3p'.format(MODEL_NAME)
expected_path = os.path.join(export_path, expected_filename)
self.assertTrue(os.path.exists(expected_path))
# Sanity check number of lines
num_lines = len(open(expected_path).readlines())
self.assertGreater(num_lines, 12)
# (On success) remove the export folder
shutil.rmtree(export_path)
if __name__ == '__main__':
smtk.testing.process_arguments()
unittest.main()
#=============================================================================
#
# Copyright (c) Kitware, Inc.
# All rights reserved.
# See LICENSE.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even
# the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the above copyright notice for more information.
#
#=============================================================================
"""Utility functions for simulation-export tests"""
import smtk
import smtk.view
def _instantiate_instanced_view(att_resource, view):
"""Initialize attributes specified in instanced view
"""
comp = view.details()
atts_comp = comp.child(0)
for i in range(atts_comp.numberOfChildren()):
att_comp = atts_comp.child(i)
att_name = att_comp.attributes().get('Name')
att_type = att_comp.attributes().get('Type')
# Attributes can appean in multiple instanced views, so check if
# att has already been created.
att = att_resource.findAttribute(att_name)
if att is not None:
return
defn = att_resource.findDefinition(att_type)
if defn is None:
raise RuntimeError('Definition {} not found'.format(att_type))
att = att_resource.createAttribute(att_name, defn)
# print('Created attribute \"{}\" type \"{}\"').format(att_name,
# att_type)
def _recursive_create_instanced_atts(att_resource, comp):
"""Traverse view components to find/create instanced attributes
"""
if comp is None:
raise RuntimeError('Component is None')
if comp.name() == 'View':
title = comp.attributes().get('Title')
view = att_resource.findView(title)
if view is None:
raise RuntimeError('View {} not found'.format(title))
if view.type() == 'Instanced':
_instantiate_instanced_view(att_resource, view)
else:
_recursive_create_instanced_atts(att_resource, view.details())
return
# (else) process component children
for i in range(comp.numberOfChildren()):
child = comp.child(i)
_recursive_create_instanced_atts(att_resource, child)
def create_instanced_atts(att_resource):
"""Instantiates all attributes referenced in Instanced views
Code doesn't instantiate *all* instanced views, but instead
traverses from top-level view to find those instanced views
that will be displayed.
"""
top_view = att_resource.findTopLevelView()
if top_view is None:
print('WARNING: Attribute resource has no top-level view')
return
_recursive_create_instanced_atts(att_resource, top_view.details())
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment