Skip to content

Commit

Permalink
Fix more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ansjmoody committed Jan 27, 2025
1 parent 244b08c commit c437b86
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 255 deletions.
4 changes: 2 additions & 2 deletions examples/01-project-configuration/add_cca.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import os

from ansys.sherlock.core import launcher
from ansys.sherlock.core.errors import SherlockAddCCAError, SherlockImportODBError
from ansys.sherlock.core.errors import SherlockAddCCAError, SherlockImportProjectZipArchiveError

###############################################################################
# Launch PySherlock service
Expand All @@ -68,7 +68,7 @@
archive_file=(os.path.join(ANSYS_ROOT, "sherlock", "tutorial", "Tutorial Project.zip")),
)
print("Tutorial project imported successfully.")
except SherlockImportODBError as e:
except SherlockImportProjectZipArchiveError as e:
print(f"Error importing ODB++ archive: {str(e)}")

###############################################################################
Expand Down
11 changes: 6 additions & 5 deletions examples/02-analyses/run_harmonic_vibe_strain_map_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,12 @@
except SherlockImportProjectZipArchiveError as e:
print(f"Error importing project zip archive: {e}")

###############################################################################
# Add Strain Map
# ====================================
# Add a strain map to the project.

try:
# Add strain maps to the project
strain_map_path = os.path.join(
ANSYS_ROOT, "sherlock", "tutorial", "StrainMaps", "StrainMap.csv"
)
Expand Down Expand Up @@ -145,15 +149,12 @@
# Run the strain map analysis, including harmonic vibration and other analysis types.

try:
analysis_type_enum = RunStrainMapAnalysisRequest.StrainMapAnalysis.AnalysisType

analysis_type = analysis_type_enum.HarmonicVibe
sherlock.analysis.run_strain_map_analysis(
"Test",
"Main Board",
[
[
analysis_type,
RunStrainMapAnalysisRequest.StrainMapAnalysis.AnalysisType.HarmonicVibe,
[
["On The Road", "5 - Harmonic Vibe", "TOP", "StrainMap - Top"],
["On The Road", "5 - Harmonic Vibe", "BOTTOM", "StrainMap - Bottom"],
Expand Down
73 changes: 30 additions & 43 deletions examples/02-analyses/run_mechanical_shock_strain_map_analysis.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2024 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2025 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -43,14 +43,13 @@
# sphinx_gallery_thumbnail_path = './images/sherlock_run_mechanical_shock_analysis_example.png'

import os
import time

from SherlockAnalysisService_pb2 import RunStrainMapAnalysisRequest

from ansys.sherlock.core import launcher
from ansys.sherlock.core.errors import (
SherlockAddStrainMapsError,
SherlockImportODBError,
SherlockImportProjectZipArchiveError,
SherlockRunStrainMapAnalysisError,
)
from ansys.sherlock.core.types.analysis_types import ModelSource
Expand All @@ -67,42 +66,28 @@
sherlock = launcher.launch_sherlock(port=9092)

###############################################################################
# Import ODB Archive and Strain Maps
# ===================================
# Import a project and add strain maps to the Sherlock project.
# Import Tutorial Project
# ========================
# Import the tutorial project zip archive from the Sherlock tutorial directory.

try:
# Import ODB++ archive into the project
sherlock.project.import_odb_archive(
ANSYS_ROOT
+ os.path.sep
+ "sherlock"
+ os.path.sep
+ "tutorial"
+ os.path.sep
+ "ODB++ Tutorial.tgz",
True,
True,
True,
True,
sherlock.project.import_project_zip_archive(
project="Test",
cca_name="Card",
category="Demos",
archive_file=(os.path.join(ANSYS_ROOT, "sherlock", "tutorial", "Tutorial Project.zip")),
)
except SherlockImportODBError as e:
print(f"Error importing ODB archive: {str(e)}")
print("Tutorial project imported successfully.")
except SherlockImportProjectZipArchiveError as e:
print(f"Error importing project zip archive: {e}")

###############################################################################
# Add Strain Map
# ====================================
# Add a strain map to the project.

try:
# Add strain maps to the project
strain_map_path = (
ANSYS_ROOT
+ os.path.sep
+ "sherlock"
+ os.path.sep
+ "tutorial"
+ os.path.sep
+ "StrainMaps"
+ os.path.sep
+ "StrainMap.csv"
strain_map_path = os.path.join(
ANSYS_ROOT, "sherlock", "tutorial", "StrainMaps", "StrainMap.csv"
)
sherlock.project.add_strain_maps(
"Test",
Expand All @@ -114,13 +99,14 @@
0,
"SolidID",
"PCB Strain",
"\u03bc\u03b5",
["Card"],
"µε",
["Main Board"],
)
],
)
print("Strain maps added successfully.")
except SherlockAddStrainMapsError as e:
print(f"Error adding strain maps: {str(e)}")
print(f"Error adding strain maps: {e}")

###############################################################################
# Update Mechanical Shock Properties
Expand All @@ -133,7 +119,7 @@
"Test",
[
{
"cca_name": "Card",
"cca_name": "Main Board",
"model_source": ModelSource.STRAIN_MAP,
"shock_result_count": 1,
"part_validation_enabled": False,
Expand All @@ -148,8 +134,9 @@
}
],
)
print("Mechanical shock properties updated successfully.")
except SherlockRunStrainMapAnalysisError as e:
print(f"Error updating mechanical shock properties: {str(e)}")
print(f"Error updating mechanical shock properties: {e}")

###############################################################################
# Run Mechanical Shock Analysis
Expand All @@ -159,25 +146,25 @@
try:
sherlock.analysis.run_strain_map_analysis(
"Test",
"Card",
"Main Board",
[
[
RunStrainMapAnalysisRequest.StrainMapAnalysis.AnalysisType.MechanicalShock,
[
["Phase 1", "Shock Event", "TOP", "StrainMap - Top"],
["Phase 1", "Shock Event", "BOTTOM", "StrainMap - Bottom"],
["On The Road", "2 - Pothole", "TOP", "StrainMap - Top"],
["On The Road", "3 - Collision", "BOTTOM", "StrainMap - Bottom"],
],
]
],
)
print("Mechanical shock analysis executed successfully.")
except SherlockRunStrainMapAnalysisError as e:
print(f"Error running mechanical shock analysis: {str(e)}")
print(f"Error running mechanical shock analysis: {e}")

###############################################################################
# Exit Sherlock
# =============
# Exit the gRPC connection and shut down Sherlock.

time.sleep(5)
sherlock.common.exit(True)
print("Sherlock gRPC connection closed successfully.")
70 changes: 27 additions & 43 deletions examples/02-analyses/run_random_vibe_strain_map_analysis.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2024 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2025 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -43,14 +43,13 @@
# sphinx_gallery_thumbnail_path = './images/sherlock_run_random_vibe_analysis_example.png'

import os
import time

from ansys.api.sherlock.v0 import SherlockAnalysisService_pb2

from ansys.sherlock.core import launcher
from ansys.sherlock.core.errors import (
SherlockAddStrainMapsError,
SherlockImportODBError,
SherlockImportProjectZipArchiveError,
SherlockRunStrainMapAnalysisError,
)
from ansys.sherlock.core.types.analysis_types import (
Expand All @@ -70,42 +69,28 @@
sherlock = launcher.launch_sherlock(port=9092)

###############################################################################
# Import ODB Archive and Strain Maps
# ==================================
# Import a project and add strain maps to the Sherlock project.
# Import Tutorial Project
# ========================
# Import the tutorial project zip archive from the Sherlock tutorial directory.

try:
# Import ODB++ archive into the project
sherlock.project.import_odb_archive(
ANSYS_ROOT
+ os.path.sep
+ "sherlock"
+ os.path.sep
+ "tutorial"
+ os.path.sep
+ "ODB++ Tutorial.tgz",
True,
True,
True,
True,
sherlock.project.import_project_zip_archive(
project="Test",
cca_name="Card",
category="Demos",
archive_file=(os.path.join(ANSYS_ROOT, "sherlock", "tutorial", "Tutorial Project.zip")),
)
except SherlockImportODBError as e:
print(f"Error importing ODB archive: {str(e)}")
print("Tutorial project imported successfully.")
except SherlockImportProjectZipArchiveError as e:
print(f"Error importing project zip archive: {e}")

###############################################################################
# Add Strain Map
# ====================================
# Add a strain map to the project.

try:
# Add strain maps to the project
strain_map_path = (
ANSYS_ROOT
+ os.path.sep
+ "sherlock"
+ os.path.sep
+ "tutorial"
+ os.path.sep
+ "StrainMaps"
+ os.path.sep
+ "StrainMap.csv"
strain_map_path = os.path.join(
ANSYS_ROOT, "sherlock", "tutorial", "StrainMaps", "StrainMap.csv"
)
sherlock.project.add_strain_maps(
"Test",
Expand All @@ -118,23 +103,23 @@
"SolidID",
"PCB Strain",
"µε",
["Card"],
["Main Board"],
)
],
)
print("Strain maps added successfully.")
except SherlockAddStrainMapsError as e:
print(f"Error adding strain maps: {str(e)}")
print(f"Error adding strain maps: {e}")

###############################################################################
# Update Random Vibration Properties
# ==================================
# Configure properties for random vibration analysis using strain maps.

try:
# Update properties for random vibration analysis
sherlock.analysis.update_random_vibe_props(
project="Test",
cca_name="Card",
cca_name="Main Board",
model_source=ModelSource.STRAIN_MAP,
random_vibe_damping="0.01",
part_validation_enabled=False,
Expand All @@ -150,7 +135,7 @@
strain_map_natural_freqs="100, 200, 300",
)
except SherlockRunStrainMapAnalysisError as e:
print(f"Error updating random vibration properties: {str(e)}")
print(f"Error updating random vibration properties: {e}")

###############################################################################
# Run Random Vibration Analysis
Expand All @@ -161,25 +146,24 @@
analysis_request = SherlockAnalysisService_pb2.RunStrainMapAnalysisRequest
sherlock.analysis.run_strain_map_analysis(
"Test",
"Card",
"Main Board",
[
[
RunStrainMapAnalysisRequestAnalysisType.RANDOM_VIBE,
[
["Phase 1", "Random Event", "TOP", "StrainMap - Top"],
["Phase 1", "Random Event", "BOTTOM", "StrainMap - Bottom"],
["On The Road", "1 - Vibration", "TOP", "StrainMap - Top"],
["On The Road", "1 - Vibration", "BOTTOM", "StrainMap - Bottom"],
],
]
],
)
except SherlockRunStrainMapAnalysisError as e:
print(f"Error running random vibration analysis: {str(e)}")
print(f"Error running random vibration analysis: {e}")

###############################################################################
# Exit Sherlock
# =============
# Exit the gRPC connection and shut down Sherlock.

time.sleep(5)
sherlock.common.exit(True)
print("Sherlock gRPC connection closed successfully.")
Loading

0 comments on commit c437b86

Please sign in to comment.