solve.py 1.8 KB

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