Calculate computer cost#
Download the computer model used in this example and import it into a new project to work on.
Download the model:
Computer Model.Open the SAM editor in your browser and select the desired organization (for example, MyOrga).
Select New Project > SysML V2 > Import File.
Select Choose File for the File to import option.
Select the
computer.xmifile that you just downloaded. The project name is automatically set tocomputer.Click Import and wait for the project to load.
You can now work on this computer model.
Calculate cost#
Note
You need to replace the organization ID, server URL, and token with your own data. For more information, see Find information.
Calculate computer cost using recursive traversal#
1# Copyright (C) 2024 - 2026 ANSYS, Inc. and/or its affiliates.
2# SPDX-License-Identifier: MIT
3#
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in all
13# copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
22
23"""Computer cost example for PySAM SysML2."""
24
25import requests
26from urllib3.exceptions import InsecureRequestWarning
27
28from ansys.sam.sysml2 import AnsysSysML2APIConnector, SysML2ProjectManager
29from ansys.sam.sysml2.tools import SysMLTools
30
31# Used to disable warnings
32requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
33
34ansyssysml2apiconnector = AnsysSysML2APIConnector(
35 server_url="<SAM Server URL>", # Your SAM server base URL
36 organization_id="<Orga ID>", # The organization ID
37 token="<Token>", # Your authorization token
38 use_ssl=False, # If the server hasn't a valid SSL
39)
40
41project_manager = SysML2ProjectManager(connector=ansyssysml2apiconnector)
42
43project = project_manager.get_scripting_project(
44 "<Computer Project ID>"
45) # You can find your project ID in the URL of the editor.
46
47real_systems = project.get_root_package().RealSystems
48
49
50def assess_cost(element):
51 """Calculate the cost of element."""
52 if hasattr(element, "cost") and (element.cost.get_value() is not None):
53 cost = element.cost.get_value()
54 if type(cost) is int:
55 return cost
56 elif type(cost) is tuple: # a tuple means an int value and a unit
57 return cost[0]
58 raise ValueError(f"Problem of value type for the cost of {element._name}")
59 cost = 0
60 for sub_element in element._ownedElement:
61 if SysMLTools.isinstance(sub_element, "PartUsage"):
62 cost += assess_cost(sub_element)
63 return cost
64
65
66for system in real_systems._ownedElement:
67 print(system._name, " : ", assess_cost(system))
Calculate computer cost using recursive traversal#
1# Copyright (C) 2024 - 2026 ANSYS, Inc. and/or its affiliates.
2# SPDX-License-Identifier: MIT
3#
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in all
13# copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
22
23"""Computer cost static example for PySAM SysML2."""
24
25import requests
26from urllib3.exceptions import InsecureRequestWarning
27
28from ansys.sam.sysml2 import AnsysSysML2APIConnector, SysML2ProjectManager
29from ansys.sam.sysml2.meta_model.attribute_usage import AttributeUsage
30from ansys.sam.sysml2.meta_model.element import Element
31from ansys.sam.sysml2.meta_model.package import Package
32from ansys.sam.sysml2.meta_model.part_usage import PartUsage
33
34# Used to disable warnings
35requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
36
37ansyssysml2apiconnector = AnsysSysML2APIConnector(
38 server_url="<SAM Server URL>", # Your SAM server base URL
39 organization_id="<Orga ID>", # The organization ID
40 token="<Token>", # Your authorization token
41 use_ssl=False, # If the server hasn't a valid SSL
42)
43
44project_manager = SysML2ProjectManager(connector=ansyssysml2apiconnector)
45
46project = project_manager.get_sysml_project(
47 "<Computer Project ID>"
48) # You can find your project ID in the URL of the editor.
49
50real_systems: Package = project.get_root_package().get("RealSystems")
51
52
53def assess_cost(element: Element):
54 """Calculate the cost of element."""
55 cost_attribute: AttributeUsage = element.get("cost")
56 if (cost_attribute is not None) and (cost_attribute.get_value() is not None):
57 cost = cost_attribute.get_value()
58 if type(cost) is int:
59 return cost
60 elif type(cost) is tuple: # a tuple means an int value and a unit
61 return cost[0]
62 raise ValueError(f"Problem of value type for the cost of {element._name}")
63 cost = 0
64 for sub_element in element.owned_element:
65 if isinstance(sub_element, PartUsage):
66 cost += assess_cost(sub_element)
67 return cost
68
69
70for system in real_systems.owned_element:
71 print(system.name, " : ", assess_cost(system))