Docsity

Apprendre thermo cal, Slides de Informations et informatique

Aperçu partiel du texte

Télécharge Apprendre thermo cal et plus Slides au format PDF de Informations et informatique sur Docsity uniquement!

TC-Python Documentation

Release 2023a

Thermo-Calc Software AB

Feb 03, 2023

  • 1 Installation Guide
    • 1.1 What type of installation should I choose?
    • 1.2 Using the Python-interpreter bundled to Thermo-Calc
      • 1.2.1 Limitations
      • 1.2.2 Step 1: Install an IDE (Integrated Development Environment)
      • 1.2.3 Step 2: Configure PyCharm to use the bundled Python-interpreter
      • 1.2.4 Step 3: Run a TC-Python Example
        • 1.2.4.1 Open the TC-Python Project in PyCharm
    • 1.3 Installing TC-Python into the Python-interpreter of your choice
      • 1.3.1 Step 1: Install a Python Distribution
        • 1.3.1.1 Install Anaconda
      • 1.3.2 Step 2: Install Thermo-Calc and the TC-Python SDK
      • 1.3.3 Step 3: Install TC-Python
      • 1.3.4 Step 4: Install an IDE (Integrated Development Environment)
      • 1.3.5 Step 5: Open the IDE and Run a TC-Python Example
        • 1.3.5.1 Open the TC-Python Project in PyCharm
        • 1.3.5.2 Fixing potential issues with the environment
    • 1.4 Updating to a newer version
  • 2 Mac OS: Setting Environment Variables
  • 3 Architecture overview
    • 3.1 TCPython
    • 3.2 SystemBuilder and System
    • 3.3 Calculation
      • 3.3.1 Single equilibrium calculations
      • 3.3.2 Batch equilibrium calculations
      • 3.3.3 Precipitation calculations
      • 3.3.4 Scheil calculations
      • 3.3.5 Property diagram calculations
      • 3.3.6 Phase diagram calculations
      • 3.3.7 Diffusion calculations
      • 3.3.8 Property Model calculations
      • 3.3.9 Material to Material calculations
      • 3.3.10 Process Metallurgy calculations
    • 3.4 Result
      • 3.4.1 DiffusionResult
    • 3.5 Property Model Framework
      • 3.5.1 Property models vs. TC-Python
      • 3.5.2 Architecture
      • 3.5.3 Property Model directory
  • 4 Best Practices
    • 4.1 Re-use of the single equilibrium calculation state
    • 4.2 Re-use and saving of results
    • 4.3 All TC-Python objects are non-copyable
    • 4.4 Python Virtual Environments
    • 4.5 Using with TCPython() efficiently
    • 4.6 Parallel calculations
    • 4.7 Handling crashes of the calculation engine
    • 4.8 Using TC-Python within a Jupyter Notebook or the Python console
    • 4.9 Property Model Framework
      • 4.9.1 Debugging Property Model code
      • 4.9.2 Developing Property Models in several files
        • 4.9.2.1 side-by-side modules
        • 4.9.2.2 common modules
      • 4.9.3 Alternative Python for Property Models
        • 4.9.3.1 Default bundled Python interpreter
        • 4.9.3.2 Configuring another Python interpreter
    • 4.10 Process Metallurgy Calculations
      • 4.10.1 Equilibrium calculations with changing elements between calculations
      • 4.10.2 Zones
      • 4.10.3 Applications
      • 4.10.4 Implementation of practical process models
  • 5 API Reference
    • 5.1 Calculations
      • 5.1.1 Module “single_equilibrium”
      • 5.1.2 Module “batch_equilibrium”
      • 5.1.3 Module “precipitation”
      • 5.1.4 Module “scheil”
      • 5.1.5 Module “step_or_map_diagrams”
      • 5.1.6 Module “diffusion”
      • 5.1.7 Module “propertymodel”
      • 5.1.8 Module “material_to_material”
      • 5.1.9 Package “process_metallurgy”
        • 5.1.9.1 Module “base”
        • 5.1.9.2 Module “equilibrium”
        • 5.1.9.3 Module “process”
    • 5.2 Module “system”
    • 5.3 Module “entities”
    • 5.4 Module “server”
    • 5.5 Module “quantity_factory”
    • 5.6 Module “utils”
    • 5.7 Module “propertymodel_sdk”
    • 5.8 Module “exceptions”
    • 5.9 Module “abstract_base”
  • 6 Troubleshooting
    • 6.1 Diagnostics script
    • 6.2 “No module named tc_python” error on first usage
    • 6.3 “pip install” fails with “Failed to establish a new network connection” or similar
  • Python Module Index

CHAPTER

ONE

INSTALLATION GUIDE

This guide helps you to get a working TC-Python API installation. There is a PDF guide included with your installation. In the Thermo-Calc menu, select Help → Manuals Folder. Then double-click to open the Software Development Kits (SDKs) folder.

Note: A license is required to run TC-Python.

1.1 What type of installation should I choose?

There are two possibilities to install TC-Python:

  1. Using the Python-interpreter bundled to Thermo-Calc : This interpreter has TC-Python preinstalled together with some popular Python-packages. This is the recommended option for new users to TC-Python, but it is limited to the preinstalled packages.
  2. Installing TC-Python into the Python-interpreter of your choice : This is the recommended option for any more advanced usage and provides full flexibility.

1.2 Using the Python-interpreter bundled to Thermo-Calc

Note: A Python-interpreter is bundled to Thermo-Calc beginning with version 2021a.

1.2.1 Limitations

The bundled Python 3.7.2 interpreter is containing the following major packages:

Package Version matplotlib 3.3. numpy 1.19. scikit-learn 0.23. scipy 1.5. TC-Python 2023a

TC-Python Documentation, Release 2023a

  1. Select your added interpreter and confirm.

1.2.4 Step 3: Run a TC-Python Example

Now you are ready to start working with TC-Python. It is recommended that you open one or more of the included examples to both check that the installation has worked and to start familiarizing yourself with the code.

1.2.4.1 Open the TC-Python Project in PyCharm

When you first open the TC-Python project and examples, it can take a few moments for the Pycharm IDE to index before some of the options are available.

  1. Open PyCharm and then choose File → Open. The first time you open the project you will need to navigate to the path of the TC-Python installation:

Operating sys- tem

Path to the TC-Python folder

Windows C:\Users\UserName\Documents\Thermo-Calc\2023a\SDK TC-Python Linux /home/UserName/Thermo-Calc/2023a/SDK/TC-Python MacOS /Users/Shared/Thermo-Calc/2023a/SDK/TC-Python

  1. Click on the Examples folder and then click OK.
  2. From any subfolder:
    • Double-click to open an example file to examine the code.
    • Right-click an example and choose Run.

Note: If you are not following the recommended approach and create a new project ( File → New Project... ), you need to consider that by default the options to choose the interpreter are hidden within the Create Project window. So click on Project Interpreter: New Virtual Environment and in most cases choose your System Interpreter containing the Python bundled to Thermo-Calc instead of the default New Virtual Environment.

1.3 Installing TC-Python into the Python-interpreter of your choice

1.3.1 Step 1: Install a Python Distribution

If you already have a Python distribution installation, version 3.5 or higher, skip this step. These instructions are based on using the Anaconda platform for the Python distribution. Install version 3.5 or higher to be able to work with TC-Python, although it is recommended that you use the most recent version.

1.3. Installing TC-Python into the Python-interpreter of your choice 3

TC-Python Documentation, Release 2023a

1.3.1.1 Install Anaconda

  1. Navigate to the Anaconda website: https://www.anaconda.com/download/.
  2. Click to choose your OS (operating system) and then click Download. Follow the instructions. It is recommended you keep all the defaults.

1.3.2 Step 2: Install Thermo-Calc and the TC-Python SDK

Note: TC-Python is available starting with Thermo-Calc version 2018a.

  1. Install Thermo-Calc
  2. When the installation is complete, open the TC-Python folder that includes the *.whl file needed for the next step. There is also an file: Examples folder with Python files you can use in the IDE to understand and work with TC-Python.

1.3.3 Step 3: Install TC-Python

On Windows, it is recommended that you use the Python distribution prompt (i.e. Anaconda,... ), espe- cially if you have other Python installations. Do not use Virtual Environments unless you have a good reason for that.

  1. Open the command line. For example, in Anaconda on a Windows OS, go to Start → Anaconda → Anaconda Prompt.
  2. At the command line, enter the following. Make sure there are no spaces at the end of the string or in the folder name or it will not run:

pip install /TC_Python--py3-none- ↪→any.whl

Tip: Note that on Linux depending on the interpreter usually pip3 is used.

Operating sys- tem

Path to the TC-Python folder

Windows C:\Users\UserName\Documents\Thermo-Calc\2023a\SDK TC-Python Linux /home/UserName/Thermo-Calc/2023a/SDK/TC-Python MacOS /Users/Shared/Thermo-Calc/2023a/SDK/TC-Python

  1. Press . When the process is completed, there is a confirmation that TC-Python is installed.

Note: If your computer is located behind a proxy-server, the default pip -command will fail with a network connection error. In that case you need to install the dependencies of TC-Python in a special configuration:

pip install -proxy user:password@proxy_ip:port py4j jproperties

4 Chapter 1. Installation Guide

TC-Python Documentation, Release 2023a

1.3.5.2 Fixing potential issues with the environment

In most cases you should run TC-Python within your global Python 3 interpreter and not use Virtual Environments unless you have a good reason to do so. A common problem on first usage of TC-Python is the error message “ No module named tc_python ”. You can resolve this and other problems with the interpreter settings as follows:

  1. Go the menu File → Settings.
  2. Navigate in the tree to Project: YourProjectName and choose Project Interpreter.
  3. Click on the settings symbol close to the Project Interpreter dropdown menu and choose Add.
  4. Now choose System Interpreter and add your existing Python 3 interpreter.
  5. Select your added interpreter and confirm.

Note: If you are not following the recommended approach and create a new project ( File → New Project... ), you need to consider that by default the options to choose the interpreter are hidden within the Create Project window. So click on Project Interpreter: New Virtual Environment and in most cases choose your System Interpreter instead of the default New Virtual Environment.

Note: If you really need to use a Virtual Environment, please consider the hints given in the Python Virtual Environments chapter.

1.4 Updating to a newer version

When updating to a newer version of Thermo-Calc, you need to also install the latest version of TC- Python. This is not necessary if are using the bundled Python-interpreter that has it automatically installed. It is not sufficient to run the installer of Thermo-Calc:

pip install /TC_Python--py3-none-any.whl

Tip: Note that on Linux depending on the interpreter usually pip3 is used.

In case of problems you may wish to uninstall the previous version of TC-Python in advance:

pip uninstall TC-Python pip install /TC_Python--py3-none-any.whl

However, that should normally not be required. You can check the currently installed version of TC-Python by running:

pip show TC-Python

6 Chapter 1. Installation Guide

CHAPTER

TWO

MAC OS: SETTING ENVIRONMENT VARIABLES

In order to use TC-Python on Mac you need to set some environment variables.

TC23A_HOME=/Applications/Thermo-Calc-2023a.app/Contents/Resources

If you use a license server:

LSHOST=

If you have a node-locked license:

LSHOST=NO-NET LSERVRC=/Users/Shared/Thermo-Calc/lservrc

In PyCharm, you can add environment variables in the configurations.

Select Run → Edit Configurations to open the Run/Debug Configurations window. Choose Templates and then Python. Enter the environment variable(s) by clicking the button to the right of the Environment Variables text field. Now the environment variables(s) will be set for each new configuration by default.

Note: Existing configurations need to be removed and recreated to obtain the environment variables in them.

The same way for configuring the environment variables can be used on other operating systems as if necessary.

CHAPTER

THREE

ARCHITECTURE OVERVIEW

TC-Python contains classes of these types:

  • TCPython – this is where you start with general settings.
  • SystemBuilder and System – where you choose database and elements etc.
  • Calculation – where you choose and configure the calculation.
  • Result – where you get the results from a calculation you have run.

3.1 TCPython

This is the starting point for all TC-Python usage.

You can think of this as the start of a “wizard”.

You use it to select databases and elements. That will take you to the next step in the wizard, where you configure the system.

Example:

from tc_python import *

with TCPython() as start: start.select_database_and_elements(...

e.t.c

after with clause

or like this

with TCPython(): SetUp().select_database_and_elements(...

e.t.c

after with clause

Tip: If you use TC-Python from Jupyter Lab / Notebook, you should use TC-Python slightly different to be able to use multiple cells. See Using TC-Python within a Jupyter Notebook or the Python console for details.

Note: When your python script runs a row like this:

with TCPython() as start:

TC-Python Documentation, Release 2023a

a process running a calculation server starts. Your code, via TC-Python, uses socket communication to send and receive messages to and from that server.

When your Python script has run as far as this row:

after with clause

the calculation server automatically shuts down, and all temporary files are deleted. It is important to ensure that this happens by structuring your Python code using a with() clause as in the above example.

Note: To re-use results from previous calculations, set a folder where TC-Python saves results, and looks for previous results.

This is done with the function set_cache_folder().

from tc_python import *

with TCPython() as start: start.set_cache_folder("cache")

This folder can be a network folder and shared by many users. If a previous TC-Python calculation has run with the same cache_folder and EXACTLY the same system and calculation settings, the calculation is not re-run. Instead the result is automatically loaded from disk.

It is also possible to explicitly save and load results.

from tc_python import *

with TCPython() as start: #... diffusion calculation (could be any calculation type) calculation_result.save_to_disk('path to folder') #... loaded_result = start.load_result_from_disk().diffusion('path to folder')

3.2 SystemBuilder and System

A SystemBuilder is returned when you have selected your database and elements in TCPython.

The SystemBuilder lets you further specify your system, for example the phases that should be part of your system.

Example:

from tc_python import *

with TCPython() as start: start.select_database_and_elements("ALDEMO", ["Al", "Sc"])

e.t.c

When all configuration is done, you call get_system() which returns an instance of a System class. The System class is fixed and cannot be changed. If you later want to change the database, elements or something else, change the SystemBuilder and call get_system() again, or create a new SystemBuilder and call get_system().

From the System you can create one or more calculations, which is the next step in the “wizard”.

10 Chapter 3. Architecture overview

TC-Python Documentation, Release 2023a

3.3.2 Batch equilibrium calculations

Batch equilibrium calculations are used when you want to do many single equilibrium calculations and it is known from the beginning which result values are required from the equilibrium. This is a vectorized type of calculation that can reduce the overhead from Python and TC-Python similar to the approach used in numpy -functions for example.

Tip: The performance of batch equilibrium calculations can be significantly better than looping and using single equilibrium calculations if the actual Thermo-Calc calculation is fast. There is little advantage if the Thermo-Calc equilibrium calculations take a long time (typically for large systems and databases).

Example:

from tc_python import *

with TCPython() as start: calculation = ( start .set_cache_folder(os.path.basename(file) + "_cache") .select_database_and_elements("NIDEMO", ["Ni", "Al", "Cr"]) .get_system() .with_batch_equilibrium_calculation() .set_condition("T", 800.1) .set_condition("X(Al)", 1E-2) .set_condition("X(Cr)", 1E-2) .disable_global_minimization() )

list_of_x_Al = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] list_of_x_Cr = [3, 5, 7, 9, 11, 13, 15] lists_of_conditions = [] for x_Al in list_of_x_Al: for x_Cr in list_of_x_Cr: lists_of_conditions.append([ ("X(Al)", x_Al / 100), ("X(Cr)", x_Cr / 100)]) calculation.set_conditions_for_equilibria(lists_of_conditions)

results = calculation.calculate(["BM", "VM"])

masses = results.get_values_of("BM") volumes = results.get_values_of('VM')

print(masses) print(volumes)

12 Chapter 3. Architecture overview

TC-Python Documentation, Release 2023a

3.3.3 Precipitation calculations

All that can be configured in the Precipitation Calculator in Graphical Mode can also be done here in this calculation. However, you must at least enter a matrix phase, a precipitate phase, temperature, simulation time and compositions.

Example:

from tc_python import *

with TCPython() as start: precipitation_curve = ( start. select_thermodynamic_and_kinetic_databases_with_elements("ALDEMO", "MALDEMO", ↪→ ["Al", "Sc"]). get_system(). with_isothermal_precipitation_calculation(). set_composition("Sc", 0.18). set_temperature(623.15). set_simulation_time(1e5). with_matrix_phase(MatrixPhase("FCC_A1"). add_precipitate_phase(PrecipitatePhase("AL3SC"))). calculate() )

3.3.4 Scheil calculations

All Scheil calculations available in Graphical Mode or Console Mode can also be done here in this calculation. The minimum you need to specify are the elements and compositions. Everything else is set to a default value.

Example:

from tc_python import *

with TCPython() as start: temperature_vs_mole_fraction_of_solid = ( start. select_database_and_elements("FEDEMO", ["Fe", "C"]). get_system(). with_scheil_calculation(). set_composition("C", 0.3). calculate(). get_values_of(ScheilQuantity.temperature(), ScheilQuantity.mole_fraction_of_all_solid_phases()) )

3.3. Calculation 13

TC-Python Documentation, Release 2023a

(continued from previous page) set_condition(ThermodynamicQuantity.mole_fraction_of_a_component("C"), 0. ↪→01). calculate(). get_values_grouped_by_stable_phases_of(ThermodynamicQuantity.mass_ ↪→fraction_of_a_component("C"), ThermodynamicQuantity. ↪→temperature()) )

3.3.7 Diffusion calculations

For diffusion calculations, everything that you can configure in the Diffusion Calculator can also be configured in this calculation. The minimum you need to specify are elements, temperature, simulation time, a region with a grid and width, a phase and an initial composition.

Example:

from tc_python import *

with TCPython() as start: diffusion_result = ( start. select_thermodynamic_and_kinetic_databases_with_elements("FEDEMO", "MFEDEMO", ↪→ ["Fe", "Ni"]). get_system(). with_isothermal_diffusion_calculation(). set_temperature(1400.0). set_simulation_time(108000.0). add_region(Region("Austenite"). set_width(1E-4). with_grid(CalculatedGrid.linear().set_no_of_points(50)). with_composition_profile(CompositionProfile(). add("Ni", ElementProfile.linear(10.0, 50.0)) ). add_phase("FCC_A1")). calculate())

distance, ni_fraction = diffusion_result.get_mass_fraction_of_component_at_time("Ni", ↪→ 108000.0)

3.3.8 Property Model calculations

For Property Model calculations, everything that you can configure in the Property Model Calculator in Graphical Mode can also be configured in this calculation. The minimum you need to specify are elements, composition and which Property Model you want to use.

Example:

from tc_python import *

with TCPython() as start: (continues on next page)

3.3. Calculation 15

TC-Python Documentation, Release 2023a

(continued from previous page) print("Available Property Models: {}".format(start.get_property_models())) property_model = ( start. select_database_and_elements("FEDEMO", ["Fe", "C"]). get_system(). with_property_model_calculation("Driving force"). set_composition("C", 1.0). set_argument("precipitate", "GRAPHITE"))

print("Available arguments: {}".format(property_model.get_arguments())) result = property_model.calculate()

print("Available result quantities: {}".format(result.get_result_quantities())) driving_force = result.get_value_of("normalizedDrivingForce")

3.3.9 Material to Material calculations

Material to Material calculations are generally regular single equilibrium, property diagram or phase diagram calcu- lations but they are specialised to handle the mixture of two materials A and B. Everything that you can configure in the Material to Material Calculator in Graphical Mode can also be configured in this calculation. The minimum required configuration is shown below for a Property diagram calculation for varying amount of material B. The other calculators (single fraction of material B and phase diagram calculations) are configured in a similar way.

Example:

from tc_python import *

with TCPython() as start: material_to_material_property_diagram = ( start. select_database_and_elements("FEDEMO", ["Fe", "Cr", "Ni", "C"]). get_system(). with_material_to_material(). with_property_diagram_calculation(). set_material_a({"Cr": 10.0, "Ni": 15.0}, "Fe"). set_material_b({"Cr": 15.0, "Ni": 10.0}, "Fe"). set_activities({"C": 0.1}). with_constant_condition(ConstantCondition.temperature(800 + 273.15)). with_axis(MaterialToMaterialCalculationAxis.fraction_of_material_b(from_ ↪→fraction=0.0, to_ ↪→fraction=1.0, start_ ↪→fraction=0.5)) )

result = material_to_material_property_diagram.calculate() data = result.get_values_grouped_by_quantity_of(MATERIAL_B_FRACTION, ThermodynamicQuantity.volume_ ↪→fraction_of_a_phase(ALL_PHASES))

(continues on next page)

16 Chapter 3. Architecture overview