planformcreator2 4.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,495 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ """
5
+
6
+ App model and state management for the PlanformCreator2.
7
+
8
+ - holding stateful data like wing, planform, etc.
9
+ - signals for changes in data to inform the UI
10
+ - watchdog thread for monitoring polar generation
11
+ - can be notified of changes in planform geometry and other parameters
12
+
13
+ The App Model is needed as the 'real' model is QObject agnostic and stateless.
14
+
15
+ """
16
+
17
+ import os
18
+ from enum import Enum, auto
19
+ from typing import override
20
+ from PyQt6.QtCore import pyqtSignal, QObject, QThread, QTimer
21
+
22
+ from airfoileditor.resources import get_assets_dir as ae_assets_dir
23
+ from airfoileditor.base.common_utils import Parameters, clip
24
+ from airfoileditor.base.app_utils import Settings
25
+ from airfoileditor.model.airfoil_examples import Example
26
+ from airfoileditor.model.xo2_driver import Worker
27
+ from airfoileditor.model.polar_set import Polar_Task, Polar_Definition, polarType
28
+
29
+ # --- the real model imports
30
+
31
+ from .model.wing import Wing, WingSection, FILENAME_NEW
32
+ from .model.VLM_wing import VLM_OpPoint, VLM_Polar
33
+
34
+
35
+ import logging
36
+ logger = logging.getLogger(__name__)
37
+ # logger.setLevel(logging.DEBUG)
38
+
39
+
40
+ # -----------------------------------------------------------------------------
41
+
42
+ class Mode_Id(Enum):
43
+ """ Application Mode Identifiers """
44
+ VIEW = auto()
45
+ MODIFY = auto()
46
+
47
+
48
+ # -----------------------------------------------------------------------------
49
+
50
+
51
+
52
+ class App_Model (QObject):
53
+
54
+ """
55
+ The App Mode is a shell around the Model enriched with App specific objects
56
+ having a state of the current Airfoil
57
+ and provides Signals when data is changed
58
+
59
+ """
60
+
61
+ WORKER_MIN_VERSION = '1.0.10'
62
+
63
+ sig_new_mode = pyqtSignal() # new mode selected
64
+ sig_new_wing = pyqtSignal() # new wing loaded
65
+
66
+ sig_planform_changed = pyqtSignal() # planform geometry changed
67
+ sig_wingSection_selected = pyqtSignal() # current wing section selected
68
+ sig_wingSection_changed = pyqtSignal() # current wing section changed
69
+ sig_polar_set_changed = pyqtSignal() # new polar sets attached to airfoil
70
+ sig_new_polars = pyqtSignal() # new polars generated
71
+ sig_paneling_changed = pyqtSignal() # paneling definition changed
72
+
73
+ sig_vlm_polar_reset = pyqtSignal() # VLM reset - new polar will be generated on demand
74
+ sig_vlm_polar_changed = pyqtSignal() # current VLM polar changed
75
+ sig_vlm_opPoint_changed = pyqtSignal() # current VLM operating point changed
76
+
77
+
78
+ def __init__(self, workingDir_default: str = None):
79
+ super().__init__()
80
+
81
+ self._workingDir_default= workingDir_default if workingDir_default else os.getcwd()
82
+
83
+ logger.info (f"Init App_Model - working dir: {self._workingDir_default}")
84
+
85
+
86
+ self._version = "" # application version
87
+ self._change_text = "" # change text for this version
88
+ self._is_first_run = False # is first run of this version
89
+
90
+ self._wing : Wing = None # actual wing model
91
+ self._cur_wingSection : WingSection = None # current selected wing section
92
+
93
+ self._cur_vlm_alpha : float = None # current VLM opPoint alpha
94
+ self._cur_vlm_polar : VLM_Polar = None # for change detection current VLM polar
95
+ self._cur_polar_def : Polar_Definition = None # current polar definition
96
+ self._vlm_alpha_fixed_to_max : bool = False # is vlm alpha fixed to max lift alpha
97
+
98
+ self._mode_id : Mode_Id = None # current app mode
99
+ self._is_lower_minimized= False # is lower panel minimized
100
+
101
+ # set working dir for Example airfoils created
102
+ Example.workingDir_default = workingDir_default
103
+
104
+ # setup path of worker and its working dir
105
+ Worker (workingDir=self.workingDir_default).isReady (ae_assets_dir(), min_version=self.WORKER_MIN_VERSION)
106
+
107
+ # initialize watchdog thread for polars and xo2 state changes
108
+ self._init_watchdog()
109
+
110
+
111
+ def __repr__(self):
112
+ """ nice representation of self """
113
+ return f"<App_Model {self.wing}>"
114
+
115
+
116
+ def _init_watchdog (self):
117
+ """ initialize watchdog thread to check for new polars and xo2 state changes """
118
+
119
+ self._watchdog = Watchdog (self)
120
+
121
+ # watch for new polars
122
+ self._watchdog.sig_new_polars.connect (self.sig_new_polars.emit)
123
+
124
+ self._watchdog.start()
125
+
126
+
127
+ def _finish_watchdog (self):
128
+ """ finish watchdog thread """
129
+
130
+ if self._watchdog:
131
+ self._watchdog.requestInterruption()
132
+ self._watchdog.wait (2000) # wait max 2s for finish
133
+ self._watchdog = None
134
+
135
+
136
+ def _get_vlm_alpha_max (self) -> float | None:
137
+ """ get alpha max from VLM polar """
138
+
139
+ if self.cur_vlm_polar is None:
140
+ return None # no polar yet - wait
141
+ else:
142
+ opPoint = self.cur_vlm_polar.opPoint_at_alpha_max ()
143
+ if opPoint is not None:
144
+ return opPoint.alpha # we got it
145
+ else:
146
+ return None # maybe polars were not ready - so wait
147
+
148
+
149
+ # --- properties
150
+
151
+ def set_app_info (self, version: str, change_text: str, is_first_run: bool):
152
+ """ set application info """
153
+ self._version = version
154
+ self._change_text = change_text
155
+ self._is_first_run = is_first_run
156
+
157
+
158
+ @property
159
+ def mode_id (self) -> Mode_Id:
160
+ """ current application mode id """
161
+ return self._mode_id
162
+
163
+
164
+ def set_mode (self, mode_id : Mode_Id):
165
+ """ set new application mode id and case """
166
+
167
+ # sanity - ensure mode and case are compatible
168
+
169
+ ok = False
170
+ if mode_id == Mode_Id.MODIFY:
171
+ ok = True
172
+
173
+ if not ok:
174
+ raise ValueError (f"{self} cannot set mode {mode_id}")
175
+
176
+ self._mode_id = mode_id
177
+ self.sig_new_mode.emit()
178
+
179
+
180
+ @property
181
+ def is_ready (self) -> bool:
182
+ """ is app model ready to work"""
183
+ return self.wing
184
+
185
+ @property
186
+ def is_mode_view (self) -> bool:
187
+ """ is current mode view """
188
+ return self._mode_id == Mode_Id.VIEW
189
+
190
+ @property
191
+ def is_mode_modify (self) -> bool:
192
+ """ is current mode modify """
193
+ return self._mode_id == Mode_Id.MODIFY
194
+
195
+
196
+
197
+ @property
198
+ def wing (self) -> Wing:
199
+ """ current wing """
200
+ return self._wing
201
+
202
+ @property
203
+ def cur_wingSection (self) -> WingSection:
204
+ """ Dispatcher for current WingSection between Edit and Diagram """
205
+
206
+ # ensure cur_wingSection still exists
207
+ if self._cur_wingSection:
208
+ try:
209
+ self._cur_wingSection.index ()
210
+ except:
211
+ self._cur_wingSection = None
212
+
213
+ # handle first time or not existing
214
+ if self._cur_wingSection is None:
215
+ real_sections = self.wing.planform.wingSections.without_for_panels
216
+
217
+ if len (real_sections) > 2:
218
+ self._cur_wingSection = real_sections[1] # set second section as initial
219
+ else:
220
+ self._cur_wingSection = real_sections[0] # take root
221
+
222
+ return self._cur_wingSection
223
+
224
+
225
+ def set_cur_wingSection (self, aSection : WingSection | str | None):
226
+ """ set current wing section either by object or by string"""
227
+
228
+ if isinstance (aSection, str):
229
+ sec = next((sec for sec in self.wing.planform.wingSections if sec.name_short == aSection), None)
230
+ else:
231
+ sec = aSection
232
+
233
+ self._cur_wingSection = sec
234
+ logger.debug (f"{sec} as current")
235
+
236
+ self.sig_wingSection_selected.emit()
237
+
238
+
239
+ # --- VLM -----
240
+
241
+ @property
242
+ def cur_polar_def (self) -> Polar_Definition:
243
+ """ selected T1 polar definition of wingSection at root"""
244
+
245
+ new_polar_def = self._cur_polar_def
246
+
247
+ # ensure current polar def is in existing polar definitions
248
+ if self._cur_polar_def not in self.polar_definitions_T1:
249
+ new_polar_def = None
250
+
251
+ if self._cur_polar_def is None and self.polar_definitions_T1:
252
+ new_polar_def = self.polar_definitions_T1[0]
253
+
254
+ if new_polar_def != self._cur_polar_def:
255
+ self._cur_polar_def = new_polar_def
256
+ logger.debug (f"{self} set cur_polar_def to {new_polar_def}")
257
+
258
+ # reset current VLM opPoint
259
+ self._cur_vlm_alpha = None if self.vlm_alpha_fixed_to_max else 0.0
260
+ self.sig_vlm_opPoint_changed.emit()
261
+
262
+ return self._cur_polar_def
263
+
264
+
265
+ def set_cur_polar_def (self, polar_def : Polar_Definition):
266
+ """ set selected polar definition of wingSection at root"""
267
+
268
+ if polar_def != self._cur_polar_def and polar_def in self.wing.polar_definitions:
269
+ self._cur_polar_def = polar_def
270
+ logger.debug (f"{self} set cur_polar_def to {polar_def}")
271
+
272
+ # reset current VLM opPoint
273
+ self.notify_polar_definitions_changed ()
274
+
275
+
276
+ @property
277
+ def polar_definitions_T1 (self) -> list[Polar_Definition]:
278
+ """ available T1 polar definitions of wingSection at root"""
279
+
280
+ return [polar_def for polar_def in self.wing.polar_definitions if polar_def.type == polarType.T1]
281
+
282
+ @property
283
+ def cur_vlm_polar (self) -> VLM_Polar:
284
+ """ current VLM polar based on cur polar_def at root chord """
285
+
286
+ if self.wing.vlm_wing and self.cur_polar_def:
287
+ v = self.cur_polar_def.calc_v_for_chord(self.wing.planform.chord_root)
288
+ new_polar = self.wing.vlm_wing.polar_at (v)
289
+ # do change detection for signal
290
+ if new_polar != self._cur_vlm_polar:
291
+ self._cur_vlm_polar = new_polar
292
+ logger.debug (f"{self} set cur_vlm_polar to {new_polar}")
293
+ self.sig_vlm_polar_changed.emit()
294
+ return new_polar
295
+ else:
296
+ return None
297
+
298
+ @property
299
+ def cur_vlm_alpha (self) -> float:
300
+ """ alpha for current vlm opPoint"""
301
+ # if fixed to max, try to get it
302
+ if self.vlm_alpha_fixed_to_max and self._cur_vlm_alpha is None:
303
+ self._cur_vlm_alpha = self._get_vlm_alpha_max ()
304
+
305
+ return self._cur_vlm_alpha if self._cur_vlm_alpha is not None else 0.0
306
+
307
+ def set_cur_vlm_alpha (self, aVal : float):
308
+ aVal = clip (aVal, -20, 20)
309
+ if self._cur_vlm_alpha != aVal:
310
+ self._cur_vlm_alpha = aVal
311
+ self.sig_vlm_opPoint_changed.emit()
312
+
313
+ @property
314
+ def vlm_alpha_fixed_to_max (self) -> bool:
315
+ """ is vlm alpha fixed to max lift alpha """
316
+ return self._vlm_alpha_fixed_to_max
317
+
318
+ def set_vlm_alpha_fixed_to_max (self, aBool : bool):
319
+ if self._vlm_alpha_fixed_to_max != aBool:
320
+ self._vlm_alpha_fixed_to_max = aBool
321
+
322
+ if aBool: # try to set to max now
323
+ self._cur_vlm_alpha = self._get_vlm_alpha_max ()
324
+ self.sig_vlm_opPoint_changed.emit()
325
+
326
+ @property
327
+ def cur_vlm_opPoint (self) -> VLM_OpPoint:
328
+ """ current VLM operating point based on alpha """
329
+ if self.cur_vlm_polar:
330
+ return self.cur_vlm_polar.opPoint_at (self.cur_vlm_alpha)
331
+ else:
332
+ return None
333
+
334
+
335
+ def load_wing (self, pathFilename, as_new = False):
336
+ """ creates / loads new wing as current"""
337
+
338
+ self._wing = Wing (pathFilename, defaultDir=self.workingDir_default)
339
+
340
+ if as_new:
341
+ logger.info (f"{self} Loading new wing from {pathFilename}")
342
+
343
+ # set working dir of new wing to default working dir
344
+ new_pathFilename_abs = os.path.join(self.workingDir_default, FILENAME_NEW)
345
+ self.wing.save (new_pathFilename_abs)
346
+
347
+ self._cur_wingSection = None
348
+ self._cur_vlm_alpha = None
349
+ self._cur_polar_def = None
350
+
351
+ self.sig_new_wing.emit()
352
+
353
+
354
+ def cleanup_wing (self, all=False):
355
+ """ close current wing - remove temp files"""
356
+
357
+ # remove airfoil strak dir etc...
358
+ if all:
359
+ self.wing.remove_tmp ()
360
+
361
+ # remove airfoils dir if new file or empty
362
+ self.wing.remove_airfoils_dir_not_needed ()
363
+
364
+ # remove lost worker input files
365
+ if Worker.ready:
366
+ Worker().clean_workingDir (self.wing.workingDir)
367
+
368
+
369
+
370
+ def notify_polar_definitions_changed (self):
371
+ """ notify self that polar definitions have changed """
372
+
373
+ # as polar definitions could have changed, ensure a new initialized polarSet
374
+ self.wing.planform.wingSections.refresh_polar_sets (ensure=True)
375
+ self.sig_polar_set_changed.emit()
376
+
377
+ # reset alpha - may angle won't be available anymore
378
+ self._cur_vlm_alpha = None if self.vlm_alpha_fixed_to_max else 0.0
379
+
380
+ QTimer.singleShot(0, self.sig_vlm_polar_reset.emit) # notify after current events processed
381
+
382
+
383
+ def notify_planform_changed (self):
384
+ """ notify self that planform changed """
385
+ self.sig_planform_changed.emit()
386
+ self.notify_paneling_changed ()
387
+
388
+
389
+ def notify_wingSection_changed (self):
390
+ """ notify self that wing section changed """
391
+ self.sig_wingSection_changed.emit()
392
+ self.notify_paneling_changed ()
393
+
394
+
395
+ def notify_paneling_changed (self):
396
+ """ notify self that paneling changed """
397
+ self.wing.vlm_wing_reset ()
398
+ self.sig_paneling_changed.emit()
399
+
400
+ # init VLM recalculation
401
+ if self.vlm_alpha_fixed_to_max:
402
+ self._cur_vlm_alpha = None
403
+
404
+ QTimer.singleShot(0, self.sig_vlm_polar_reset.emit) # notify after current events processed
405
+
406
+
407
+ @property
408
+ def workingDir_default (self) -> str:
409
+ """ default working directory """
410
+ return self._workingDir_default
411
+
412
+ @property
413
+ def workingDir (self) -> str:
414
+ """ directory we are currently in (equals dir of wing if loaded) """
415
+ if self.wing and self.wing.workingDir:
416
+ return self.wing.workingDir
417
+ else:
418
+ return self.workingDir_default
419
+
420
+ @property
421
+ def settings (self) -> Parameters:
422
+ """ current loaded settings """
423
+ return self._settings
424
+
425
+
426
+ def close (self):
427
+ """ finish app model """
428
+
429
+ self._finish_watchdog()
430
+
431
+ self.cleanup_wing ()
432
+
433
+
434
+ # -----------------------------------------------------------------------------
435
+
436
+
437
+ class Watchdog (QThread):
438
+ """
439
+ Long running QThread to check if there is some new and signal parent
440
+
441
+ - new polars generated - check Polar.Tasks
442
+ - Xoptfoil2 state
443
+
444
+ """
445
+
446
+ sig_new_polars = pyqtSignal ()
447
+
448
+
449
+ def __repr__(self) -> str:
450
+ """ nice representation of self """
451
+ return f"<{type(self).__name__}>"
452
+
453
+
454
+ @override
455
+ def run (self) :
456
+ # Note: This is never called directly. It is called by Qt once the
457
+ # thread environment has been set up.
458
+ # Thread is started with .start()
459
+
460
+ logger.info (f"Starting WatchdogThread")
461
+ self.msleep (1000) # initial wait before polling begins
462
+
463
+ while not self.isInterruptionRequested():
464
+
465
+
466
+ # check for new polars
467
+
468
+ n_polars = 0
469
+ n_new_polars = 0
470
+
471
+ polar_tasks = Polar_Task.get_instances ()
472
+
473
+ for task in polar_tasks:
474
+
475
+ n_polars += task.n_polars
476
+ n_new_polars += task.load_polars()
477
+
478
+ if task.isCompleted():
479
+ task.finalize()
480
+ else:
481
+ # this ensures, that polars are returned in the order tasks were generated
482
+ # and not randomly by worker execution time -> more consistent diagram updates
483
+ # break
484
+ pass # deactivated
485
+
486
+ # if new polars loaded signal
487
+
488
+ if n_new_polars:
489
+
490
+ self.sig_new_polars.emit()
491
+ logger.debug (f"{self} --> {n_new_polars} new in {n_polars} polars")
492
+
493
+ self.msleep (500)
494
+
495
+ return