I have an involuntary sexual problem during sleep. After I fall asleep for some time, I wake up and in an involuntary and impulsive way I take down my pants, then I lie face down on bed and I touch my bare buttocks in a sensual way. This is accompanied by erection, sometimes it progresses rapidly to ejaculation. This occurs as if I am programmed to do this without complete consiousness or awareness, and I cannot stop myself. In the past, through the Internet, I was into viewing people being spanked on their bare buttocks. However, with the grace of God, I stopped watching this, and I stopped masturbation. I would like to return to normal sleeping without awakening in the middle of the night. Could you please provide me with advice and help? Is there a way to obtain purity of thoughts and purity of the subconscious mind?
You are describing having wet dreams. Men constantly produce semen and eventually the semenal vesicles get full. The body has to get rid of older semen to make room for newer batches of semen. Since you no longer masturbate, your body is taking care of the need by masturbating in your sleep or in your case in that state of sleep when you are semi-conscious. It is very similar to sleep walking. Your body is using the habits you built up when you were masturbating to pornography to trigger the physical reaction needed to ejaculate. The brain also provides images that you've connected to erotic ideas as additional motivation, which is where the "dreams" come in. The only odd thing is that what the brain connects to the idea of erotic is sometimes very weird.
You can't control your dreams directly. All you can do is not provide it with more bad material to work with. Just understand that this is the way your body works.
The only other choice you have is to remove what triggers wet dreams by keeping your seminal vesicles less full. That means learning to masturbate without pornography or other lustful thoughts.
Some guys rather have the wet dreams because they know they aren't directly responsible for what goes on. Others rather masturbate because they are awake and can apply conscious control over their thoughts and actions. That is a decision that I can't make for you.