เจาะลึกการเขียน Script ใน scSTREAM

เมื่อเร็วๆ นี้ Christophe Gayet, วิศวกรฝ่ายสนับสนุนทางเทคนิค (Technical Support Engineer) จาก Hexagon ผู้มีประสบการณ์กว่า 14 ปี ได้จัด Webinar แนะนำการเขียน Script ในโปรแกรม scSTREAM อย่างละเอียด คู่มือนี้ได้สรุปประเด็นสำคัญและแนวทางการนำ scSTREAM Scripting ไปประยุกต์ใช้งานจริง โดยเน้นถึงประโยชน์, วิธีการเริ่มต้นเขียน Script ด้วยตนเอง และตัวอย่างการใช้งานที่เข้าใจง่าย

Screenshot of webinar introduction slide with Christophe Gayet's name and title.
ข้อมูลเบื้องต้นเกี่ยวกับ Webinar แนะนำการเขียน Script โดย Christophe Gayet
Source: (SimAcademy Webinar) An Introduction for Scripting within scSTREAM

ทำไมต้องใช้ scSTREAM Scripting?

A slide explaining script uses, listing tasks such as adjusting temperature, setting heat sources, setting material properties, and adding user variables in a smart thermostat CFD model. Scripts can address such need.
Source: (SimAcademy Webinar) An Introduction for Scripting within scSTREAM

การเขียน Script ใน scSTREAM มีประโยชน์ในหลายกรณี เช่น:

  • การปรับอุณหภูมิของลมที่เป่าออกมา (Blowing Temperature) แบบ Dynamic ตามอุณหภูมิภายในห้อง
  • การกำหนดแหล่งกำเนิดความร้อน (Heat Sources) ที่ขึ้นอยู่กับข้อมูลอุณหภูมิตามตำแหน่งในพื้นที่
  • การใช้คุณสมบัติของวัสดุที่กำหนดเองผ่านสมการ
  • การเพิ่มตัวแปรและผลลัพธ์ที่ผู้ใช้กำหนดเองเข้าไปในไฟล์ Simulation

ประวัติการพัฒนา Script

ในยุคแรก scSTREAM อนุญาตให้มี User Functions ผ่านการแก้ไขและ Compile ไฟล์ ซึ่งจำกัดการใช้งานข้าม Platform แต่ scSTREAM Scripting ในปัจจุบันใช้ JavaScript ที่ฝังโดยตรงใน Project Files (.S files) ทำให้สามารถใช้งานข้าม Platform ได้ทันทีโดยไม่จำเป็นต้อง Compile เพิ่มเติม

Diagram comparing user functions and scripts in STREAM software, featuring compilation requirements, language options, and integration with the STREAM solver—ideal for tasks like smart thermostat CFD model development or a temperature control script CFD.
ความเป็นมาของ Scripting ใน scSTREAM
Source: (SimAcademy Webinar) An Introduction for Scripting within scSTREAM

ประเภทของ Scripts ใน scSTREAM

A software interface shows parameter settings for a simulation. A dropdown menu highlights “Unformatted script,” and text explains formatted and unformatted scripts using a temperature control script CFD example.
Source: (SimAcademy Webinar) An Introduction for Scripting within scSTREAM

Unformatted Scripts

  • เป็น Script ที่ไม่ต้องระบุ Arguments ทำให้สามารถเขียนโค้ดได้อย่างยืดหยุ่นและใช้งานได้หลากหลาย
  • มีประโยชน์สำหรับการกำหนด Flux หรือ Heat Sources แบบ Dynamic โดยอิงตามพิกัดเชิงพื้นที่ (Spatial Coordinates)
A software interface shows parameter settings for a simulation. A dropdown menu highlights “Unformatted script,” and text explains formatted and unformatted scripts using a temperature control script CFD example.
Source: (SimAcademy Webinar) An Introduction for Scripting within scSTREAM

Formatted Scripts

  • เป็น Script ที่มี Context และ Arguments ที่กำหนดไว้ล่วงหน้า ใช้สำหรับตั้งค่า Parameter ของ Solver หรือดึงข้อมูลจาก Solver โดยเฉพาะ
  • แบ่งออกเป็นหมวดหมู่ได้ดังนี้:
    • Setting Functions: ใช้สำหรับกำหนดค่า Parameter ของ Solver
    • Timing Functions: ใช้สั่งให้ Script ทำงาน ณ ช่วงเวลาที่กำหนดของการจำลอง
    • Utility Functions: ใช้สำหรับดึงข้อมูลจาก Solver หรือบันทึกข้อความ (Log Messages)
A PowerPoint slide explains "Formatted scripts" in a software, listing key points and showing a screenshot of a user interface for inputting a numeric value—ideal for topics like the scFLOW thermostatic control script.
Source: (SimAcademy Webinar) An Introduction for Scripting within scSTREAM

ตัวอย่างการใช้งานจริง

Setting Functions

หนึ่งในแอปพลิเคชันที่พบบ่อยของ scSTREAM Scripting คือการตั้งค่า Heat Source แบบ Dynamic:

  • กำหนด Formatted Script ในส่วน Solver Parameters โดยใช้ Arguments ที่สงวนไว้ เช่น UXV และ TELP
  • ดึงข้อมูลพิกัดของ Element (Element Coordinates) หรือเงื่อนไขต่างๆ แบบ Dynamic เพื่อปรับค่า Heat Source
Slide showing formatted script setup for dynamic heat sources.
ตัวอย่างการกำหนด Heat Source แบบ Dynamic
Source: (SimAcademy Webinar) An Introduction for Scripting within scSTREAM

Timing Functions

ใช้ USRPOS เพื่อ:

  • สั่งให้ Script ทำงานตอนเริ่มต้น (Initialization), ณ รอบเวลา (Cycle) ที่กำหนด หรือก่อนที่จะทำการบันทึกไฟล์ผลลัพธ์
  • บันทึกข้อมูลสำคัญ เช่น ค่า Global Variable Values ลงในไฟล์ .L โดยตรง เพื่อนำไปใช้ในขั้นตอน Post-Processing
Slide depicting the use of USRPOS for timing function scripts.
ตัวอย่าง Timing Functions ใน scSTREAM

Utility Functions

  • ดึงค่าเชิงกายภาพ (Physical Quantities) โดยใช้ ERCATCH หรือ USREARCATCH
  • รับค่าปริพันธ์ (Integral) หรือค่าเฉลี่ยผ่าน Variable Registration เพื่อนำมาใช้ปรับ Script แบบ Dynamic
Slide detailing retrieval of physical quantities via utility functions.
ตัวอย่าง Utility Functions ใน scSTREAM

สถานการณ์ตัวอย่าง: การควบคุมอุณหภูมิแบบ Dynamic

ลองพิจารณาสถานการณ์ในห้องหนึ่ง ที่ต้องการควบคุมอุณหภูมิของลมที่จ่ายเข้ามา (Inlet Air Temperature) โดยอิงตามค่าเฉลี่ยอุณหภูมิของห้อง:

  • Variable Registration: คำนวณค่าอุณหภูมิเฉลี่ยสำหรับพื้นที่ที่กำหนด
  • Dynamic Inlet Temperature: ใช้ Script เพื่อปรับเปลี่ยนอุณหภูมิที่ Inlet แบบ Dynamic ตามค่าเฉลี่ยอุณหภูมิที่คำนวณได้
  • Output Enhancements: เพิ่มผลลัพธ์ที่กำหนดเอง เช่น ค่าความแตกต่างของอุณหภูมิ เข้าไปในไฟล์ผลลัพธ์ (.FLD files) และไฟล์ Log (.L files) โดยตรง

ขั้นตอนการนำไปใช้:

  1. เปิดใช้งาน Variable Registration สำหรับ Volume และ Face เพื่อคำนวณค่าเฉลี่ยและ Flux
  2. กำหนด Formatted Scripts สำหรับเงื่อนไข Inlet แบบ Dynamic:
    • คำนวณอุณหภูมิโดยใช้ตัวแปรที่ลงทะเบียนไว้ (Registered Variables)
    • ปรับอุณหภูมิที่ Inlet ตามสัดส่วนของ Feedback ที่ได้จากอุณหภูมิห้อง
  3. ปรับปรุงผลลัพธ์:
    • ใช้ Script สำหรับบันทึกข้อมูลเพิ่มเติมโดยใช้ USRPOS
    • สั่งให้แสดงผลค่าความแปรผันของอุณหภูมิที่คำนวณได้ เพื่อการวิเคราะห์หลังการประมวลผล (Post-Analysis) ที่ละเอียดขึ้น
Implementation steps for dynamic temperature control
ขั้นตอนการทำ Dynamic Temperature Control

เคล็ดลับสำคัญ

  • เมนูคลิกขวา: ใช้เมนูคลิกขวา (Right-click context menus) ให้เป็นประโยชน์ เพื่อเข้าถึงตัวแปรที่กำหนดไว้ล่วงหน้า, รายละเอียด Mesh, และ Function ที่มีให้ใช้งานได้อย่างรวดเร็ว
  • Documentation: หมั่นเปิดอ่านคู่มือ scSTREAM Documentation เพื่อดูรายละเอียดของ Function และตัวอย่างของ scSTREAM Scripting
Slide emphasizing the importance of right-click context menus and documentation.
เคล็ดลับสำคัญเพื่อการเขียน Script ที่มีประสิทธิภาพ

ถาม-ตอบ (Questions and Answers)

ในระหว่าง Webinar มีการตอบคำถามที่น่าสนใจหลายข้อ:

  • ปัจจุบันยังไม่รองรับ Python Scripting โดยยังคงใช้ JavaScript เป็นภาษาหลักในการเขียน Script
  • Script สามารถควบคุมการไหลของอากาศ (Airflow) แบบ Dynamic ตามอุณหภูมิในแต่ละพื้นที่ได้ ซึ่งเป็นประโยชน์อย่างยิ่งสำหรับ Data-Driven HVAC Simulations
  • Script มีศักยภาพในการเปิดใช้งานการปรับเปลี่ยน (Dynamic Adjustments) Parameter ของ Simulation เช่น Timestep ตามเงื่อนไขของตัวแปร ซึ่งขึ้นอยู่กับความเข้ากันได้ของ Solver

สรุป

Webinar ของ Christophe ได้สรุปภาพรวมการใช้งานจริง, ความรู้พื้นฐานที่จำเป็น และตัวอย่างอย่างละเอียด เพื่อให้สามารถนำ Scripting ใน scSTREAM ไปใช้ได้อย่างมีประสิทธิภาพ การนำ scSTREAM Scripting มาปรับใช้จะช่วยเพิ่มความยืดหยุ่นและระบบอัตโนมัติในการทำ Simulation ได้อย่างมาก ซึ่งจำเป็นอย่างยิ่งสำหรับสถานการณ์ที่ซับซ้อนและเปลี่ยนแปลงตลอดเวลาในงานด้านพลศาสตร์ของไหลเชิงคำนวณ (Computational Fluid Dynamics – CFD)

อ้างอิง

หากต้องการรับชม Webinar ฉบับเต็ม สามารถเข้าไปที่: SimAcademy Webinar – An Introduction for Scripting within scSTREAM