solve.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from .coefficients import coefficients
  2. from .adaptation import hdpg1d
  3. from .postprocess import utils
  4. def queryYesNo(question, default="yes"):
  5. valid = {"yes": True, "y": True, "ye": True,
  6. "no": False, "n": False}
  7. if default is None:
  8. prompt = " [y/n] "
  9. elif default == "yes":
  10. prompt = " [Y/n] "
  11. elif default == "no":
  12. prompt = " [y/N] "
  13. else:
  14. raise ValueError("invalid default answer: '%s'" % default)
  15. while True:
  16. print(question + prompt)
  17. choice = input().lower()
  18. if default is not None and choice == '':
  19. return valid[default]
  20. elif choice in valid:
  21. return valid[choice]
  22. else:
  23. print("Please respond with 'yes' or 'no' "
  24. "(or 'y' or 'n').\n")
  25. def getCoefficients():
  26. question = 'Do you want to use the default parameters?'
  27. isDefault = queryYesNo(question, "yes")
  28. if (isDefault):
  29. Coeff = coefficients(1e-6, 0, 1, 2, 2, 1, 1)
  30. else:
  31. Coeff = coefficients.fromInput()
  32. return Coeff
  33. def menu():
  34. menu = {}
  35. menu['1.'] = "Solve with HDG."
  36. menu['2.'] = "Solve with HDPG."
  37. menu['3.'] = "Exit."
  38. for key, value in sorted(menu.items()):
  39. print(key, value)
  40. def hdgSolve():
  41. hdgCoeff = getCoefficients()
  42. print("Solving...")
  43. hdgSolution = hdpg1d(hdgCoeff)
  44. # solve the problem adaptively and plot convergence history
  45. hdgSolution.adaptive()
  46. print("Problem solved. Please check the convergence plot.")
  47. utils(hdgSolution).convHistory()
  48. def runInteractive():
  49. menu()
  50. selection = input("Please Select: ")
  51. while True:
  52. if selection == '1':
  53. hdgSolve()
  54. break
  55. elif selection == '2':
  56. print("In development...")
  57. elif selection == '3':
  58. print("Bye.")
  59. break
  60. else:
  61. print("Unknown Option Selected!")
  62. continue